2023-01-03 20:21:11 +02:00
|
|
|
"""The test for the Coolmaster integration."""
|
2024-03-08 14:50:25 +01:00
|
|
|
|
2025-07-01 09:26:04 +03:00
|
|
|
from homeassistant.components.coolmaster.const import DOMAIN
|
2023-01-03 20:21:11 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
2025-07-01 09:26:04 +03:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
|
|
|
|
|
from tests.typing import WebSocketGenerator
|
2023-01-03 20:21:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_load_entry(
|
2026-01-27 04:07:22 +08:00
|
|
|
hass: HomeAssistant, load_int: ConfigEntry, unit_count: int
|
2023-01-03 20:21:11 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Test Coolmaster initial load."""
|
2026-01-27 04:07:22 +08:00
|
|
|
# 4 units times 4 entities (climate, binary_sensor, sensor, button).
|
|
|
|
|
assert hass.states.async_entity_ids_count() == unit_count * 4
|
2023-01-03 20:21:11 +02:00
|
|
|
assert load_int.state is ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_entry(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
load_int: ConfigEntry,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test Coolmaster unloading an 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
|
2025-07-01 09:26:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_registry_cleanup(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
load_int: ConfigEntry,
|
|
|
|
|
hass_ws_client: WebSocketGenerator,
|
2026-01-27 04:07:22 +08:00
|
|
|
unit_count: int,
|
2025-07-01 09:26:04 +03:00
|
|
|
) -> 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"
|
|
|
|
|
|
2026-01-27 04:07:22 +08:00
|
|
|
assert (
|
|
|
|
|
len(dr.async_entries_for_config_entry(device_registry, entry_id)) == unit_count
|
|
|
|
|
)
|
2025-07-01 09:26:04 +03:00
|
|
|
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",
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-27 04:07:22 +08:00
|
|
|
assert (
|
|
|
|
|
len(dr.async_entries_for_config_entry(device_registry, entry_id))
|
|
|
|
|
== unit_count + 1
|
|
|
|
|
)
|
2025-07-01 09:26:04 +03:00
|
|
|
|
|
|
|
|
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"]
|
2026-01-27 04:07:22 +08:00
|
|
|
assert (
|
|
|
|
|
len(dr.async_entries_for_config_entry(device_registry, entry_id))
|
|
|
|
|
== unit_count + 1
|
|
|
|
|
)
|
2025-07-01 09:26:04 +03:00
|
|
|
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"]
|
2026-01-27 04:07:22 +08:00
|
|
|
assert (
|
|
|
|
|
len(dr.async_entries_for_config_entry(device_registry, entry_id)) == unit_count
|
|
|
|
|
)
|
2025-07-01 09:26:04 +03:00
|
|
|
assert device_registry.async_get_device(identifiers={(DOMAIN, dead_id)}) is None
|