Add support for async_remove_config_entry_device to homekit_controller (#72630)

This commit is contained in:
J. Nick Koston 2022-05-28 09:47:14 -10:00 committed by GitHub
parent 233f086853
commit e0614953a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 1 deletions

View file

@ -8,9 +8,17 @@ from aiohomekit.model.services import ServicesTypes
from homeassistant.components.homekit_controller.const import ENTITY_MAP
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import EntityRegistry
from homeassistant.setup import async_setup_component
from .common import Helper, remove_device
from tests.components.homekit_controller.common import setup_test_component
ALIVE_DEVICE_NAME = "Light Bulb"
ALIVE_DEEVICE_ENTITY_ID = "light.testdevice"
def create_motion_sensor_service(accessory):
"""Define motion characteristics as per page 225 of HAP spec."""
@ -47,3 +55,37 @@ async def test_async_remove_entry(hass: HomeAssistant):
assert len(controller.pairings) == 0
assert hkid not in hass.data[ENTITY_MAP].storage_data
def create_alive_service(accessory):
"""Create a service to validate we can only remove dead devices."""
service = accessory.add_service(ServicesTypes.LIGHTBULB, name=ALIVE_DEVICE_NAME)
service.add_char(CharacteristicsTypes.ON)
return service
async def test_device_remove_devices(hass, hass_ws_client):
"""Test we can only remove a device that no longer exists."""
assert await async_setup_component(hass, "config", {})
helper: Helper = await setup_test_component(hass, create_alive_service)
config_entry = helper.config_entry
entry_id = config_entry.entry_id
registry: EntityRegistry = er.async_get(hass)
entity = registry.entities[ALIVE_DEEVICE_ENTITY_ID]
device_registry = dr.async_get(hass)
live_device_entry = device_registry.async_get(entity.device_id)
assert (
await remove_device(await hass_ws_client(hass), live_device_entry.id, entry_id)
is False
)
dead_device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={("homekit_controller:accessory-id", "E9:88:E7:B8:B4:40:aid:1")},
)
assert (
await remove_device(await hass_ws_client(hass), dead_device_entry.id, entry_id)
is True
)