Improve imports in deCONZ tests (#41976)

* Improve imports in deconz tests

* Add spacing
This commit is contained in:
Robert Svensson 2020-10-17 18:20:06 +02:00 committed by GitHub
parent 1b94ef69d8
commit 7eb532ddd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 547 additions and 398 deletions

View file

@ -1,11 +1,9 @@
"""Tests for deCONZ config flow."""
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_MANUAL_INPUT,
CONF_SERIAL,
@ -18,7 +16,18 @@ from homeassistant.components.deconz.const import (
CONF_MASTER_GATEWAY,
DOMAIN,
)
from homeassistant.components.ssdp import (
ATTR_SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER_URL,
ATTR_UPNP_SERIAL,
)
from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from .test_gateway import API_KEY, BRIDGEID, setup_deconz_integration
@ -39,17 +48,17 @@ async def test_flow_discovered_bridges(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "1.2.3.4"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -62,7 +71,7 @@ async def test_flow_discovered_bridges(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == BRIDGEID
assert result["data"] == {
CONF_HOST: "1.2.3.4",
@ -80,14 +89,14 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: CONF_MANUAL_INPUT}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
result = await hass.config_entries.flow.async_configure(
@ -95,7 +104,7 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock):
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -114,7 +123,7 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == BRIDGEID
assert result["data"] == {
CONF_HOST: "1.2.3.4",
@ -132,10 +141,10 @@ async def test_flow_manual_configuration(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
result = await hass.config_entries.flow.async_configure(
@ -143,7 +152,7 @@ async def test_flow_manual_configuration(hass, aioclient_mock):
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -162,7 +171,7 @@ async def test_flow_manual_configuration(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == BRIDGEID
assert result["data"] == {
CONF_HOST: "1.2.3.4",
@ -176,23 +185,23 @@ 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(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
assert not hass.config_entries.flow._progress[result["flow_id"]].bridges
async def test_manual_configuration_after_discovery_ResponseError(hass, aioclient_mock):
"""Test failed discovery fallbacks to manual configuration."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=config_flow.ResponseError)
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=pydeconz.errors.ResponseError)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
assert not hass.config_entries.flow._progress[result["flow_id"]].bridges
@ -208,10 +217,10 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
result = await hass.config_entries.flow.async_configure(
@ -219,7 +228,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock):
user_input={CONF_HOST: "2.3.4.5", CONF_PORT: 80},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -238,7 +247,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "2.3.4.5"
@ -254,10 +263,10 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
result = await hass.config_entries.flow.async_configure(
@ -265,7 +274,7 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -284,7 +293,7 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
@ -297,10 +306,10 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "manual_input"
result = await hass.config_entries.flow.async_configure(
@ -308,7 +317,7 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock):
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -325,7 +334,7 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "no_bridges"
@ -338,14 +347,14 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock):
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "1.2.3.4"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post("http://1.2.3.4:80/api", exc=pydeconz.errors.ResponseError)
@ -354,7 +363,7 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
assert result["errors"] == {"base": "no_key"}
@ -364,14 +373,14 @@ async def test_flow_ssdp_discovery(hass, aioclient_mock):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={
ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ATTR_UPNP_SERIAL: BRIDGEID,
},
context={"source": "ssdp"},
context={"source": SOURCE_SSDP},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -384,7 +393,7 @@ async def test_flow_ssdp_discovery(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == BRIDGEID
assert result["data"] == {
CONF_HOST: "1.2.3.4",
@ -398,14 +407,14 @@ async def test_flow_ssdp_discovery_bad_bridge_id_aborts(hass, aioclient_mock):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={
ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ssdp.ATTR_UPNP_SERIAL: BAD_BRIDGEID,
ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ATTR_UPNP_SERIAL: BAD_BRIDGEID,
},
context={"source": "ssdp"},
context={"source": SOURCE_SSDP},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "link"
aioclient_mock.post(
@ -418,7 +427,7 @@ async def test_flow_ssdp_discovery_bad_bridge_id_aborts(hass, aioclient_mock):
result["flow_id"], user_input={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "no_hardware_available"
@ -426,11 +435,11 @@ 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(
DOMAIN,
data={ssdp.ATTR_UPNP_MANUFACTURER_URL: "not deconz bridge"},
context={"source": "ssdp"},
data={ATTR_UPNP_MANUFACTURER_URL: "not deconz bridge"},
context={"source": SOURCE_SSDP},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "not_deconz_bridge"
@ -445,15 +454,15 @@ async def test_ssdp_discovery_update_configuration(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={
ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/",
ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
ATTR_SSDP_LOCATION: "http://2.3.4.5:80/",
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ATTR_UPNP_SERIAL: BRIDGEID,
},
context={"source": "ssdp"},
context={"source": SOURCE_SSDP},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "2.3.4.5"
assert len(mock_setup_entry.mock_calls) == 1
@ -466,33 +475,33 @@ async def test_ssdp_discovery_dont_update_configuration(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={
ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ATTR_UPNP_SERIAL: BRIDGEID,
},
context={"source": "ssdp"},
context={"source": SOURCE_SSDP},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "1.2.3.4"
async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass):
"""Test to ensure the SSDP discovery does not update an Hass.io entry."""
config_entry = await setup_deconz_integration(hass, source="hassio")
config_entry = await setup_deconz_integration(hass, source=SOURCE_HASSIO)
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={
ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
ATTR_SSDP_LOCATION: "http://1.2.3.4:80/",
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
ATTR_UPNP_SERIAL: BRIDGEID,
},
context={"source": "ssdp"},
context={"source": SOURCE_SSDP},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "1.2.3.4"
@ -508,9 +517,9 @@ async def test_flow_hassio_discovery(hass):
CONF_SERIAL: BRIDGEID,
CONF_API_KEY: API_KEY,
},
context={"source": "hassio"},
context={"source": SOURCE_HASSIO},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "hassio_confirm"
assert result["description_placeholders"] == {"addon": "Mock Addon"}
@ -525,7 +534,7 @@ async def test_flow_hassio_discovery(hass):
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["result"].data == {
CONF_HOST: "mock-deconz",
CONF_PORT: 80,
@ -551,11 +560,11 @@ async def test_hassio_discovery_update_configuration(hass):
CONF_API_KEY: "updated",
CONF_SERIAL: BRIDGEID,
},
context={"source": "hassio"},
context={"source": SOURCE_HASSIO},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "2.3.4.5"
assert config_entry.data[CONF_PORT] == 8080
@ -575,10 +584,10 @@ async def test_hassio_discovery_dont_update_configuration(hass):
CONF_API_KEY: API_KEY,
CONF_SERIAL: BRIDGEID,
},
context={"source": "hassio"},
context={"source": SOURCE_HASSIO},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
@ -588,7 +597,7 @@ async def test_option_flow(hass):
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "deconz_devices"
result = await hass.config_entries.options.async_configure(
@ -600,7 +609,7 @@ async def test_option_flow(hass):
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
CONF_ALLOW_CLIP_SENSOR: False,
CONF_ALLOW_DECONZ_GROUPS: False,