From 32099ea38a9906d3e96f756002098aa42c02a519 Mon Sep 17 00:00:00 2001 From: Jc2k Date: Mon, 24 Jan 2022 02:49:40 +0000 Subject: [PATCH] Easier identification of devices with homekit_controller (#64804) --- .../components/homekit_controller/button.py | 17 +++++++- .../components/homekit_controller/const.py | 1 + .../specific_devices/test_haa_fan.py | 4 +- .../specific_devices/test_koogeek_ls1.py | 8 ++++ .../homekit_controller/test_device_trigger.py | 27 ++++++++++-- .../homekit_controller/test_diagnostics.py | 42 ++++++++++++++++++- .../homekit_controller/test_sensor.py | 2 + 7 files changed, 93 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/homekit_controller/button.py b/homeassistant/components/homekit_controller/button.py index b1359028ec3..efb13fc3496 100644 --- a/homeassistant/components/homekit_controller/button.py +++ b/homeassistant/components/homekit_controller/button.py @@ -45,8 +45,23 @@ BUTTON_ENTITIES: dict[str, HomeKitButtonEntityDescription] = { entity_category=EntityCategory.CONFIG, write_value="#HAA@trcmd", ), + CharacteristicsTypes.IDENTIFY: HomeKitButtonEntityDescription( + key=CharacteristicsTypes.IDENTIFY, + name="Identify", + entity_category=EntityCategory.DIAGNOSTIC, + write_value=True, + ), } +# For legacy reasons, "built-in" characteristic types are in their short form +# And vendor types don't have a short form +# This means long and short forms get mixed up in this dict, and comparisons +# don't work! +# We call get_uuid on *every* type to normalise them to the long form +# Eventually aiohomekit will use the long form exclusively amd this can be removed. +for k, v in list(BUTTON_ENTITIES.items()): + BUTTON_ENTITIES[CharacteristicsTypes.get_uuid(k)] = BUTTON_ENTITIES.pop(k) + async def async_setup_entry( hass: HomeAssistant, @@ -92,7 +107,7 @@ class HomeKitButton(CharacteristicEntity, ButtonEntity): def name(self) -> str: """Return the name of the device if any.""" if name := super().name: - return f"{name} - {self.entity_description.name}" + return f"{name} {self.entity_description.name}" return f"{self.entity_description.name}" async def async_press(self) -> None: diff --git a/homeassistant/components/homekit_controller/const.py b/homeassistant/components/homekit_controller/const.py index ff5c0a6f20a..d0dfa9bad4f 100644 --- a/homeassistant/components/homekit_controller/const.py +++ b/homeassistant/components/homekit_controller/const.py @@ -74,6 +74,7 @@ CHARACTERISTIC_PLATFORMS = { CharacteristicsTypes.DENSITY_NO2: "sensor", CharacteristicsTypes.DENSITY_SO2: "sensor", CharacteristicsTypes.DENSITY_VOC: "sensor", + CharacteristicsTypes.IDENTIFY: "button", } # For legacy reasons, "built-in" characteristic types are in their short form diff --git a/tests/components/homekit_controller/specific_devices/test_haa_fan.py b/tests/components/homekit_controller/specific_devices/test_haa_fan.py index e51bec194ed..ae67bda2000 100644 --- a/tests/components/homekit_controller/specific_devices/test_haa_fan.py +++ b/tests/components/homekit_controller/specific_devices/test_haa_fan.py @@ -64,14 +64,14 @@ async def test_haa_fan_setup(hass): ), EntityTestInfo( entity_id="button.haa_c718b3_setup", - friendly_name="HAA-C718B3 - Setup", + friendly_name="HAA-C718B3 Setup", unique_id="homekit-C718B3-1-aid:1-sid:1010-cid:1012", entity_category=EntityCategory.CONFIG, state="unknown", ), EntityTestInfo( entity_id="button.haa_c718b3_update", - friendly_name="HAA-C718B3 - Update", + friendly_name="HAA-C718B3 Update", unique_id="homekit-C718B3-1-aid:1-sid:1010-cid:1011", entity_category=EntityCategory.CONFIG, state="unknown", diff --git a/tests/components/homekit_controller/specific_devices/test_koogeek_ls1.py b/tests/components/homekit_controller/specific_devices/test_koogeek_ls1.py index f5c09342402..9591eb27b6f 100644 --- a/tests/components/homekit_controller/specific_devices/test_koogeek_ls1.py +++ b/tests/components/homekit_controller/specific_devices/test_koogeek_ls1.py @@ -8,6 +8,7 @@ from aiohomekit.testing import FakePairing import pytest from homeassistant.components.light import SUPPORT_BRIGHTNESS, SUPPORT_COLOR +from homeassistant.helpers.entity import EntityCategory import homeassistant.util.dt as dt_util from tests.common import async_fire_time_changed @@ -49,6 +50,13 @@ async def test_koogeek_ls1_setup(hass): capabilities={"supported_color_modes": ["hs"]}, state="off", ), + EntityTestInfo( + entity_id="button.koogeek_ls1_20833f_identify", + friendly_name="Koogeek-LS1-20833F Identify", + unique_id="homekit-AAAA011111111111-aid:1-sid:1-cid:6", + entity_category=EntityCategory.DIAGNOSTIC, + state="unknown", + ), ], ), ) diff --git a/tests/components/homekit_controller/test_device_trigger.py b/tests/components/homekit_controller/test_device_trigger.py index 8541bbd2fe6..7a842470bd5 100644 --- a/tests/components/homekit_controller/test_device_trigger.py +++ b/tests/components/homekit_controller/test_device_trigger.py @@ -97,7 +97,14 @@ async def test_enumerate_remote(hass, utcnow): "entity_id": "sensor.testdevice_battery", "platform": "device", "type": "battery_level", - } + }, + { + "device_id": device.id, + "domain": "button", + "entity_id": "button.testdevice_identify", + "platform": "device", + "type": "pressed", + }, ] for button in ("button1", "button2", "button3", "button4"): @@ -135,7 +142,14 @@ async def test_enumerate_button(hass, utcnow): "entity_id": "sensor.testdevice_battery", "platform": "device", "type": "battery_level", - } + }, + { + "device_id": device.id, + "domain": "button", + "entity_id": "button.testdevice_identify", + "platform": "device", + "type": "pressed", + }, ] for subtype in ("single_press", "double_press", "long_press"): @@ -172,7 +186,14 @@ async def test_enumerate_doorbell(hass, utcnow): "entity_id": "sensor.testdevice_battery", "platform": "device", "type": "battery_level", - } + }, + { + "device_id": device.id, + "domain": "button", + "entity_id": "button.testdevice_identify", + "platform": "device", + "type": "pressed", + }, ] for subtype in ("single_press", "double_press", "long_press"): diff --git a/tests/components/homekit_controller/test_diagnostics.py b/tests/components/homekit_controller/test_diagnostics.py index 292d657257f..86b3ed42ce3 100644 --- a/tests/components/homekit_controller/test_diagnostics.py +++ b/tests/components/homekit_controller/test_diagnostics.py @@ -234,6 +234,26 @@ async def test_config_entry(hass: HomeAssistant, hass_client: ClientSession, utc "sw_version": "2.2.15", "hw_version": "", "entities": [ + { + "device_class": None, + "disabled": False, + "disabled_by": None, + "entity_category": "diagnostic", + "icon": None, + "original_device_class": None, + "original_icon": None, + "original_name": "Koogeek-LS1-20833F Identify", + "state": { + "attributes": { + "friendly_name": "Koogeek-LS1-20833F Identify" + }, + "entity_id": "button.koogeek_ls1_20833f_identify", + "last_changed": "2023-01-01T00:00:00+00:00", + "last_updated": "2023-01-01T00:00:00+00:00", + "state": "unknown", + }, + "unit_of_measurement": None, + }, { "original_name": "Koogeek-LS1-20833F", "disabled": False, @@ -255,7 +275,7 @@ async def test_config_entry(hass: HomeAssistant, hass_client: ClientSession, utc "last_changed": "2023-01-01T00:00:00+00:00", "last_updated": "2023-01-01T00:00:00+00:00", }, - } + }, ], } ], @@ -484,6 +504,24 @@ async def test_device(hass: HomeAssistant, hass_client: ClientSession, utcnow): "sw_version": "2.2.15", "hw_version": "", "entities": [ + { + "device_class": None, + "disabled": False, + "disabled_by": None, + "entity_category": "diagnostic", + "icon": None, + "original_device_class": None, + "original_icon": None, + "original_name": "Koogeek-LS1-20833F Identify", + "state": { + "attributes": {"friendly_name": "Koogeek-LS1-20833F Identify"}, + "entity_id": "button.koogeek_ls1_20833f_identify", + "last_changed": "2023-01-01T00:00:00+00:00", + "last_updated": "2023-01-01T00:00:00+00:00", + "state": "unknown", + }, + "unit_of_measurement": None, + }, { "original_name": "Koogeek-LS1-20833F", "disabled": False, @@ -505,7 +543,7 @@ async def test_device(hass: HomeAssistant, hass_client: ClientSession, utcnow): "last_changed": "2023-01-01T00:00:00+00:00", "last_updated": "2023-01-01T00:00:00+00:00", }, - } + }, ], }, } diff --git a/tests/components/homekit_controller/test_sensor.py b/tests/components/homekit_controller/test_sensor.py index 426572457d0..c50e23bac13 100644 --- a/tests/components/homekit_controller/test_sensor.py +++ b/tests/components/homekit_controller/test_sensor.py @@ -89,6 +89,8 @@ async def test_temperature_sensor_not_added_twice(hass, utcnow): ) for state in hass.states.async_all(): + if state.entity_id.startswith("button"): + continue assert state.entity_id == helper.entity_id