2018-04-18 14:27:44 +00:00
|
|
|
"""Test deCONZ component setup process."""
|
2020-10-17 16:20:06 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
import asyncio
|
2020-01-03 10:50:53 +00:00
|
|
|
from copy import deepcopy
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2019-09-25 16:56:31 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.deconz import (
|
|
|
|
DeconzGateway,
|
|
|
|
async_setup_entry,
|
|
|
|
async_unload_entry,
|
2021-02-06 13:32:17 +00:00
|
|
|
async_update_group_unique_id,
|
|
|
|
)
|
|
|
|
from homeassistant.components.deconz.const import (
|
|
|
|
CONF_GROUP_ID_BASE,
|
|
|
|
DOMAIN as DECONZ_DOMAIN,
|
2020-10-17 16:20:06 +00:00
|
|
|
)
|
2020-10-06 21:25:57 +00:00
|
|
|
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
|
2021-02-06 13:32:17 +00:00
|
|
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
|
|
|
|
from homeassistant.helpers import entity_registry
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2020-01-03 17:11:04 +00:00
|
|
|
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2021-02-06 13:32:17 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTRY1_HOST = "1.2.3.4"
|
2019-04-05 00:48:24 +00:00
|
|
|
ENTRY1_PORT = 80
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTRY1_API_KEY = "1234567890ABCDEF"
|
|
|
|
ENTRY1_BRIDGEID = "12345ABC"
|
2019-09-25 16:56:31 +00:00
|
|
|
ENTRY1_UUID = "456DEF"
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTRY2_HOST = "2.3.4.5"
|
2019-04-05 00:48:24 +00:00
|
|
|
ENTRY2_PORT = 80
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTRY2_API_KEY = "1234567890ABCDEF"
|
|
|
|
ENTRY2_BRIDGEID = "23456DEF"
|
2019-09-25 16:56:31 +00:00
|
|
|
ENTRY2_UUID = "789ACE"
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def setup_entry(hass, entry):
|
|
|
|
"""Test that setup entry works."""
|
2020-10-17 16:20:06 +00:00
|
|
|
with patch.object(DeconzGateway, "async_setup", return_value=True), patch.object(
|
|
|
|
DeconzGateway, "async_update_device_registry", return_value=True
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-10-17 16:20:06 +00:00
|
|
|
assert await async_setup_entry(hass, entry) is True
|
2018-08-24 17:37:22 +00:00
|
|
|
|
2018-04-18 14:27:44 +00:00
|
|
|
|
2019-03-24 18:27:32 +00:00
|
|
|
async def test_setup_entry_fails(hass):
|
|
|
|
"""Test setup entry fails if deCONZ is not available."""
|
2019-12-08 15:53:34 +00:00
|
|
|
with patch("pydeconz.DeconzSession.initialize", side_effect=Exception):
|
2020-01-03 17:11:04 +00:00
|
|
|
await setup_deconz_integration(hass)
|
2020-10-17 16:20:06 +00:00
|
|
|
assert not hass.data[DECONZ_DOMAIN]
|
2019-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
|
2018-04-23 16:00:16 +00:00
|
|
|
async def test_setup_entry_no_available_bridge(hass):
|
|
|
|
"""Test setup entry fails if deCONZ is not available."""
|
2020-01-03 17:11:04 +00:00
|
|
|
with patch("pydeconz.DeconzSession.initialize", side_effect=asyncio.TimeoutError):
|
|
|
|
await setup_deconz_integration(hass)
|
2020-10-17 16:20:06 +00:00
|
|
|
assert not hass.data[DECONZ_DOMAIN]
|
2018-04-23 16:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_successful(hass):
|
|
|
|
"""Test setup entry is successful."""
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry = await setup_deconz_integration(hass)
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert hass.data[DECONZ_DOMAIN]
|
|
|
|
assert gateway.bridgeid in hass.data[DECONZ_DOMAIN]
|
|
|
|
assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_entry_multiple_gateways(hass):
|
|
|
|
"""Test setup entry is successful with multiple gateways."""
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry = await setup_deconz_integration(hass)
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-01-03 17:11:04 +00:00
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
data["config"]["bridgeid"] = "01234E56789B"
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry2 = await setup_deconz_integration(
|
2021-02-06 13:32:17 +00:00
|
|
|
hass, get_state_response=data, entry_id="2", unique_id="01234E56789B"
|
2020-01-03 10:50:53 +00:00
|
|
|
)
|
2020-10-06 21:25:57 +00:00
|
|
|
gateway2 = get_gateway_from_config_entry(hass, config_entry2)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
|
|
|
assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master
|
|
|
|
assert not hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master
|
2018-04-29 14:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_entry(hass):
|
|
|
|
"""Test being able to unload an entry."""
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry = await setup_deconz_integration(hass)
|
2020-10-17 16:20:06 +00:00
|
|
|
assert hass.data[DECONZ_DOMAIN]
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert await async_unload_entry(hass, config_entry)
|
|
|
|
assert not hass.data[DECONZ_DOMAIN]
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_entry_multiple_gateways(hass):
|
|
|
|
"""Test being able to unload an entry and master gateway gets moved."""
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry = await setup_deconz_integration(hass)
|
2020-01-03 10:50:53 +00:00
|
|
|
|
2020-01-03 17:11:04 +00:00
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
data["config"]["bridgeid"] = "01234E56789B"
|
2020-10-06 21:25:57 +00:00
|
|
|
config_entry2 = await setup_deconz_integration(
|
2021-02-06 13:32:17 +00:00
|
|
|
hass, get_state_response=data, entry_id="2", unique_id="01234E56789B"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-10-06 21:25:57 +00:00
|
|
|
gateway2 = get_gateway_from_config_entry(hass, config_entry2)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert await async_unload_entry(hass, config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert len(hass.data[DECONZ_DOMAIN]) == 1
|
|
|
|
assert hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master
|
2021-02-06 13:32:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_group_unique_id(hass):
|
|
|
|
"""Test successful migration of entry data."""
|
|
|
|
old_unique_id = "123"
|
|
|
|
new_unique_id = "1234"
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DECONZ_DOMAIN,
|
|
|
|
unique_id=new_unique_id,
|
|
|
|
data={
|
|
|
|
CONF_API_KEY: "1",
|
|
|
|
CONF_HOST: "2",
|
|
|
|
CONF_GROUP_ID_BASE: old_unique_id,
|
|
|
|
CONF_PORT: "3",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
registry = await entity_registry.async_get_registry(hass)
|
|
|
|
# Create entity entry to migrate to new unique ID
|
|
|
|
registry.async_get_or_create(
|
|
|
|
LIGHT_DOMAIN,
|
|
|
|
DECONZ_DOMAIN,
|
|
|
|
f"{old_unique_id}-OLD",
|
|
|
|
suggested_object_id="old",
|
|
|
|
config_entry=entry,
|
|
|
|
)
|
|
|
|
# Create entity entry with new unique ID
|
|
|
|
registry.async_get_or_create(
|
|
|
|
LIGHT_DOMAIN,
|
|
|
|
DECONZ_DOMAIN,
|
|
|
|
f"{new_unique_id}-NEW",
|
|
|
|
suggested_object_id="new",
|
|
|
|
config_entry=entry,
|
|
|
|
)
|
|
|
|
|
|
|
|
await async_update_group_unique_id(hass, entry)
|
|
|
|
|
|
|
|
assert entry.data == {CONF_API_KEY: "1", CONF_HOST: "2", CONF_PORT: "3"}
|
|
|
|
|
|
|
|
old_entity = registry.async_get(f"{LIGHT_DOMAIN}.old")
|
|
|
|
assert old_entity.unique_id == f"{new_unique_id}-OLD"
|
|
|
|
|
|
|
|
new_entity = registry.async_get(f"{LIGHT_DOMAIN}.new")
|
|
|
|
assert new_entity.unique_id == f"{new_unique_id}-NEW"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_update_group_unique_id_no_legacy_group_id(hass):
|
|
|
|
"""Test migration doesn't trigger without old legacy group id in entry data."""
|
|
|
|
old_unique_id = "123"
|
|
|
|
new_unique_id = "1234"
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DECONZ_DOMAIN,
|
|
|
|
unique_id=new_unique_id,
|
|
|
|
data={},
|
|
|
|
)
|
|
|
|
|
|
|
|
registry = await entity_registry.async_get_registry(hass)
|
|
|
|
# Create entity entry to migrate to new unique ID
|
|
|
|
registry.async_get_or_create(
|
|
|
|
LIGHT_DOMAIN,
|
|
|
|
DECONZ_DOMAIN,
|
|
|
|
f"{old_unique_id}-OLD",
|
|
|
|
suggested_object_id="old",
|
|
|
|
config_entry=entry,
|
|
|
|
)
|
|
|
|
|
|
|
|
await async_update_group_unique_id(hass, entry)
|
|
|
|
|
|
|
|
old_entity = registry.async_get(f"{LIGHT_DOMAIN}.old")
|
|
|
|
assert old_entity.unique_id == f"{old_unique_id}-OLD"
|