Skip to content

Commit 0acfb81

Browse files
authored
Clean up YoLink entities on startup (home-assistant#148718)
1 parent 7d06aec commit 0acfb81

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

homeassistant/components/yolink/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
165165
hass.data[DOMAIN][entry.entry_id] = YoLinkHomeStore(
166166
yolink_home, device_coordinators
167167
)
168+
169+
# Clean up yolink devices which are not associated to the account anymore.
170+
device_registry = dr.async_get(hass)
171+
device_entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
172+
for device_entry in device_entries:
173+
for identifier in device_entry.identifiers:
174+
if (
175+
identifier[0] == DOMAIN
176+
and device_coordinators.get(identifier[1]) is None
177+
):
178+
device_registry.async_update_device(
179+
device_entry.id, remove_config_entry_id=entry.entry_id
180+
)
181+
168182
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
169183

170184
async def async_yolink_unload(event) -> None:
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Provide common fixtures for the YoLink integration tests."""
2+
3+
from __future__ import annotations
4+
5+
from collections.abc import Generator
6+
from unittest.mock import AsyncMock, MagicMock, patch
7+
8+
import pytest
9+
from yolink.home_manager import YoLinkHome
10+
11+
from homeassistant.components.application_credentials import (
12+
ClientCredential,
13+
async_import_client_credential,
14+
)
15+
from homeassistant.components.yolink.api import ConfigEntryAuth
16+
from homeassistant.core import HomeAssistant
17+
from homeassistant.setup import async_setup_component
18+
19+
from tests.common import MockConfigEntry
20+
21+
CLIENT_ID = "12345"
22+
CLIENT_SECRET = "6789"
23+
DOMAIN = "yolink"
24+
25+
26+
@pytest.fixture
27+
async def setup_credentials(hass: HomeAssistant) -> None:
28+
"""Fixture to setup credentials."""
29+
assert await async_setup_component(hass, "application_credentials", {})
30+
await async_import_client_credential(
31+
hass,
32+
DOMAIN,
33+
ClientCredential(CLIENT_ID, CLIENT_SECRET),
34+
)
35+
36+
37+
@pytest.fixture(name="mock_auth_manager")
38+
def mock_auth_manager() -> Generator[MagicMock]:
39+
"""Mock the authentication manager."""
40+
with patch(
41+
"homeassistant.components.yolink.api.ConfigEntryAuth", autospec=True
42+
) as mock_auth:
43+
mock_auth.return_value = MagicMock(spec=ConfigEntryAuth)
44+
yield mock_auth
45+
46+
47+
@pytest.fixture(name="mock_yolink_home")
48+
def mock_yolink_home() -> Generator[AsyncMock]:
49+
"""Mock YoLink home instance."""
50+
with patch(
51+
"homeassistant.components.yolink.YoLinkHome", autospec=True
52+
) as mock_home:
53+
mock_home.return_value = AsyncMock(spec=YoLinkHome)
54+
yield mock_home
55+
56+
57+
@pytest.fixture
58+
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
59+
"""Mock a config entry for YoLink."""
60+
config_entry = MockConfigEntry(
61+
unique_id=DOMAIN,
62+
domain=DOMAIN,
63+
title="yolink",
64+
data={
65+
"auth_implementation": DOMAIN,
66+
"token": {
67+
"refresh_token": "mock-refresh-token",
68+
"access_token": "mock-access-token",
69+
"type": "Bearer",
70+
"expires_in": 60,
71+
"scope": "create",
72+
},
73+
},
74+
options={},
75+
)
76+
config_entry.add_to_hass(hass)
77+
return config_entry
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Tests for the yolink integration."""
2+
3+
import pytest
4+
5+
from homeassistant.components.yolink import DOMAIN
6+
from homeassistant.core import HomeAssistant
7+
from homeassistant.helpers import device_registry as dr
8+
9+
from tests.common import MockConfigEntry
10+
11+
12+
@pytest.mark.usefixtures("setup_credentials", "mock_auth_manager", "mock_yolink_home")
13+
async def test_device_remove_devices(
14+
hass: HomeAssistant,
15+
device_registry: dr.DeviceRegistry,
16+
mock_config_entry: MockConfigEntry,
17+
) -> None:
18+
"""Test we can only remove a device that no longer exists."""
19+
20+
device_registry.async_get_or_create(
21+
config_entry_id=mock_config_entry.entry_id,
22+
identifiers={(DOMAIN, "stale_device_id")},
23+
)
24+
device_entries = dr.async_entries_for_config_entry(
25+
device_registry, mock_config_entry.entry_id
26+
)
27+
28+
assert len(device_entries) == 1
29+
device_entry = device_entries[0]
30+
assert device_entry.identifiers == {(DOMAIN, "stale_device_id")}
31+
32+
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
33+
await hass.async_block_till_done()
34+
35+
device_entries = dr.async_entries_for_config_entry(
36+
device_registry, mock_config_entry.entry_id
37+
)
38+
assert len(device_entries) == 0

0 commit comments

Comments
 (0)