diff --git a/homeassistant/components/tuya/binary_sensor.py b/homeassistant/components/tuya/binary_sensor.py index 0a6f0aed053..a00e4e19e1a 100644 --- a/homeassistant/components/tuya/binary_sensor.py +++ b/homeassistant/components/tuya/binary_sensor.py @@ -1,14 +1,18 @@ """Support for Tuya binary sensors.""" from __future__ import annotations +from dataclasses import dataclass + from tuya_iot import TuyaDevice, TuyaDeviceManager from homeassistant.components.binary_sensor import ( DEVICE_CLASS_DOOR, + DEVICE_CLASS_MOTION, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -17,24 +21,46 @@ from . import HomeAssistantTuyaData from .base import TuyaEntity from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode + +@dataclass +class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes a Tuya binary sensor.""" + + on_value: bool | float | int | str = True + + # All descriptions can be found here. Mostly the Boolean data types in the # default status set of each category (that don't have a set instruction) # end up being a binary sensor. # https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq -BINARY_SENSORS: dict[str, tuple[BinarySensorEntityDescription, ...]] = { +BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = { # Door Window Sensor # https://developer.tuya.com/en/docs/iot/s?id=K9gf48hm02l8m "mcs": ( - BinarySensorEntityDescription( + TuyaBinarySensorEntityDescription( key=DPCode.DOORCONTACT_STATE, device_class=DEVICE_CLASS_DOOR, ), - BinarySensorEntityDescription( + TuyaBinarySensorEntityDescription( key=DPCode.TEMPER_ALARM, name="Tamper", entity_registry_enabled_default=False, ), ), + # PIR Detector + # https://developer.tuya.com/en/docs/iot/categorypir?id=Kaiuz3ss11b80 + "pir": ( + TuyaBinarySensorEntityDescription( + key=DPCode.PIR, + device_class=DEVICE_CLASS_MOTION, + on_value="pir", + ), + TuyaBinarySensorEntityDescription( + key=DPCode.TEMPER_ALARM, + name="Tamper", + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + ), } @@ -74,11 +100,13 @@ async def async_setup_entry( class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity): """Tuya Binary Sensor Entity.""" + entity_description: TuyaBinarySensorEntityDescription + def __init__( self, device: TuyaDevice, device_manager: TuyaDeviceManager, - description: BinarySensorEntityDescription, + description: TuyaBinarySensorEntityDescription, ) -> None: """Init Tuya binary sensor.""" super().__init__(device, device_manager) @@ -88,4 +116,9 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return true if sensor is on.""" - return self.device.status.get(self.entity_description.key, False) + if self.entity_description.key not in self.device.status: + return False + return ( + self.device.status[self.entity_description.key] + == self.entity_description.on_value + ) diff --git a/homeassistant/components/tuya/const.py b/homeassistant/components/tuya/const.py index 0bc71a478b4..f25dacaab37 100644 --- a/homeassistant/components/tuya/const.py +++ b/homeassistant/components/tuya/const.py @@ -37,13 +37,13 @@ TUYA_SUPPORTED_PRODUCT_CATEGORIES = ( "fs", # Fan "fwl", # Ambient light "jsq", # Humidifier's light - "kfj", # Coffee Maker + "kfj", # Coffee maker "kg", # Switch "kj", # Air Purifier - "kfj", # Coffee maker "kt", # Air conditioner "mcs", # Door Window Sensor "pc", # Power Strip + "pir", # PIR Detector "qn", # Heater "wk", # Thermostat "xdd", # Ceiling Light @@ -96,6 +96,7 @@ class DPCode(str, Enum): LOCK = "lock" # Lock / Child lock MATERIAL = "material" # Material MODE = "mode" # Working mode / Mode + PIR = "pir" # Motion sensor POWDER_SET = "powder_set" # Powder PUMP_RESET = "pump_reset" # Water pump reset SHAKE = "shake" # Oscillating diff --git a/homeassistant/components/tuya/sensor.py b/homeassistant/components/tuya/sensor.py index 8d36a9b1207..0d6d179ab76 100644 --- a/homeassistant/components/tuya/sensor.py +++ b/homeassistant/components/tuya/sensor.py @@ -17,6 +17,7 @@ from homeassistant.const import ( DEVICE_CLASS_CURRENT, DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, + ENTITY_CATEGORY_DIAGNOSTIC, PERCENTAGE, ) from homeassistant.core import HomeAssistant, callback @@ -74,6 +75,23 @@ SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = { entity_registry_enabled_default=False, ), ), + # PIR Detector + # https://developer.tuya.com/en/docs/iot/categorypir?id=Kaiuz3ss11b80 + "pir": ( + SensorEntityDescription( + key=DPCode.BATTERY_PERCENTAGE, + name="Battery", + native_unit_of_measurement=PERCENTAGE, + device_class=DEVICE_CLASS_BATTERY, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + SensorEntityDescription( + key=DPCode.BATTERY_STATE, + name="Battery State", + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + ), } # Socket (duplicate of `kg`)