From f800248c10d379ecee77058e7a231e8c326d937a Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 25 Jun 2025 10:33:13 +0300 Subject: [PATCH] Add more binary sensors to Alexa Devices (#146402) * Add more binary sensors to Amazon Devices * apply review comment * Add sensor platform to Amazon Devices * Revert "Add sensor platform to Amazon Devices" This reverts commit 25a9ca673e450634a17bdb79462b14aa855aca10. * clean * fix logic after latest changes * apply review comments --- .../components/alexa_devices/binary_sensor.py | 49 +++++++++++++++++-- .../components/alexa_devices/icons.json | 34 ++++++++++++- .../components/alexa_devices/strings.json | 15 ++++++ 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/alexa_devices/binary_sensor.py b/homeassistant/components/alexa_devices/binary_sensor.py index 16cf73aee9f..231f144dd89 100644 --- a/homeassistant/components/alexa_devices/binary_sensor.py +++ b/homeassistant/components/alexa_devices/binary_sensor.py @@ -7,6 +7,7 @@ from dataclasses import dataclass from typing import Final from aioamazondevices.api import AmazonDevice +from aioamazondevices.const import SENSOR_STATE_OFF from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, @@ -28,7 +29,8 @@ PARALLEL_UPDATES = 0 class AmazonBinarySensorEntityDescription(BinarySensorEntityDescription): """Alexa Devices binary sensor entity description.""" - is_on_fn: Callable[[AmazonDevice], bool] + is_on_fn: Callable[[AmazonDevice, str], bool] + is_supported: Callable[[AmazonDevice, str], bool] = lambda device, key: True BINARY_SENSORS: Final = ( @@ -36,13 +38,49 @@ BINARY_SENSORS: Final = ( key="online", device_class=BinarySensorDeviceClass.CONNECTIVITY, entity_category=EntityCategory.DIAGNOSTIC, - is_on_fn=lambda _device: _device.online, + is_on_fn=lambda device, _: device.online, ), AmazonBinarySensorEntityDescription( key="bluetooth", entity_category=EntityCategory.DIAGNOSTIC, translation_key="bluetooth", - is_on_fn=lambda _device: _device.bluetooth_state, + is_on_fn=lambda device, _: device.bluetooth_state, + ), + AmazonBinarySensorEntityDescription( + key="babyCryDetectionState", + translation_key="baby_cry_detection", + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, + ), + AmazonBinarySensorEntityDescription( + key="beepingApplianceDetectionState", + translation_key="beeping_appliance_detection", + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, + ), + AmazonBinarySensorEntityDescription( + key="coughDetectionState", + translation_key="cough_detection", + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, + ), + AmazonBinarySensorEntityDescription( + key="dogBarkDetectionState", + translation_key="dog_bark_detection", + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, + ), + AmazonBinarySensorEntityDescription( + key="humanPresenceDetectionState", + device_class=BinarySensorDeviceClass.MOTION, + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, + ), + AmazonBinarySensorEntityDescription( + key="waterSoundsDetectionState", + translation_key="water_sounds_detection", + is_on_fn=lambda device, key: (device.sensors[key].value != SENSOR_STATE_OFF), + is_supported=lambda device, key: device.sensors.get(key) is not None, ), ) @@ -60,6 +98,7 @@ async def async_setup_entry( AmazonBinarySensorEntity(coordinator, serial_num, sensor_desc) for sensor_desc in BINARY_SENSORS for serial_num in coordinator.data + if sensor_desc.is_supported(coordinator.data[serial_num], sensor_desc.key) ) @@ -71,4 +110,6 @@ class AmazonBinarySensorEntity(AmazonEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return True if the binary sensor is on.""" - return self.entity_description.is_on_fn(self.device) + return self.entity_description.is_on_fn( + self.device, self.entity_description.key + ) diff --git a/homeassistant/components/alexa_devices/icons.json b/homeassistant/components/alexa_devices/icons.json index e3b20eb2c4a..492f89b8fe4 100644 --- a/homeassistant/components/alexa_devices/icons.json +++ b/homeassistant/components/alexa_devices/icons.json @@ -2,9 +2,39 @@ "entity": { "binary_sensor": { "bluetooth": { - "default": "mdi:bluetooth", + "default": "mdi:bluetooth-off", "state": { - "off": "mdi:bluetooth-off" + "on": "mdi:bluetooth" + } + }, + "baby_cry_detection": { + "default": "mdi:account-voice-off", + "state": { + "on": "mdi:account-voice" + } + }, + "beeping_appliance_detection": { + "default": "mdi:bell-off", + "state": { + "on": "mdi:bell-ring" + } + }, + "cough_detection": { + "default": "mdi:blur-off", + "state": { + "on": "mdi:blur" + } + }, + "dog_bark_detection": { + "default": "mdi:dog-side-off", + "state": { + "on": "mdi:dog-side" + } + }, + "water_sounds_detection": { + "default": "mdi:water-pump-off", + "state": { + "on": "mdi:water-pump" } } } diff --git a/homeassistant/components/alexa_devices/strings.json b/homeassistant/components/alexa_devices/strings.json index 9d615b248ed..eb279e28d35 100644 --- a/homeassistant/components/alexa_devices/strings.json +++ b/homeassistant/components/alexa_devices/strings.json @@ -41,6 +41,21 @@ "binary_sensor": { "bluetooth": { "name": "Bluetooth" + }, + "baby_cry_detection": { + "name": "Baby crying" + }, + "beeping_appliance_detection": { + "name": "Beeping appliance" + }, + "cough_detection": { + "name": "Coughing" + }, + "dog_bark_detection": { + "name": "Dog barking" + }, + "water_sounds_detection": { + "name": "Water sounds" } }, "notify": {