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

@ -30,6 +30,7 @@ from homeassistant.const import (
PRESSURE_HPA,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS,
Platform,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
@ -556,11 +557,16 @@ class RSSISensor(HomeKitEntity, SensorEntity):
return "Signal strength"
@property
def unique_id(self) -> str:
"""Return the ID of this device."""
def old_unique_id(self) -> str:
"""Return the old ID of this device."""
serial = self.accessory_info.value(CharacteristicsTypes.SERIAL_NUMBER)
return f"homekit-{serial}-rssi"
@property
def unique_id(self) -> str:
"""Return the ID of this device."""
return f"{self._accessory.unique_id}_rssi"
@property
def native_value(self) -> int | None:
"""Return the current rssi value."""
@ -587,7 +593,11 @@ async def async_setup_entry(
) and not service.has(required_char):
return False
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)])
entity: HomeKitSensor = entity_class(conn, info)
conn.async_migrate_unique_id(
entity.old_unique_id, entity.unique_id, Platform.SENSOR
)
async_add_entities([entity])
return True
conn.add_listener(async_add_service)
@ -599,7 +609,11 @@ async def async_setup_entry(
if description.probe and not description.probe(char):
return False
info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
async_add_entities([SimpleSensor(conn, info, char, description)])
entity = SimpleSensor(conn, info, char, description)
conn.async_migrate_unique_id(
entity.old_unique_id, entity.unique_id, Platform.SENSOR
)
async_add_entities([entity])
return True
@ -614,7 +628,11 @@ async def async_setup_entry(
service_type=ServicesTypes.ACCESSORY_INFORMATION
)
info = {"aid": accessory.aid, "iid": accessory_info.iid}
async_add_entities([RSSISensor(conn, info)])
entity = RSSISensor(conn, info)
conn.async_migrate_unique_id(
entity.old_unique_id, entity.unique_id, Platform.SENSOR
)
async_add_entities([entity])
return True
conn.add_accessory_factory(async_add_accessory)