Axis - config flow use new helper functions (#31286)

* Make use of new config flow helpers
Simplify Axis entry config to work with config flow helpers

* Keep old device data for rollback purposes
This commit is contained in:
Robert Svensson authored and GitHub committed 2020-01-30 22:20:30 +01:00
1 parent cd1aa46404
commit 56657fa859
8 files changed
+183 -130

No files matched your search

+34 -29
View File
@@ -4,7 +4,7 @@ from unittest.mock import Mock, patch
from homeassistant.components import axis
from homeassistant.components.axis import config_flow
from .test_device import MAC, setup_axis_integration
from .test_device import MAC, MODEL, NAME, setup_axis_integration
from tests.common import MockConfigEntry, mock_coro
@@ -54,12 +54,10 @@ async def test_flow_manual_configuration(hass):
assert result["type"] == "create_entry"
assert result["title"] == f"prodnbr - {MAC}"
assert result["data"] == {
axis.CONF_DEVICE: {
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
},
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
config_flow.CONF_MAC: MAC,
config_flow.CONF_MODEL: "prodnbr",
config_flow.CONF_NAME: "prodnbr 0",
@@ -95,11 +93,8 @@ async def test_manual_configuration_update_configuration(hass):
)
assert result["type"] == "abort"
assert result["reason"] == "updated_configuration"
assert (
device.config_entry.data[config_flow.CONF_DEVICE][config_flow.CONF_HOST]
== "2.3.4.5"
)
assert result["reason"] == "already_configured"
assert device.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5"
async def test_flow_fails_already_configured(hass):
@@ -223,12 +218,10 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass):
assert result["type"] == "create_entry"
assert result["title"] == f"prodnbr - {MAC}"
assert result["data"] == {
axis.CONF_DEVICE: {
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
},
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
config_flow.CONF_MAC: MAC,
config_flow.CONF_MODEL: "prodnbr",
config_flow.CONF_NAME: "prodnbr 2",
@@ -271,12 +264,10 @@ async def test_zeroconf_flow(hass):
assert result["type"] == "create_entry"
assert result["title"] == f"prodnbr - {MAC}"
assert result["data"] == {
axis.CONF_DEVICE: {
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
},
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_USERNAME: "user",
config_flow.CONF_PASSWORD: "pass",
config_flow.CONF_PORT: 80,
config_flow.CONF_MAC: MAC,
config_flow.CONF_MODEL: "prodnbr",
config_flow.CONF_NAME: "prodnbr 0",
@@ -310,6 +301,15 @@ async def test_zeroconf_flow_updated_configuration(hass):
"""Test that zeroconf update configuration with new parameters."""
device = await setup_axis_integration(hass)
assert device.host == "1.2.3.4"
assert device.config_entry.data == {
config_flow.CONF_HOST: "1.2.3.4",
config_flow.CONF_PORT: 80,
config_flow.CONF_USERNAME: "username",
config_flow.CONF_PASSWORD: "password",
config_flow.CONF_MAC: MAC,
config_flow.CONF_MODEL: MODEL,
config_flow.CONF_NAME: NAME,
}
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
@@ -323,11 +323,16 @@ async def test_zeroconf_flow_updated_configuration(hass):
)
assert result["type"] == "abort"
assert result["reason"] == "updated_configuration"
assert device.host == "2.3.4.5"
assert (
device.config_entry.data[config_flow.CONF_DEVICE][config_flow.CONF_PORT] == 8080
)
assert result["reason"] == "already_configured"
assert device.config_entry.data == {
config_flow.CONF_HOST: "2.3.4.5",
config_flow.CONF_PORT: 8080,
config_flow.CONF_USERNAME: "username",
config_flow.CONF_PASSWORD: "password",
config_flow.CONF_MAC: MAC,
config_flow.CONF_MODEL: MODEL,
config_flow.CONF_NAME: NAME,
}
async def test_zeroconf_flow_ignore_non_axis_device(hass):
+12 -15
View File
@@ -14,18 +14,14 @@ MAC = "00408C12345"
MODEL = "model"
NAME = "name"
DEVICE_DATA = {
axis.device.CONF_HOST: "1.2.3.4",
axis.device.CONF_USERNAME: "username",
axis.device.CONF_PASSWORD: "password",
axis.device.CONF_PORT: 80,
}
ENTRY_OPTIONS = {axis.device.CONF_CAMERA: True, axis.device.CONF_EVENTS: True}
ENTRY_OPTIONS = {axis.CONF_CAMERA: True, axis.CONF_EVENTS: True}
ENTRY_CONFIG = {
axis.device.CONF_DEVICE: DEVICE_DATA,
axis.device.CONF_MAC: MAC,
axis.CONF_HOST: "1.2.3.4",
axis.CONF_USERNAME: "username",
axis.CONF_PASSWORD: "password",
axis.CONF_PORT: 80,
axis.CONF_MAC: MAC,
axis.device.CONF_MODEL: MODEL,
axis.device.CONF_NAME: NAME,
}
@@ -76,6 +72,7 @@ async def setup_axis_integration(
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
options=deepcopy(options),
entry_id="1",
version=2,
)
config_entry.add_to_hass(hass)
@@ -116,10 +113,10 @@ async def test_device_setup(hass):
assert forward_entry_setup.mock_calls[1][1] == (entry, "binary_sensor")
assert forward_entry_setup.mock_calls[2][1] == (entry, "switch")
assert device.host == DEVICE_DATA[axis.device.CONF_HOST]
assert device.host == ENTRY_CONFIG[axis.CONF_HOST]
assert device.model == ENTRY_CONFIG[axis.device.CONF_MODEL]
assert device.name == ENTRY_CONFIG[axis.device.CONF_NAME]
assert device.serial == ENTRY_CONFIG[axis.device.CONF_MAC]
assert device.serial == ENTRY_CONFIG[axis.CONF_MAC]
async def test_update_address(hass):
@@ -204,7 +201,7 @@ async def test_get_device_fails(hass):
with patch(
"axis.param_cgi.Params.update_brand", side_effect=axislib.Unauthorized
), pytest.raises(axis.errors.AuthenticationRequired):
await axis.device.get_device(hass, DEVICE_DATA)
await axis.device.get_device(hass, host="", port="", username="", password="")
async def test_get_device_device_unavailable(hass):
@@ -212,7 +209,7 @@ async def test_get_device_device_unavailable(hass):
with patch(
"axis.param_cgi.Params.update_brand", side_effect=axislib.RequestError
), pytest.raises(axis.errors.CannotConnect):
await axis.device.get_device(hass, DEVICE_DATA)
await axis.device.get_device(hass, host="", port="", username="", password="")
async def test_get_device_unknown_error(hass):
@@ -220,4 +217,4 @@ async def test_get_device_unknown_error(hass):
with patch(
"axis.param_cgi.Params.update_brand", side_effect=axislib.AxisException
), pytest.raises(axis.errors.AuthenticationRequired):
await axis.device.get_device(hass, DEVICE_DATA)
await axis.device.get_device(hass, host="", port="", username="", password="")
+42 -4
View File
@@ -4,7 +4,7 @@ from unittest.mock import Mock, patch
from homeassistant.components import axis
from homeassistant.setup import async_setup_component
from .test_device import MAC, setup_axis_integration
from .test_device import ENTRY_CONFIG, MAC, setup_axis_integration
from tests.common import MockConfigEntry, mock_coro
@@ -16,7 +16,7 @@ async def test_setup_device_already_configured(hass):
assert await async_setup_component(
hass,
axis.DOMAIN,
{axis.DOMAIN: {"device_name": {axis.config_flow.CONF_HOST: "1.2.3.4"}}},
{axis.DOMAIN: {"device_name": {axis.CONF_HOST: "1.2.3.4"}}},
)
assert not mock_config_entries.flow.mock_calls
@@ -38,7 +38,7 @@ async def test_setup_entry(hass):
async def test_setup_entry_fails(hass):
"""Test successful setup of entry."""
entry = MockConfigEntry(
domain=axis.DOMAIN, data={axis.device.CONF_MAC: "0123"}, options=True
domain=axis.DOMAIN, data={axis.CONF_MAC: "0123"}, options=True
)
mock_device = Mock()
@@ -63,7 +63,7 @@ async def test_unload_entry(hass):
async def test_populate_options(hass):
"""Test successful populate options."""
entry = MockConfigEntry(domain=axis.DOMAIN, data={"device": {}})
entry = MockConfigEntry(domain=axis.DOMAIN, data=ENTRY_CONFIG)
entry.add_to_hass(hass)
with patch.object(axis, "get_device", return_value=mock_coro(Mock())):
@@ -75,3 +75,41 @@ async def test_populate_options(hass):
axis.CONF_EVENTS: True,
axis.CONF_TRIGGER_TIME: axis.DEFAULT_TRIGGER_TIME,
}
async def test_migrate_entry(hass):
"""Test successful migration of entry data."""
legacy_config = {
axis.CONF_DEVICE: {
axis.CONF_HOST: "1.2.3.4",
axis.CONF_USERNAME: "username",
axis.CONF_PASSWORD: "password",
axis.CONF_PORT: 80,
},
axis.CONF_MAC: "mac",
axis.device.CONF_MODEL: "model",
axis.device.CONF_NAME: "name",
}
entry = MockConfigEntry(domain=axis.DOMAIN, data=legacy_config)
assert entry.data == legacy_config
assert entry.version == 1
await axis.async_migrate_entry(hass, entry)
assert entry.data == {
axis.CONF_DEVICE: {
axis.CONF_HOST: "1.2.3.4",
axis.CONF_USERNAME: "username",
axis.CONF_PASSWORD: "password",
axis.CONF_PORT: 80,
},
axis.CONF_HOST: "1.2.3.4",
axis.CONF_USERNAME: "username",
axis.CONF_PASSWORD: "password",
axis.CONF_PORT: 80,
axis.CONF_MAC: "mac",
axis.device.CONF_MODEL: "model",
axis.device.CONF_NAME: "name",
}
assert entry.version == 2