From eb94fe1ca7e0871478a54ec832125670e1a614e2 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Sun, 30 Jan 2022 22:59:01 +0000 Subject: [PATCH] Use upstream constants when defining homekit service to platform mapping (#65272) --- .../homekit_controller/connection.py | 6 +- .../components/homekit_controller/const.py | 55 ++++++++++--------- tests/components/homekit_controller/common.py | 2 +- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index 88706e6fbd0..08b3e9823e3 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -494,7 +494,11 @@ class HKDevice: tasks = [] for accessory in self.accessories: for service in accessory["services"]: - stype = ServicesTypes.get_short(service["type"].upper()) + try: + stype = ServicesTypes.get_short_uuid(service["type"].upper()) + except KeyError: + stype = service["type"].upper() + if stype in HOMEKIT_ACCESSORY_DISPATCH: platform = HOMEKIT_ACCESSORY_DISPATCH[stype] if platform not in self.platforms: diff --git a/homeassistant/components/homekit_controller/const.py b/homeassistant/components/homekit_controller/const.py index d88f896af31..9ec991c0d88 100644 --- a/homeassistant/components/homekit_controller/const.py +++ b/homeassistant/components/homekit_controller/const.py @@ -2,6 +2,7 @@ from typing import Final from aiohomekit.model.characteristics import CharacteristicsTypes +from aiohomekit.model.services import ServicesTypes DOMAIN = "homekit_controller" @@ -20,33 +21,33 @@ IDENTIFIER_LEGACY_ACCESSORY_ID = "accessory-id" # Mapping from Homekit type to component. HOMEKIT_ACCESSORY_DISPATCH = { - "lightbulb": "light", - "outlet": "switch", - "switch": "switch", - "thermostat": "climate", - "heater-cooler": "climate", - "security-system": "alarm_control_panel", - "garage-door-opener": "cover", - "window": "cover", - "window-covering": "cover", - "lock-mechanism": "lock", - "contact": "binary_sensor", - "motion": "binary_sensor", - "carbon-dioxide": "sensor", - "humidity": "sensor", - "humidifier-dehumidifier": "humidifier", - "light": "sensor", - "temperature": "sensor", - "battery": "sensor", - "smoke": "binary_sensor", - "carbon-monoxide": "binary_sensor", - "leak": "binary_sensor", - "fan": "fan", - "fanv2": "fan", - "occupancy": "binary_sensor", - "television": "media_player", - "valve": "switch", - "camera-rtp-stream-management": "camera", + ServicesTypes.LIGHTBULB: "light", + ServicesTypes.OUTLET: "switch", + ServicesTypes.SWITCH: "switch", + ServicesTypes.THERMOSTAT: "climate", + ServicesTypes.HEATER_COOLER: "climate", + ServicesTypes.SECURITY_SYSTEM: "alarm_control_panel", + ServicesTypes.GARAGE_DOOR_OPENER: "cover", + ServicesTypes.WINDOW: "cover", + ServicesTypes.WINDOW_COVERING: "cover", + ServicesTypes.LOCK_MECHANISM: "lock", + ServicesTypes.CONTACT_SENSOR: "binary_sensor", + ServicesTypes.MOTION_SENSOR: "binary_sensor", + ServicesTypes.CARBON_DIOXIDE_SENSOR: "sensor", + ServicesTypes.HUMIDITY_SENSOR: "sensor", + ServicesTypes.HUMIDIFIER_DEHUMIDIFIER: "humidifier", + ServicesTypes.LIGHT_SENSOR: "sensor", + ServicesTypes.TEMPERATURE_SENSOR: "sensor", + ServicesTypes.BATTERY_SERVICE: "sensor", + ServicesTypes.SMOKE_SENSOR: "binary_sensor", + ServicesTypes.CARBON_MONOXIDE_SENSOR: "binary_sensor", + ServicesTypes.LEAK_SENSOR: "binary_sensor", + ServicesTypes.FAN: "fan", + ServicesTypes.FAN_V2: "fan", + ServicesTypes.OCCUPANCY_SENSOR: "binary_sensor", + ServicesTypes.TELEVISION: "media_player", + ServicesTypes.VALVE: "switch", + ServicesTypes.CAMERA_RTP_STREAM_MANAGEMENT: "camera", } CHARACTERISTIC_PLATFORMS = { diff --git a/tests/components/homekit_controller/common.py b/tests/components/homekit_controller/common.py index ef77d5bc652..802c150b981 100644 --- a/tests/components/homekit_controller/common.py +++ b/tests/components/homekit_controller/common.py @@ -230,7 +230,7 @@ async def setup_test_component(hass, setup_accessory, capitalize=False, suffix=N domain = None for service in accessory.services: - service_name = ServicesTypes.get_short(service.type) + service_name = ServicesTypes.get_short_uuid(service.type) if service_name in HOMEKIT_ACCESSORY_DISPATCH: domain = HOMEKIT_ACCESSORY_DISPATCH[service_name] break