mirror of
https://github.com/home-assistant/core.git
synced 2026-04-17 23:53:49 +01:00
Co-authored-by: Tao Jiang <tao.jiang@linkplay.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Joostlek <joostlek@outlook.com>
209 lines
6.6 KiB
Python
209 lines
6.6 KiB
Python
"""Tests for the WiiM config flow."""
|
|
|
|
from ipaddress import ip_address
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.wiim.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
|
from homeassistant.const import CONF_HOST
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|
|
|
DISCOVERY_INFO = ZeroconfServiceInfo(
|
|
ip_address=ip_address("192.168.1.100"),
|
|
ip_addresses=[ip_address("192.168.1.100")],
|
|
hostname="wiim-pro.local.",
|
|
name="WiiM Pro._linkplay._tcp.local.",
|
|
port=49152,
|
|
properties={"uuid": "uuid:test-udn-1234"},
|
|
type="_linkplay._tcp.local.",
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_probe_player", "mock_setup_entry")
|
|
async def test_user_flow_create_entry(hass: HomeAssistant) -> None:
|
|
"""Test the user flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "WiiM Pro"
|
|
assert result["data"] == {CONF_HOST: "192.168.1.100"}
|
|
assert result["result"].unique_id == "uuid:test-udn-1234"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_setup_entry")
|
|
async def test_user_flow_cannot_connect(
|
|
hass: HomeAssistant, mock_probe_player: AsyncMock
|
|
) -> None:
|
|
"""Test the user flow handles connection failures."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
mock_probe_player.side_effect = TimeoutError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
mock_probe_player.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_setup_entry")
|
|
async def test_user_flow_no_probe(
|
|
hass: HomeAssistant, mock_probe_player: AsyncMock
|
|
) -> None:
|
|
"""Test the user flow handles connection failures."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
old_return_value = mock_probe_player.return_value
|
|
|
|
mock_probe_player.return_value = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
|
|
|
mock_probe_player.return_value = old_return_value
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_probe_player")
|
|
async def test_user_flow_already_configured(
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
|
) -> None:
|
|
"""Test the user flow aborts for an already configured device."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_HOST: "192.168.1.100"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_probe_player", "mock_setup_entry")
|
|
async def test_zeroconf_flow(hass: HomeAssistant) -> None:
|
|
"""Test the zeroconf discovery flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_ZEROCONF},
|
|
data=DISCOVERY_INFO,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "discovery_confirm"
|
|
assert result["description_placeholders"] == {"name": "WiiM Pro"}
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "WiiM Pro"
|
|
assert result["data"] == {CONF_HOST: "192.168.1.100"}
|
|
assert result["result"].unique_id == "uuid:test-udn-1234"
|
|
|
|
|
|
async def test_zeroconf_flow_cannot_connect(
|
|
hass: HomeAssistant, mock_probe_player: AsyncMock
|
|
) -> None:
|
|
"""Test the zeroconf flow aborts on connection errors."""
|
|
mock_probe_player.side_effect = TimeoutError
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_ZEROCONF},
|
|
data=DISCOVERY_INFO,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "cannot_connect"
|
|
|
|
|
|
async def test_zeroconf_flow_no_probe(
|
|
hass: HomeAssistant, mock_probe_player: AsyncMock
|
|
) -> None:
|
|
"""Test the zeroconf flow aborts when probing failed."""
|
|
mock_probe_player.return_value = None
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_ZEROCONF},
|
|
data=DISCOVERY_INFO,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "cannot_connect"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_setup_entry")
|
|
async def test_zeroconf_flow_already_configured(
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
|
) -> None:
|
|
"""Test the zeroconf flow aborts for an already configured device."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_ZEROCONF},
|
|
data=ZeroconfServiceInfo(
|
|
ip_address=ip_address("192.168.1.101"),
|
|
ip_addresses=[ip_address("192.168.1.101")],
|
|
hostname="wiim-pro.local.",
|
|
name="WiiM Pro._linkplay._tcp.local.",
|
|
port=49152,
|
|
properties={"uuid": "uuid:test-udn-1234"},
|
|
type="_linkplay._tcp.local.",
|
|
),
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
assert mock_config_entry.data[CONF_HOST] == "192.168.1.101"
|