Add remove entity to vesync (#156213)

This commit is contained in:
cdnninja
2025-11-11 01:35:19 -07:00
committed by GitHub
parent 095a7ad060
commit 363c86faf3
2 changed files with 73 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.dispatcher import async_dispatcher_send
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) ->
hass.config_entries.async_update_entry(config_entry, minor_version=2)
return True
async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Remove a config entry from a device."""
manager = hass.data[DOMAIN][VS_MANAGER]
await manager.get_devices()
for dev in manager.devices:
if isinstance(dev.sub_device_no, int):
device_id = f"{dev.cid}{dev.sub_device_no!s}"
else:
device_id = dev.cid
identifier = next(iter(device_entry.identifiers), None)
if identifier and device_id == identifier[1]:
return False
return True

View File

@@ -5,12 +5,16 @@ from unittest.mock import AsyncMock, patch
from pyvesync import VeSync
from pyvesync.utils.errors import VeSyncLoginError
from homeassistant.components.vesync import SERVICE_UPDATE_DEVS, async_setup_entry
from homeassistant.components.vesync import (
SERVICE_UPDATE_DEVS,
async_remove_config_entry_device,
async_setup_entry,
)
from homeassistant.components.vesync.const import DOMAIN, VS_MANAGER
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
@@ -165,3 +169,51 @@ async def test_migrate_config_entry(
e for e in entity_registry.entities.values() if e.domain == "humidifer"
]
assert len(humidifer_entities) == 1
async def test_async_remove_config_entry_device_positive(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry: ConfigEntry,
manager: VeSync,
fan,
) -> None:
"""Test removing a config entry from a device when no match is found."""
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
manager._dev_list["fans"].append(fan)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, "test_device")},
)
result = await async_remove_config_entry_device(hass, config_entry, device_entry)
assert result is True
async def test_async_remove_config_entry_device_negative(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry: ConfigEntry,
manager: VeSync,
fan,
) -> None:
"""Test removing a config entry from a device when a match is found."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
manager._dev_list["fans"].append(fan)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, "fan")},
)
# Call the remove method
result = await async_remove_config_entry_device(hass, config_entry, device_entry)
# Assert it returns False (device matched)
assert result is False