Migrate HomeKit Controller to use stable identifiers (#80064)

This commit is contained in:
J. Nick Koston 2022-10-11 11:26:03 -10:00 committed by GitHub
parent e3a3f93441
commit f23b1750e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 781 additions and 234 deletions

View file

@ -14,6 +14,7 @@ from aiohomekit.model.services import Service, ServicesTypes
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -182,7 +183,7 @@ class DeclarativeCharacteristicSwitch(CharacteristicEntity, SwitchEntity):
)
ENTITY_TYPES = {
ENTITY_TYPES: dict[str, type[HomeKitSwitch] | type[HomeKitValve]] = {
ServicesTypes.SWITCH: HomeKitSwitch,
ServicesTypes.OUTLET: HomeKitSwitch,
ServicesTypes.VALVE: HomeKitValve,
@ -195,15 +196,19 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Homekit switches."""
hkid = config_entry.data["AccessoryPairingID"]
conn = hass.data[KNOWN_DEVICES][hkid]
hkid: str = config_entry.data["AccessoryPairingID"]
conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
@callback
def async_add_service(service: Service) -> bool:
if not (entity_class := ENTITY_TYPES.get(service.type)):
return False
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)
entity: HomeKitSwitch | HomeKitValve = entity_class(conn, info)
conn.async_migrate_unique_id(
entity.old_unique_id, entity.unique_id, Platform.SWITCH
)
async_add_entities([entity])
return True
conn.add_listener(async_add_service)
@ -214,9 +219,11 @@ async def async_setup_entry(
return False
info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
async_add_entities(
[DeclarativeCharacteristicSwitch(conn, info, char, description)], True
entity = DeclarativeCharacteristicSwitch(conn, info, char, description)
conn.async_migrate_unique_id(
entity.old_unique_id, entity.unique_id, Platform.SWITCH
)
async_add_entities([entity])
return True
conn.add_char_factory(async_add_characteristic)