diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index d79f80b96b0..4873528d982 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -3,13 +3,23 @@ import asyncio import pydeconz +from homeassistant import data_entry_flow from homeassistant.components import ssdp from homeassistant.components.deconz import config_flow +from homeassistant.components.deconz.config_flow import ( + CONF_SERIAL, + DECONZ_MANUFACTURERURL, +) +from homeassistant.components.deconz.const import ( + CONF_ALLOW_CLIP_SENSOR, + CONF_ALLOW_DECONZ_GROUPS, + CONF_MASTER_GATEWAY, + DOMAIN, +) +from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT from .test_gateway import API_KEY, BRIDGEID, setup_deconz_integration -from tests.common import MockConfigEntry - async def test_flow_1_discovered_bridge(hass, aioclient_mock): """Test that config flow for one discovered bridge works.""" @@ -20,10 +30,10 @@ async def test_flow_1_discovered_bridge(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -36,12 +46,12 @@ async def test_flow_1_discovered_bridge(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "create_entry" + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { - config_flow.CONF_HOST: "1.2.3.4", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "1.2.3.4", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, } @@ -57,17 +67,17 @@ async def test_flow_2_discovered_bridges(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={config_flow.CONF_HOST: "1.2.3.4"} + result["flow_id"], user_input={CONF_HOST: "1.2.3.4"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -80,12 +90,12 @@ async def test_flow_2_discovered_bridges(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "create_entry" + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { - config_flow.CONF_HOST: "1.2.3.4", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "1.2.3.4", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, } @@ -98,18 +108,17 @@ async def test_flow_manual_configuration(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={config_flow.CONF_HOST: "1.2.3.4", config_flow.CONF_PORT: 80}, + result["flow_id"], user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -128,12 +137,12 @@ async def test_flow_manual_configuration(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "create_entry" + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { - config_flow.CONF_HOST: "1.2.3.4", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "1.2.3.4", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, } @@ -142,10 +151,10 @@ async def test_manual_configuration_after_discovery_timeout(hass, aioclient_mock aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=asyncio.TimeoutError) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" assert not hass.config_entries.flow._progress[result["flow_id"]].bridges @@ -155,10 +164,10 @@ async def test_manual_configuration_after_discovery_ResponseError(hass, aioclien aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=config_flow.ResponseError) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" assert not hass.config_entries.flow._progress[result["flow_id"]].bridges @@ -174,18 +183,17 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={config_flow.CONF_HOST: "2.3.4.5", config_flow.CONF_PORT: 80}, + result["flow_id"], user_input={CONF_HOST: "2.3.4.5", CONF_PORT: 80}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -204,9 +212,9 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" + assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" async def test_manual_configuration_dont_update_configuration(hass, aioclient_mock): @@ -220,18 +228,17 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={config_flow.CONF_HOST: "1.2.3.4", config_flow.CONF_PORT: 80}, + result["flow_id"], user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -250,7 +257,7 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo result["flow_id"], user_input={} ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" @@ -263,18 +270,17 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={config_flow.CONF_HOST: "1.2.3.4", config_flow.CONF_PORT: 80}, + result["flow_id"], user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -291,7 +297,7 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "no_bridges" @@ -304,10 +310,10 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, context={"source": "user"} + DOMAIN, context={"source": "user"} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post("http://1.2.3.4:80/api", exc=pydeconz.errors.ResponseError) @@ -316,7 +322,7 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" assert result["errors"] == {"base": "no_key"} @@ -324,16 +330,16 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): async def test_flow_ssdp_discovery(hass, aioclient_mock): """Test that config flow for one discovered bridge works.""" result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: config_flow.DECONZ_MANUFACTURERURL, + ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, ssdp.ATTR_UPNP_SERIAL: BRIDGEID, }, context={"source": "ssdp"}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -346,24 +352,24 @@ async def test_flow_ssdp_discovery(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == "create_entry" + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { - config_flow.CONF_HOST: "1.2.3.4", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "1.2.3.4", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, } async def test_ssdp_discovery_not_deconz_bridge(hass): """Test a non deconz bridge being discovered over ssdp.""" result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ssdp.ATTR_UPNP_MANUFACTURER_URL: "not deconz bridge"}, context={"source": "ssdp"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "not_deconz_bridge" @@ -372,18 +378,18 @@ async def test_ssdp_discovery_update_configuration(hass): gateway = await setup_deconz_integration(hass) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: config_flow.DECONZ_MANUFACTURERURL, + ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, ssdp.ATTR_UPNP_SERIAL: BRIDGEID, }, context={"source": "ssdp"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" + assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" async def test_ssdp_discovery_dont_update_configuration(hass): @@ -391,18 +397,18 @@ async def test_ssdp_discovery_dont_update_configuration(hass): gateway = await setup_deconz_integration(hass) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: config_flow.DECONZ_MANUFACTURERURL, + ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, ssdp.ATTR_UPNP_SERIAL: BRIDGEID, }, context={"source": "ssdp"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[config_flow.CONF_HOST] == "1.2.3.4" + assert gateway.config_entry.data[CONF_HOST] == "1.2.3.4" async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass): @@ -410,34 +416,34 @@ async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass): gateway = await setup_deconz_integration(hass, source="hassio") result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: config_flow.DECONZ_MANUFACTURERURL, + ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, ssdp.ATTR_UPNP_SERIAL: BRIDGEID, }, context={"source": "ssdp"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[config_flow.CONF_HOST] == "1.2.3.4" + assert gateway.config_entry.data[CONF_HOST] == "1.2.3.4" async def test_flow_hassio_discovery(hass): """Test hassio discovery flow works.""" result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ "addon": "Mock Addon", - config_flow.CONF_HOST: "mock-deconz", - config_flow.CONF_PORT: 80, - config_flow.CONF_SERIAL: BRIDGEID, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "mock-deconz", + CONF_PORT: 80, + CONF_SERIAL: BRIDGEID, + CONF_API_KEY: API_KEY, }, context={"source": "hassio"}, ) - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "hassio_confirm" assert result["description_placeholders"] == {"addon": "Mock Addon"} @@ -445,11 +451,11 @@ async def test_flow_hassio_discovery(hass): result["flow_id"], user_input={} ) - assert result["type"] == "create_entry" + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["result"].data == { - config_flow.CONF_HOST: "mock-deconz", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, + CONF_HOST: "mock-deconz", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, } @@ -458,21 +464,21 @@ async def test_hassio_discovery_update_configuration(hass): gateway = await setup_deconz_integration(hass) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ - config_flow.CONF_HOST: "2.3.4.5", - config_flow.CONF_PORT: 8080, - config_flow.CONF_API_KEY: "updated", - config_flow.CONF_SERIAL: BRIDGEID, + CONF_HOST: "2.3.4.5", + CONF_PORT: 8080, + CONF_API_KEY: "updated", + CONF_SERIAL: BRIDGEID, }, context={"source": "hassio"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" - assert gateway.config_entry.data[config_flow.CONF_PORT] == 8080 - assert gateway.config_entry.data[config_flow.CONF_API_KEY] == "updated" + assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" + assert gateway.config_entry.data[CONF_PORT] == 8080 + assert gateway.config_entry.data[CONF_API_KEY] == "updated" async def test_hassio_discovery_dont_update_configuration(hass): @@ -480,41 +486,37 @@ async def test_hassio_discovery_dont_update_configuration(hass): await setup_deconz_integration(hass) result = await hass.config_entries.flow.async_init( - config_flow.DOMAIN, + DOMAIN, data={ - config_flow.CONF_HOST: "1.2.3.4", - config_flow.CONF_PORT: 80, - config_flow.CONF_API_KEY: API_KEY, - config_flow.CONF_SERIAL: BRIDGEID, + CONF_HOST: "1.2.3.4", + CONF_PORT: 80, + CONF_API_KEY: API_KEY, + CONF_SERIAL: BRIDGEID, }, context={"source": "hassio"}, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" async def test_option_flow(hass): """Test config flow options.""" - entry = MockConfigEntry(domain=config_flow.DOMAIN, data={}, options=None) - hass.config_entries._entries.append(entry) + gateway = await setup_deconz_integration(hass) - flow = await hass.config_entries.options.async_create_flow( - entry.entry_id, context={"source": "test"}, data=None - ) + result = await hass.config_entries.options.async_init(gateway.config_entry.entry_id) - result = await flow.async_step_init() - assert result["type"] == "form" + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "deconz_devices" - result = await flow.async_step_deconz_devices( - user_input={ - config_flow.CONF_ALLOW_CLIP_SENSOR: False, - config_flow.CONF_ALLOW_DECONZ_GROUPS: False, - } + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={CONF_ALLOW_CLIP_SENSOR: False, CONF_ALLOW_DECONZ_GROUPS: False}, ) - assert result["type"] == "create_entry" + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["data"] == { - config_flow.CONF_ALLOW_CLIP_SENSOR: False, - config_flow.CONF_ALLOW_DECONZ_GROUPS: False, + CONF_ALLOW_CLIP_SENSOR: False, + CONF_ALLOW_DECONZ_GROUPS: False, + CONF_MASTER_GATEWAY: True, }