2019-12-08 08:26:31 +00:00
|
|
|
"""Tests for the Elgato Key Light config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2023-09-19 16:58:46 +00:00
|
|
|
from ipaddress import ip_address
|
2022-01-14 16:16:59 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
|
|
|
|
from elgato import ElgatoConnectionError
|
2023-02-07 18:14:13 +00:00
|
|
|
import pytest
|
2023-02-17 21:35:55 +00:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2021-11-15 19:04:21 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2022-01-13 12:17:16 +00:00
|
|
|
from homeassistant.components.elgato.const import DOMAIN
|
2019-12-08 08:26:31 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
2023-02-17 21:35:55 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE
|
2019-12-08 08:26:31 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-07 19:28:18 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
|
2021-02-13 22:50:25 +00:00
|
|
|
async def test_full_user_flow_implementation(
|
2022-01-14 16:16:59 +00:00
|
|
|
hass: HomeAssistant,
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato: MagicMock,
|
2022-01-14 16:16:59 +00:00
|
|
|
mock_setup_entry: AsyncMock,
|
2023-02-17 21:35:55 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2021-02-13 22:50:25 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the full manual user flow from start to finish."""
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
2022-01-14 16:16:59 +00:00
|
|
|
context={"source": SOURCE_USER},
|
2020-01-04 21:45:11 +00:00
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.FORM
|
2023-04-11 08:00:17 +00:00
|
|
|
assert result.get("step_id") == "user"
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2021-02-13 22:50:25 +00:00
|
|
|
result["flow_id"], user_input={CONF_HOST: "127.0.0.1", CONF_PORT: 9123}
|
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result2.get("type") == FlowResultType.CREATE_ENTRY
|
2023-02-17 21:35:55 +00:00
|
|
|
assert result2 == snapshot
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2023-02-19 19:14:18 +00:00
|
|
|
assert len(mock_elgato.info.mock_calls) == 1
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
|
2021-02-13 22:50:25 +00:00
|
|
|
async def test_full_zeroconf_flow_implementation(
|
2022-01-14 16:16:59 +00:00
|
|
|
hass: HomeAssistant,
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato: MagicMock,
|
2022-01-14 16:16:59 +00:00
|
|
|
mock_setup_entry: AsyncMock,
|
2023-02-17 21:35:55 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
2021-02-13 22:50:25 +00:00
|
|
|
"""Test the zeroconf flow from start to finish."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2022-01-14 16:16:59 +00:00
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-15 19:04:21 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("127.0.0.1"),
|
|
|
|
ip_addresses=[ip_address("127.0.0.1")],
|
2021-11-15 19:04:21 +00:00
|
|
|
hostname="example.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
2021-11-15 19:04:21 +00:00
|
|
|
port=9123,
|
2022-01-16 21:18:46 +00:00
|
|
|
properties={"id": "AA:BB:CC:DD:EE:FF"},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:04:21 +00:00
|
|
|
),
|
2021-02-13 22:50:25 +00:00
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("description_placeholders") == {"serial_number": "CN11A1A00001"}
|
|
|
|
assert result.get("step_id") == "zeroconf_confirm"
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.FORM
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2021-03-10 00:53:00 +00:00
|
|
|
progress = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(progress) == 1
|
2022-01-14 16:16:59 +00:00
|
|
|
assert progress[0].get("flow_id") == result["flow_id"]
|
|
|
|
assert "context" in progress[0]
|
|
|
|
assert progress[0]["context"].get("confirm_only") is True
|
2021-03-10 00:53:00 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2021-02-13 22:50:25 +00:00
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2022-01-14 16:16:59 +00:00
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result2.get("type") == FlowResultType.CREATE_ENTRY
|
2023-02-17 21:35:55 +00:00
|
|
|
assert result2 == snapshot
|
2022-01-14 16:16:59 +00:00
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2023-02-19 19:14:18 +00:00
|
|
|
assert len(mock_elgato.info.mock_calls) == 1
|
2021-02-13 22:50:25 +00:00
|
|
|
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
async def test_connection_error(
|
2022-01-14 16:16:59 +00:00
|
|
|
hass: HomeAssistant,
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato: MagicMock,
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we show user form on Elgato Key Light connection error."""
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato.info.side_effect = ElgatoConnectionError
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
2022-01-14 16:16:59 +00:00
|
|
|
context={"source": SOURCE_USER},
|
2021-02-13 22:50:25 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 9123},
|
2019-12-08 08:26:31 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.FORM
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("errors") == {"base": "cannot_connect"}
|
|
|
|
assert result.get("step_id") == "user"
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_connection_error(
|
2022-01-14 16:16:59 +00:00
|
|
|
hass: HomeAssistant,
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato: MagicMock,
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow on Elgato Key Light connection error."""
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato.info.side_effect = ElgatoConnectionError
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:45:11 +00:00
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-23 15:32:58 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("127.0.0.1"),
|
|
|
|
ip_addresses=[ip_address("127.0.0.1")],
|
2021-11-23 15:32:58 +00:00
|
|
|
hostname="mock_hostname",
|
|
|
|
name="mock_name",
|
|
|
|
port=9123,
|
|
|
|
properties={},
|
|
|
|
type="mock_type",
|
|
|
|
),
|
2019-12-08 08:26:31 +00:00
|
|
|
)
|
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("reason") == "cannot_connect"
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.ABORT
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
|
2023-02-19 19:14:18 +00:00
|
|
|
@pytest.mark.usefixtures("mock_elgato")
|
2019-12-08 08:26:31 +00:00
|
|
|
async def test_user_device_exists_abort(
|
2023-02-07 18:14:13 +00:00
|
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
|
2022-01-14 16:16:59 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
2022-01-14 16:16:59 +00:00
|
|
|
context={"source": SOURCE_USER},
|
2021-02-13 22:50:25 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 9123},
|
2020-01-04 21:45:11 +00:00
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.ABORT
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("reason") == "already_configured"
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
|
2023-02-19 19:14:18 +00:00
|
|
|
@pytest.mark.usefixtures("mock_elgato")
|
2019-12-08 08:26:31 +00:00
|
|
|
async def test_zeroconf_device_exists_abort(
|
2023-02-07 18:14:13 +00:00
|
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
|
2022-01-14 16:16:59 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
|
|
|
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
2021-11-23 15:32:58 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("127.0.0.1"),
|
|
|
|
ip_addresses=[ip_address("127.0.0.1")],
|
2021-11-23 15:32:58 +00:00
|
|
|
hostname="mock_hostname",
|
|
|
|
name="mock_name",
|
|
|
|
port=9123,
|
|
|
|
properties={},
|
|
|
|
type="mock_type",
|
|
|
|
),
|
2019-12-08 08:26:31 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.ABORT
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("reason") == "already_configured"
|
|
|
|
|
|
|
|
entries = hass.config_entries.async_entries(DOMAIN)
|
|
|
|
assert entries[0].data[CONF_HOST] == "127.0.0.1"
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2022-01-14 16:16:59 +00:00
|
|
|
# Check the host updates on discovery
|
2020-01-04 21:45:11 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-02-13 22:50:25 +00:00
|
|
|
DOMAIN,
|
|
|
|
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
2021-11-23 15:32:58 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("127.0.0.2"),
|
|
|
|
ip_addresses=[ip_address("127.0.0.2")],
|
2021-11-23 15:32:58 +00:00
|
|
|
hostname="mock_hostname",
|
|
|
|
name="mock_name",
|
|
|
|
port=9123,
|
|
|
|
properties={},
|
|
|
|
type="mock_type",
|
|
|
|
),
|
2019-12-08 08:26:31 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.ABORT
|
2022-01-14 16:16:59 +00:00
|
|
|
assert result.get("reason") == "already_configured"
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2021-02-13 22:50:25 +00:00
|
|
|
entries = hass.config_entries.async_entries(DOMAIN)
|
|
|
|
assert entries[0].data[CONF_HOST] == "127.0.0.2"
|
2022-06-22 20:37:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_during_onboarding(
|
|
|
|
hass: HomeAssistant,
|
2023-02-19 19:14:18 +00:00
|
|
|
mock_elgato: MagicMock,
|
2022-06-22 20:37:25 +00:00
|
|
|
mock_setup_entry: AsyncMock,
|
|
|
|
mock_onboarding: MagicMock,
|
2023-02-17 21:35:55 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2022-06-22 20:37:25 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the zeroconf creates an entry during onboarding."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_ZEROCONF},
|
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("127.0.0.1"),
|
|
|
|
ip_addresses=[ip_address("127.0.0.1")],
|
2022-06-22 20:37:25 +00:00
|
|
|
hostname="example.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=9123,
|
|
|
|
properties={"id": "AA:BB:CC:DD:EE:FF"},
|
|
|
|
type="mock_type",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result.get("type") == FlowResultType.CREATE_ENTRY
|
2023-02-17 21:35:55 +00:00
|
|
|
assert result == snapshot
|
2022-06-22 20:37:25 +00:00
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2023-02-19 19:14:18 +00:00
|
|
|
assert len(mock_elgato.info.mock_calls) == 1
|
2022-06-22 20:37:25 +00:00
|
|
|
assert len(mock_onboarding.mock_calls) == 1
|