2018-09-26 16:03:25 +00:00
|
|
|
"""Tests for Tradfri setup."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-09-03 16:39:24 +00:00
|
|
|
from homeassistant.components import tradfri
|
2021-03-09 13:25:03 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2018-09-26 16:03:25 +00:00
|
|
|
|
2021-10-20 11:36:02 +00:00
|
|
|
from . import GATEWAY_ID
|
|
|
|
|
2020-04-25 21:32:55 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-09-26 16:03:25 +00:00
|
|
|
|
|
|
|
|
2021-10-20 11:36:02 +00:00
|
|
|
async def test_entry_setup_unload(hass, mock_api_factory):
|
2020-09-03 16:39:24 +00:00
|
|
|
"""Test config entry setup and unload."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=tradfri.DOMAIN,
|
|
|
|
data={
|
|
|
|
tradfri.CONF_HOST: "mock-host",
|
|
|
|
tradfri.CONF_IDENTITY: "mock-identity",
|
|
|
|
tradfri.CONF_KEY: "mock-key",
|
2021-10-20 11:36:02 +00:00
|
|
|
tradfri.CONF_GATEWAY_ID: GATEWAY_ID,
|
2020-09-03 16:39:24 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
with patch.object(
|
|
|
|
hass.config_entries, "async_forward_entry_setup", return_value=True
|
|
|
|
) as setup:
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert setup.call_count == len(tradfri.PLATFORMS)
|
|
|
|
|
2021-03-09 13:25:03 +00:00
|
|
|
dev_reg = dr.async_get(hass)
|
|
|
|
dev_entries = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
|
2020-09-03 16:39:24 +00:00
|
|
|
|
|
|
|
assert dev_entries
|
|
|
|
dev_entry = dev_entries[0]
|
|
|
|
assert dev_entry.identifiers == {
|
|
|
|
(tradfri.DOMAIN, entry.data[tradfri.CONF_GATEWAY_ID])
|
|
|
|
}
|
2022-04-05 12:00:45 +00:00
|
|
|
assert dev_entry.manufacturer == "IKEA of Sweden"
|
|
|
|
assert dev_entry.name == "Gateway"
|
|
|
|
assert dev_entry.model == "E1526"
|
2020-09-03 16:39:24 +00:00
|
|
|
|
|
|
|
with patch.object(
|
|
|
|
hass.config_entries, "async_forward_entry_unload", return_value=True
|
|
|
|
) as unload:
|
|
|
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert unload.call_count == len(tradfri.PLATFORMS)
|
2021-10-20 11:36:02 +00:00
|
|
|
assert mock_api_factory.shutdown.call_count == 1
|
2022-02-01 00:43:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_remove_stale_devices(hass, mock_api_factory):
|
|
|
|
"""Test remove stale device registry entries."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=tradfri.DOMAIN,
|
|
|
|
data={
|
|
|
|
tradfri.CONF_HOST: "mock-host",
|
|
|
|
tradfri.CONF_IDENTITY: "mock-identity",
|
|
|
|
tradfri.CONF_KEY: "mock-key",
|
|
|
|
tradfri.CONF_GATEWAY_ID: GATEWAY_ID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
dev_reg = dr.async_get(hass)
|
|
|
|
dev_reg.async_get_or_create(
|
|
|
|
config_entry_id=entry.entry_id,
|
|
|
|
identifiers={(tradfri.DOMAIN, "stale_device_id")},
|
|
|
|
)
|
|
|
|
dev_entries = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
|
|
|
|
|
|
|
|
assert len(dev_entries) == 1
|
|
|
|
dev_entry = dev_entries[0]
|
|
|
|
assert dev_entry.identifiers == {(tradfri.DOMAIN, "stale_device_id")}
|
|
|
|
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
dev_entries = dr.async_entries_for_config_entry(dev_reg, entry.entry_id)
|
|
|
|
|
|
|
|
# Check that only the gateway device entry remains.
|
|
|
|
assert len(dev_entries) == 1
|
|
|
|
dev_entry = dev_entries[0]
|
|
|
|
assert dev_entry.identifiers == {
|
|
|
|
(tradfri.DOMAIN, entry.data[tradfri.CONF_GATEWAY_ID])
|
|
|
|
}
|
2022-04-05 12:00:45 +00:00
|
|
|
assert dev_entry.manufacturer == "IKEA of Sweden"
|
|
|
|
assert dev_entry.name == "Gateway"
|
|
|
|
assert dev_entry.model == "E1526"
|