2019-11-06 22:55:39 +00:00
|
|
|
"""Tests for the WLED config flow."""
|
2021-06-11 09:36:54 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-06-03 00:29:49 +00:00
|
|
|
from wled import WLEDConnectionError
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-11-22 14:19:54 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2021-06-12 11:33:23 +00:00
|
|
|
from homeassistant.components.wled.const import CONF_KEEP_MASTER_LIGHT, DOMAIN
|
2019-11-06 22:55:39 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
2021-06-11 09:36:54 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
|
2019-11-06 22:55:39 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-15 20:14:56 +00:00
|
|
|
from homeassistant.data_entry_flow import (
|
|
|
|
RESULT_TYPE_ABORT,
|
|
|
|
RESULT_TYPE_CREATE_ENTRY,
|
|
|
|
RESULT_TYPE_FORM,
|
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
async def test_full_user_flow_implementation(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: None
|
2021-05-15 20:14:56 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the full manual user flow from start to finish."""
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-08-27 11:56:20 +00:00
|
|
|
context={"source": SOURCE_USER},
|
2020-01-04 21:48:31 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("step_id") == "user"
|
|
|
|
assert result.get("type") == RESULT_TYPE_FORM
|
|
|
|
assert "flow_id" in result
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_HOST: "192.168.1.123"}
|
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("title") == "192.168.1.123"
|
|
|
|
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert "data" in result
|
|
|
|
assert result["data"][CONF_HOST] == "192.168.1.123"
|
|
|
|
assert result["data"][CONF_MAC] == "aabbccddeeff"
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
async def test_full_zeroconf_flow_implementation(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: None
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
2021-05-15 20:14:56 +00:00
|
|
|
"""Test the full manual user flow from start to finish."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-22 14:19:54 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.1.123", hostname="example.local.", properties={}
|
|
|
|
),
|
2021-05-15 20:14:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
|
|
|
|
assert result.get("description_placeholders") == {CONF_NAME: "example"}
|
|
|
|
assert result.get("step_id") == "zeroconf_confirm"
|
|
|
|
assert result.get("type") == RESULT_TYPE_FORM
|
|
|
|
assert "flow_id" in result
|
|
|
|
|
|
|
|
flow = flows[0]
|
|
|
|
assert "context" in flow
|
|
|
|
assert flow["context"][CONF_HOST] == "192.168.1.123"
|
|
|
|
assert flow["context"][CONF_NAME] == "example"
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
2020-03-18 10:10:40 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result2.get("title") == "example"
|
|
|
|
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
assert "data" in result2
|
|
|
|
assert result2["data"][CONF_HOST] == "192.168.1.123"
|
|
|
|
assert result2["data"][CONF_MAC] == "aabbccddeeff"
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_connection_error(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant, mock_wled_config_flow: MagicMock
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we show user form on WLED connection error."""
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_wled_config_flow.update.side_effect = WLEDConnectionError
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:48:31 +00:00
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: "example.com"},
|
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_FORM
|
|
|
|
assert result.get("step_id") == "user"
|
|
|
|
assert result.get("errors") == {"base": "cannot_connect"}
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_connection_error(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant, mock_wled_config_flow: MagicMock
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow on WLED connection error."""
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_wled_config_flow.update.side_effect = WLEDConnectionError
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:48:31 +00:00
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-22 14:19:54 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.1.123", hostname="example.local.", properties={}
|
|
|
|
),
|
2020-01-04 21:48:31 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_ABORT
|
|
|
|
assert result.get("reason") == "cannot_connect"
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_confirm_connection_error(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant, mock_wled_config_flow: MagicMock
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow on WLED connection error."""
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_wled_config_flow.update.side_effect = WLEDConnectionError
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:48:31 +00:00
|
|
|
context={
|
|
|
|
"source": SOURCE_ZEROCONF,
|
|
|
|
CONF_HOST: "example.com",
|
|
|
|
CONF_NAME: "test",
|
|
|
|
},
|
2021-11-22 14:19:54 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.1.123", hostname="example.com.", properties={}
|
|
|
|
),
|
2019-11-06 22:55:39 +00:00
|
|
|
)
|
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_ABORT
|
|
|
|
assert result.get("reason") == "cannot_connect"
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_user_device_exists_abort(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
init_integration: MagicMock,
|
|
|
|
mock_wled_config_flow: MagicMock,
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow if WLED device already configured."""
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:48:31 +00:00
|
|
|
context={"source": SOURCE_USER},
|
2020-04-04 05:41:08 +00:00
|
|
|
data={CONF_HOST: "192.168.1.123"},
|
2020-01-04 21:48:31 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_ABORT
|
|
|
|
assert result.get("reason") == "already_configured"
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_device_exists_abort(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
init_integration: MagicMock,
|
|
|
|
mock_wled_config_flow: MagicMock,
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow if WLED device already configured."""
|
2020-01-04 21:48:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-01-04 21:48:31 +00:00
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-22 14:19:54 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.1.123", hostname="example.local.", properties={}
|
|
|
|
),
|
2020-03-18 10:10:40 +00:00
|
|
|
)
|
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_ABORT
|
|
|
|
assert result.get("reason") == "already_configured"
|
2020-03-18 10:10:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_with_mac_device_exists_abort(
|
2021-06-11 09:36:54 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
init_integration: MockConfigEntry,
|
|
|
|
mock_wled_config_flow: MagicMock,
|
2020-03-18 10:10:40 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test we abort zeroconf flow if WLED device already configured."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-05-15 20:14:56 +00:00
|
|
|
DOMAIN,
|
2020-03-18 10:10:40 +00:00
|
|
|
context={"source": SOURCE_ZEROCONF},
|
2021-11-22 14:19:54 +00:00
|
|
|
data=zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.1.123",
|
|
|
|
hostname="example.local.",
|
|
|
|
properties={CONF_MAC: "aabbccddeeff"},
|
|
|
|
),
|
2020-01-04 21:48:31 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-15 20:14:56 +00:00
|
|
|
assert result.get("type") == RESULT_TYPE_ABORT
|
|
|
|
assert result.get("reason") == "already_configured"
|
2021-06-12 11:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_options_flow(
|
|
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Test options config flow."""
|
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
|
|
|
|
|
|
|
|
assert result.get("type") == RESULT_TYPE_FORM
|
|
|
|
assert result.get("step_id") == "init"
|
|
|
|
assert "flow_id" in result
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_KEEP_MASTER_LIGHT: True},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result2.get("data") == {
|
|
|
|
CONF_KEEP_MASTER_LIGHT: True,
|
|
|
|
}
|