Clean up homekit_controller entity setup (#32628)

This commit is contained in:
Jc2k 2020-03-10 11:06:44 +00:00 committed by GitHub
parent a8758ed3a1
commit c11a462f51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 38 deletions

View file

@ -3,7 +3,9 @@ import logging
import os import os
import aiohomekit import aiohomekit
from aiohomekit.model import Accessory
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import Service, ServicesTypes
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
@ -11,7 +13,7 @@ from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from .config_flow import normalize_hkid from .config_flow import normalize_hkid
from .connection import HKDevice, get_accessory_information from .connection import HKDevice
from .const import CONTROLLER, DOMAIN, ENTITY_MAP, KNOWN_DEVICES from .const import CONTROLLER, DOMAIN, ENTITY_MAP, KNOWN_DEVICES
from .storage import EntityMapStorage from .storage import EntityMapStorage
@ -37,6 +39,23 @@ class HomeKitEntity(Entity):
self._signals = [] self._signals = []
@property
def accessory(self) -> Accessory:
"""Return an Accessory model that this entity is attached to."""
return self._accessory.entity_map.aid(self._aid)
@property
def accessory_info(self) -> Service:
"""Information about the make and model of an accessory."""
return self.accessory.services.first(
service_type=ServicesTypes.ACCESSORY_INFORMATION
)
@property
def service(self) -> Service:
"""Return a Service model that this entity is attached to."""
return self.accessory.services.iid(self._iid)
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Entity added to hass.""" """Entity added to hass."""
self._signals.append( self._signals.append(
@ -67,8 +86,6 @@ class HomeKitEntity(Entity):
def setup(self): def setup(self):
"""Configure an entity baed on its HomeKit characteristics metadata.""" """Configure an entity baed on its HomeKit characteristics metadata."""
accessories = self._accessory.accessories
get_uuid = CharacteristicsTypes.get_uuid get_uuid = CharacteristicsTypes.get_uuid
characteristic_types = [get_uuid(c) for c in self.get_characteristic_types()] characteristic_types = [get_uuid(c) for c in self.get_characteristic_types()]
@ -77,30 +94,16 @@ class HomeKitEntity(Entity):
self._chars = {} self._chars = {}
self._char_names = {} self._char_names = {}
for accessory in accessories: # Setup events and/or polling for characteristics directly attached to this entity
if accessory["aid"] != self._aid: for char in self.service.characteristics:
if char.type not in characteristic_types:
continue continue
self._accessory_info = get_accessory_information(accessory) self._setup_characteristic(char.to_accessory_and_service_list())
for service in accessory["services"]:
if service["iid"] != self._iid:
continue
for char in service["characteristics"]:
try:
uuid = CharacteristicsTypes.get_uuid(char["type"])
except KeyError:
# If a KeyError is raised its a non-standard
# characteristic. We must ignore it in this case.
continue
if uuid not in characteristic_types:
continue
self._setup_characteristic(char)
accessory = self._accessory.entity_map.aid(self._aid) # Setup events and/or polling for characteristics attached to sub-services of this
this_service = accessory.services.iid(self._iid) # entity (like an INPUT_SOURCE).
for child_service in accessory.services.filter( for service in self.accessory.services.filter(parent_service=self.service):
parent_service=this_service for char in service.characteristics:
):
for char in child_service.characteristics:
if char.type not in characteristic_types: if char.type not in characteristic_types:
continue continue
self._setup_characteristic(char.to_accessory_and_service_list()) self._setup_characteristic(char.to_accessory_and_service_list())
@ -165,13 +168,13 @@ class HomeKitEntity(Entity):
@property @property
def unique_id(self): def unique_id(self):
"""Return the ID of this device.""" """Return the ID of this device."""
serial = self._accessory_info["serial-number"] serial = self.accessory_info.value(CharacteristicsTypes.SERIAL_NUMBER)
return f"homekit-{serial}-{self._iid}" return f"homekit-{serial}-{self._iid}"
@property @property
def name(self): def name(self):
"""Return the name of the device if any.""" """Return the name of the device if any."""
return self._accessory_info.get("name") return self.accessory_info.value(CharacteristicsTypes.NAME)
@property @property
def available(self) -> bool: def available(self) -> bool:
@ -181,14 +184,15 @@ class HomeKitEntity(Entity):
@property @property
def device_info(self): def device_info(self):
"""Return the device info.""" """Return the device info."""
accessory_serial = self._accessory_info["serial-number"] info = self.accessory_info
accessory_serial = info.value(CharacteristicsTypes.SERIAL_NUMBER)
device_info = { device_info = {
"identifiers": {(DOMAIN, "serial-number", accessory_serial)}, "identifiers": {(DOMAIN, "serial-number", accessory_serial)},
"name": self._accessory_info["name"], "name": info.value(CharacteristicsTypes.NAME),
"manufacturer": self._accessory_info.get("manufacturer", ""), "manufacturer": info.value(CharacteristicsTypes.MANUFACTURER, ""),
"model": self._accessory_info.get("model", ""), "model": info.value(CharacteristicsTypes.MODEL, ""),
"sw_version": self._accessory_info.get("firmware.revision", ""), "sw_version": info.value(CharacteristicsTypes.FIRMWARE_REVISION, ""),
} }
# Some devices only have a single accessory - we don't add a # Some devices only have a single accessory - we don't add a

View file

@ -3,7 +3,7 @@
"name": "HomeKit Controller", "name": "HomeKit Controller",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/homekit_controller", "documentation": "https://www.home-assistant.io/integrations/homekit_controller",
"requirements": ["aiohomekit[IP]==0.2.24"], "requirements": ["aiohomekit[IP]==0.2.25"],
"dependencies": [], "dependencies": [],
"zeroconf": ["_hap._tcp.local."], "zeroconf": ["_hap._tcp.local."],
"codeowners": ["@Jc2k"] "codeowners": ["@Jc2k"]

View file

@ -163,7 +163,7 @@ aioftp==0.12.0
aioharmony==0.1.13 aioharmony==0.1.13
# homeassistant.components.homekit_controller # homeassistant.components.homekit_controller
aiohomekit[IP]==0.2.24 aiohomekit[IP]==0.2.25
# homeassistant.components.emulated_hue # homeassistant.components.emulated_hue
# homeassistant.components.http # homeassistant.components.http

View file

@ -62,7 +62,7 @@ aiobotocore==0.11.1
aioesphomeapi==2.6.1 aioesphomeapi==2.6.1
# homeassistant.components.homekit_controller # homeassistant.components.homekit_controller
aiohomekit[IP]==0.2.24 aiohomekit[IP]==0.2.25
# homeassistant.components.emulated_hue # homeassistant.components.emulated_hue
# homeassistant.components.http # homeassistant.components.http