Skip to content

Commit 363c86f

Browse files
authored
Add remove entity to vesync (home-assistant#156213)
1 parent 095a7ad commit 363c86f

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

homeassistant/components/vesync/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from homeassistant.exceptions import ConfigEntryAuthFailed
1212
from homeassistant.helpers import entity_registry as er
1313
from homeassistant.helpers.aiohttp_client import async_get_clientsession
14+
from homeassistant.helpers.device_registry import DeviceEntry
1415
from homeassistant.helpers.dispatcher import async_dispatcher_send
1516

1617
from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_COORDINATOR, VS_MANAGER
@@ -121,3 +122,21 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
121122
hass.config_entries.async_update_entry(config_entry, minor_version=2)
122123

123124
return True
125+
126+
127+
async def async_remove_config_entry_device(
128+
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
129+
) -> bool:
130+
"""Remove a config entry from a device."""
131+
manager = hass.data[DOMAIN][VS_MANAGER]
132+
await manager.get_devices()
133+
for dev in manager.devices:
134+
if isinstance(dev.sub_device_no, int):
135+
device_id = f"{dev.cid}{dev.sub_device_no!s}"
136+
else:
137+
device_id = dev.cid
138+
identifier = next(iter(device_entry.identifiers), None)
139+
if identifier and device_id == identifier[1]:
140+
return False
141+
142+
return True

tests/components/vesync/test_init.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
from pyvesync import VeSync
66
from pyvesync.utils.errors import VeSyncLoginError
77

8-
from homeassistant.components.vesync import SERVICE_UPDATE_DEVS, async_setup_entry
8+
from homeassistant.components.vesync import (
9+
SERVICE_UPDATE_DEVS,
10+
async_remove_config_entry_device,
11+
async_setup_entry,
12+
)
913
from homeassistant.components.vesync.const import DOMAIN, VS_MANAGER
1014
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
1115
from homeassistant.const import Platform
1216
from homeassistant.core import HomeAssistant
13-
from homeassistant.helpers import entity_registry as er
17+
from homeassistant.helpers import device_registry as dr, entity_registry as er
1418

1519
from tests.common import MockConfigEntry
1620

@@ -165,3 +169,51 @@ async def test_migrate_config_entry(
165169
e for e in entity_registry.entities.values() if e.domain == "humidifer"
166170
]
167171
assert len(humidifer_entities) == 1
172+
173+
174+
async def test_async_remove_config_entry_device_positive(
175+
hass: HomeAssistant,
176+
device_registry: dr.DeviceRegistry,
177+
config_entry: ConfigEntry,
178+
manager: VeSync,
179+
fan,
180+
) -> None:
181+
"""Test removing a config entry from a device when no match is found."""
182+
183+
await hass.config_entries.async_setup(config_entry.entry_id)
184+
await hass.async_block_till_done()
185+
manager._dev_list["fans"].append(fan)
186+
187+
device_entry = device_registry.async_get_or_create(
188+
config_entry_id=config_entry.entry_id,
189+
identifiers={(DOMAIN, "test_device")},
190+
)
191+
192+
result = await async_remove_config_entry_device(hass, config_entry, device_entry)
193+
194+
assert result is True
195+
196+
197+
async def test_async_remove_config_entry_device_negative(
198+
hass: HomeAssistant,
199+
device_registry: dr.DeviceRegistry,
200+
config_entry: ConfigEntry,
201+
manager: VeSync,
202+
fan,
203+
) -> None:
204+
"""Test removing a config entry from a device when a match is found."""
205+
206+
assert await hass.config_entries.async_setup(config_entry.entry_id)
207+
await hass.async_block_till_done()
208+
manager._dev_list["fans"].append(fan)
209+
210+
device_entry = device_registry.async_get_or_create(
211+
config_entry_id=config_entry.entry_id,
212+
identifiers={(DOMAIN, "fan")},
213+
)
214+
215+
# Call the remove method
216+
result = await async_remove_config_entry_device(hass, config_entry, device_entry)
217+
218+
# Assert it returns False (device matched)
219+
assert result is False

0 commit comments

Comments
 (0)