Support device removal in CoolMasterNet integration (#147851)

This commit is contained in:
On Freund 2025-07-01 09:26:04 +03:00 committed by GitHub
parent 9719d2ef2b
commit ddf56f053b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 1 deletions

View File

@ -5,8 +5,9 @@ from pycoolmasternet_async import CoolMasterNet
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from .const import CONF_SWING_SUPPORT
from .const import CONF_SWING_SUPPORT, DOMAIN
from .coordinator import CoolmasterConfigEntry, CoolmasterDataUpdateCoordinator
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR]
@ -48,3 +49,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: CoolmasterConfigEntry) -
async def async_unload_entry(hass: HomeAssistant, entry: CoolmasterConfigEntry) -> bool:
"""Unload a Coolmaster config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_config_entry_device(
hass: HomeAssistant,
config_entry: CoolmasterConfigEntry,
device_entry: dr.DeviceEntry,
) -> bool:
"""Remove a config entry from a device."""
return not device_entry.identifiers.intersection(
(DOMAIN, unit_id) for unit_id in config_entry.runtime_data.data
)

View File

@ -1,7 +1,12 @@
"""The test for the Coolmaster integration."""
from homeassistant.components.coolmaster.const import DOMAIN
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from tests.typing import WebSocketGenerator
async def test_load_entry(
@ -22,3 +27,45 @@ async def test_unload_entry(
await hass.config_entries.async_unload(load_int.entry_id)
await hass.async_block_till_done()
assert load_int.state is ConfigEntryState.NOT_LOADED
async def test_registry_cleanup(
hass: HomeAssistant,
load_int: ConfigEntry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test being able to remove a disconnected device."""
entry_id = load_int.entry_id
device_registry = dr.async_get(hass)
live_id = "L1.100"
dead_id = "L2.200"
assert len(dr.async_entries_for_config_entry(device_registry, entry_id)) == 2
device_registry.async_get_or_create(
config_entry_id=entry_id,
identifiers={(DOMAIN, dead_id)},
manufacturer="CoolAutomation",
model="CoolMasterNet",
name=dead_id,
sw_version="1.0",
)
assert len(dr.async_entries_for_config_entry(device_registry, entry_id)) == 3
assert await async_setup_component(hass, "config", {})
client = await hass_ws_client(hass)
# Try to remove "L1.100" - fails since it is live
device = device_registry.async_get_device(identifiers={(DOMAIN, live_id)})
assert device is not None
response = await client.remove_device(device.id, entry_id)
assert not response["success"]
assert len(dr.async_entries_for_config_entry(device_registry, entry_id)) == 3
assert device_registry.async_get_device(identifiers={(DOMAIN, live_id)}) is not None
# Try to remove "L2.200" - succeeds since it is dead
device = device_registry.async_get_device(identifiers={(DOMAIN, dead_id)})
assert device is not None
response = await client.remove_device(device.id, entry_id)
assert response["success"]
assert len(dr.async_entries_for_config_entry(device_registry, entry_id)) == 2
assert device_registry.async_get_device(identifiers={(DOMAIN, dead_id)}) is None