2019-03-24 15:16:50 +00:00
|
|
|
"""Test Axis component setup process."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant.components import axis
|
2022-03-29 09:11:58 +00:00
|
|
|
from homeassistant.components.axis.const import DOMAIN as AXIS_DOMAIN
|
2021-01-16 00:01:14 +00:00
|
|
|
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
2020-05-14 08:49:27 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DEVICE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MAC,
|
2022-03-29 09:11:58 +00:00
|
|
|
CONF_MODEL,
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2021-03-09 13:24:34 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2021-01-16 00:01:14 +00:00
|
|
|
from homeassistant.helpers.device_registry import format_mac
|
2019-12-08 14:44:04 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-01-31 19:23:51 +00:00
|
|
|
from .test_device import MAC, setup_axis_integration
|
2020-01-04 07:58:18 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_no_config(hass):
|
|
|
|
"""Test setup without configuration."""
|
2020-05-14 08:49:27 +00:00
|
|
|
assert await async_setup_component(hass, AXIS_DOMAIN, {})
|
|
|
|
assert AXIS_DOMAIN not in hass.data
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry(hass):
|
|
|
|
"""Test successful setup of entry."""
|
2020-01-04 07:58:18 +00:00
|
|
|
await setup_axis_integration(hass)
|
2020-05-14 08:49:27 +00:00
|
|
|
assert len(hass.data[AXIS_DOMAIN]) == 1
|
2021-01-16 00:01:14 +00:00
|
|
|
assert format_mac(MAC) in hass.data[AXIS_DOMAIN]
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_fails(hass):
|
|
|
|
"""Test successful setup of entry."""
|
2020-01-31 19:23:51 +00:00
|
|
|
config_entry = MockConfigEntry(
|
2021-01-16 00:01:14 +00:00
|
|
|
domain=AXIS_DOMAIN, data={CONF_MAC: "0123"}, version=3
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-31 19:23:51 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
mock_device = Mock()
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_device.async_setup = AsyncMock(return_value=False)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(axis, "AxisNetworkDevice") as mock_device_class:
|
2019-03-24 15:16:50 +00:00
|
|
|
mock_device_class.return_value = mock_device
|
|
|
|
|
2020-01-31 19:23:51 +00:00
|
|
|
assert not await hass.config_entries.async_setup(config_entry.entry_id)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2020-05-14 08:49:27 +00:00
|
|
|
assert not hass.data[AXIS_DOMAIN]
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
|
2019-04-15 22:06:45 +00:00
|
|
|
async def test_unload_entry(hass):
|
|
|
|
"""Test successful unload of entry."""
|
2020-10-22 07:29:53 +00:00
|
|
|
config_entry = await setup_axis_integration(hass)
|
|
|
|
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
2020-05-14 08:49:27 +00:00
|
|
|
assert hass.data[AXIS_DOMAIN]
|
2019-04-15 22:06:45 +00:00
|
|
|
|
2020-01-31 19:23:51 +00:00
|
|
|
assert await hass.config_entries.async_unload(device.config_entry.entry_id)
|
2020-05-14 08:49:27 +00:00
|
|
|
assert not hass.data[AXIS_DOMAIN]
|
2020-01-30 21:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_migrate_entry(hass):
|
|
|
|
"""Test successful migration of entry data."""
|
|
|
|
legacy_config = {
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_DEVICE: {
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 80,
|
2020-01-30 21:20:30 +00:00
|
|
|
},
|
2021-01-16 00:01:14 +00:00
|
|
|
CONF_MAC: "00408C123456",
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_MODEL: "model",
|
|
|
|
CONF_NAME: "name",
|
2020-01-30 21:20:30 +00:00
|
|
|
}
|
2020-05-14 08:49:27 +00:00
|
|
|
entry = MockConfigEntry(domain=AXIS_DOMAIN, data=legacy_config)
|
2020-01-30 21:20:30 +00:00
|
|
|
|
|
|
|
assert entry.data == legacy_config
|
|
|
|
assert entry.version == 1
|
2021-01-16 00:01:14 +00:00
|
|
|
assert not entry.unique_id
|
|
|
|
|
|
|
|
# Create entity entry to migrate to new unique ID
|
2021-03-09 13:24:34 +00:00
|
|
|
registry = er.async_get(hass)
|
2021-01-16 00:01:14 +00:00
|
|
|
registry.async_get_or_create(
|
|
|
|
BINARY_SENSOR_DOMAIN,
|
|
|
|
AXIS_DOMAIN,
|
|
|
|
"00408C123456-vmd4-0",
|
|
|
|
suggested_object_id="vmd4",
|
|
|
|
config_entry=entry,
|
|
|
|
)
|
2020-01-30 21:20:30 +00:00
|
|
|
|
2020-01-31 19:23:51 +00:00
|
|
|
await entry.async_migrate(hass)
|
2020-01-30 21:20:30 +00:00
|
|
|
|
|
|
|
assert entry.data == {
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_DEVICE: {
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 80,
|
2020-01-30 21:20:30 +00:00
|
|
|
},
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 80,
|
2021-01-16 00:01:14 +00:00
|
|
|
CONF_MAC: "00408C123456",
|
2020-05-14 08:49:27 +00:00
|
|
|
CONF_MODEL: "model",
|
|
|
|
CONF_NAME: "name",
|
2020-01-30 21:20:30 +00:00
|
|
|
}
|
2021-02-06 12:17:52 +00:00
|
|
|
assert entry.version == 2 # Keep version to support rollbacking
|
2021-01-16 00:01:14 +00:00
|
|
|
assert entry.unique_id == "00:40:8c:12:34:56"
|
|
|
|
|
|
|
|
vmd4_entity = registry.async_get("binary_sensor.vmd4")
|
|
|
|
assert vmd4_entity.unique_id == "00:40:8c:12:34:56-vmd4-0"
|