2018-09-26 16:03:25 +00:00
|
|
|
"""Tests for Tradfri setup."""
|
2024-02-11 11:01:12 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-09-03 16:39:24 +00:00
|
|
|
from homeassistant.components import tradfri
|
2023-02-17 15:40:46 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
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
|
|
|
|
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
async def test_entry_setup_unload(
|
|
|
|
hass: HomeAssistant, device_registry: dr.DeviceRegistry, mock_api_factory: MagicMock
|
|
|
|
) -> None:
|
2020-09-03 16:39:24 +00:00
|
|
|
"""Test config entry setup and unload."""
|
2024-02-11 11:01:12 +00:00
|
|
|
config_entry = MockConfigEntry(
|
2020-09-03 16:39:24 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
|
|
device_registry, config_entry.entry_id
|
|
|
|
)
|
|
|
|
|
|
|
|
assert device_entries
|
|
|
|
device_entry = device_entries[0]
|
|
|
|
assert device_entry.identifiers == {
|
|
|
|
(tradfri.DOMAIN, config_entry.data[tradfri.CONF_GATEWAY_ID])
|
2020-09-03 16:39:24 +00:00
|
|
|
}
|
2024-02-11 11:01:12 +00:00
|
|
|
assert device_entry.manufacturer == "IKEA of Sweden"
|
|
|
|
assert device_entry.name == "Gateway"
|
|
|
|
assert device_entry.model == "E1526"
|
2020-09-03 16:39:24 +00:00
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_api_factory.shutdown.call_count == 1
|
2022-02-01 00:43:16 +00:00
|
|
|
|
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
async def test_remove_stale_devices(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
) -> None:
|
2022-02-01 00:43:16 +00:00
|
|
|
"""Test remove stale device registry entries."""
|
2024-02-11 11:01:12 +00:00
|
|
|
config_entry = MockConfigEntry(
|
2022-02-01 00:43:16 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
2022-02-01 00:43:16 +00:00
|
|
|
identifiers={(tradfri.DOMAIN, "stale_device_id")},
|
|
|
|
)
|
2024-02-11 11:01:12 +00:00
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
|
|
device_registry, config_entry.entry_id
|
|
|
|
)
|
2022-02-01 00:43:16 +00:00
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
assert len(device_entries) == 1
|
|
|
|
device_entry = device_entries[0]
|
|
|
|
assert device_entry.identifiers == {(tradfri.DOMAIN, "stale_device_id")}
|
2022-02-01 00:43:16 +00:00
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2022-02-01 00:43:16 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-02-11 11:01:12 +00:00
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
|
|
device_registry, config_entry.entry_id
|
|
|
|
)
|
2022-02-01 00:43:16 +00:00
|
|
|
|
|
|
|
# Check that only the gateway device entry remains.
|
2024-02-11 11:01:12 +00:00
|
|
|
assert len(device_entries) == 1
|
|
|
|
device_entry = device_entries[0]
|
|
|
|
assert device_entry.identifiers == {
|
|
|
|
(tradfri.DOMAIN, config_entry.data[tradfri.CONF_GATEWAY_ID])
|
2022-02-01 00:43:16 +00:00
|
|
|
}
|
2024-02-11 11:01:12 +00:00
|
|
|
assert device_entry.manufacturer == "IKEA of Sweden"
|
|
|
|
assert device_entry.name == "Gateway"
|
|
|
|
assert device_entry.model == "E1526"
|