Use DeviceInfo on components with via_device (#58222)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-10-22 17:04:25 +02:00 committed by GitHub
parent fc3e7f5b7e
commit 51a10f88de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 62 deletions

View file

@ -14,9 +14,9 @@ from aiohomekit.model.characteristics import (
from aiohomekit.model.services import Service, ServicesTypes
from homeassistant.components import zeroconf
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.const import ATTR_VIA_DEVICE, EVENT_HOMEASSISTANT_STOP
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import DeviceInfo, Entity
from .config_flow import normalize_hkid
from .connection import HKDevice
@ -145,24 +145,24 @@ class HomeKitEntity(Entity):
return self._accessory.available and self.service.available
@property
def device_info(self):
def device_info(self) -> DeviceInfo:
"""Return the device info."""
info = self.accessory_info
accessory_serial = info.value(CharacteristicsTypes.SERIAL_NUMBER)
device_info = {
"identifiers": {(DOMAIN, "serial-number", accessory_serial)},
"name": info.value(CharacteristicsTypes.NAME),
"manufacturer": info.value(CharacteristicsTypes.MANUFACTURER, ""),
"model": info.value(CharacteristicsTypes.MODEL, ""),
"sw_version": info.value(CharacteristicsTypes.FIRMWARE_REVISION, ""),
}
device_info = DeviceInfo(
identifiers={(DOMAIN, "serial-number", accessory_serial)},
name=info.value(CharacteristicsTypes.NAME),
manufacturer=info.value(CharacteristicsTypes.MANUFACTURER, ""),
model=info.value(CharacteristicsTypes.MODEL, ""),
sw_version=info.value(CharacteristicsTypes.FIRMWARE_REVISION, ""),
)
# Some devices only have a single accessory - we don't add a
# via_device otherwise it would be self referential.
bridge_serial = self._accessory.connection_info["serial-number"]
if accessory_serial != bridge_serial:
device_info["via_device"] = (DOMAIN, "serial-number", bridge_serial)
device_info[ATTR_VIA_DEVICE] = (DOMAIN, "serial-number", bridge_serial)
return device_info

View file

@ -12,8 +12,10 @@ from aiohomekit.model import Accessories
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes
from homeassistant.const import ATTR_IDENTIFIERS, ATTR_VIA_DEVICE
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.event import async_track_time_interval
from .const import (
@ -205,29 +207,31 @@ class HKDevice:
service_type=ServicesTypes.ACCESSORY_INFORMATION,
)
device_info = {
"identifiers": {
device_info = DeviceInfo(
identifiers={
(
DOMAIN,
"serial-number",
info.value(CharacteristicsTypes.SERIAL_NUMBER),
)
},
"name": info.value(CharacteristicsTypes.NAME),
"manufacturer": info.value(CharacteristicsTypes.MANUFACTURER, ""),
"model": info.value(CharacteristicsTypes.MODEL, ""),
"sw_version": info.value(CharacteristicsTypes.FIRMWARE_REVISION, ""),
}
name=info.value(CharacteristicsTypes.NAME),
manufacturer=info.value(CharacteristicsTypes.MANUFACTURER, ""),
model=info.value(CharacteristicsTypes.MODEL, ""),
sw_version=info.value(CharacteristicsTypes.FIRMWARE_REVISION, ""),
)
if accessory.aid == 1:
# Accessory 1 is the root device (sometimes the only device, sometimes a bridge)
# Link the root device to the pairing id for the connection.
device_info["identifiers"].add((DOMAIN, "accessory-id", self.unique_id))
device_info[ATTR_IDENTIFIERS].add(
(DOMAIN, "accessory-id", self.unique_id)
)
else:
# Every pairing has an accessory 1
# It *doesn't* have a via_device, as it is the device we are connecting to
# Every other accessory should use it as its via device.
device_info["via_device"] = (
device_info[ATTR_VIA_DEVICE] = (
DOMAIN,
"serial-number",
self.connection_info["serial-number"],