Add PIR Detector (pir) device support to Tuya (#57784)

This commit is contained in:
Franck Nijhof 2021-10-15 19:11:06 +02:00 committed by GitHub
parent 19443b474c
commit 892bf62dd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 7 deletions

View file

@ -1,14 +1,18 @@
"""Support for Tuya binary sensors.""" """Support for Tuya binary sensors."""
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from tuya_iot import TuyaDevice, TuyaDeviceManager from tuya_iot import TuyaDevice, TuyaDeviceManager
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_DOOR, DEVICE_CLASS_DOOR,
DEVICE_CLASS_MOTION,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -17,24 +21,46 @@ from . import HomeAssistantTuyaData
from .base import TuyaEntity from .base import TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode 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 # 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) # default status set of each category (that don't have a set instruction)
# end up being a binary sensor. # end up being a binary sensor.
# https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq # 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 # Door Window Sensor
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48hm02l8m # https://developer.tuya.com/en/docs/iot/s?id=K9gf48hm02l8m
"mcs": ( "mcs": (
BinarySensorEntityDescription( TuyaBinarySensorEntityDescription(
key=DPCode.DOORCONTACT_STATE, key=DPCode.DOORCONTACT_STATE,
device_class=DEVICE_CLASS_DOOR, device_class=DEVICE_CLASS_DOOR,
), ),
BinarySensorEntityDescription( TuyaBinarySensorEntityDescription(
key=DPCode.TEMPER_ALARM, key=DPCode.TEMPER_ALARM,
name="Tamper", name="Tamper",
entity_registry_enabled_default=False, 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): class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity):
"""Tuya Binary Sensor Entity.""" """Tuya Binary Sensor Entity."""
entity_description: TuyaBinarySensorEntityDescription
def __init__( def __init__(
self, self,
device: TuyaDevice, device: TuyaDevice,
device_manager: TuyaDeviceManager, device_manager: TuyaDeviceManager,
description: BinarySensorEntityDescription, description: TuyaBinarySensorEntityDescription,
) -> None: ) -> None:
"""Init Tuya binary sensor.""" """Init Tuya binary sensor."""
super().__init__(device, device_manager) super().__init__(device, device_manager)
@ -88,4 +116,9 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if sensor is on.""" """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
)

View file

@ -37,13 +37,13 @@ TUYA_SUPPORTED_PRODUCT_CATEGORIES = (
"fs", # Fan "fs", # Fan
"fwl", # Ambient light "fwl", # Ambient light
"jsq", # Humidifier's light "jsq", # Humidifier's light
"kfj", # Coffee Maker "kfj", # Coffee maker
"kg", # Switch "kg", # Switch
"kj", # Air Purifier "kj", # Air Purifier
"kfj", # Coffee maker
"kt", # Air conditioner "kt", # Air conditioner
"mcs", # Door Window Sensor "mcs", # Door Window Sensor
"pc", # Power Strip "pc", # Power Strip
"pir", # PIR Detector
"qn", # Heater "qn", # Heater
"wk", # Thermostat "wk", # Thermostat
"xdd", # Ceiling Light "xdd", # Ceiling Light
@ -96,6 +96,7 @@ class DPCode(str, Enum):
LOCK = "lock" # Lock / Child lock LOCK = "lock" # Lock / Child lock
MATERIAL = "material" # Material MATERIAL = "material" # Material
MODE = "mode" # Working mode / Mode MODE = "mode" # Working mode / Mode
PIR = "pir" # Motion sensor
POWDER_SET = "powder_set" # Powder POWDER_SET = "powder_set" # Powder
PUMP_RESET = "pump_reset" # Water pump reset PUMP_RESET = "pump_reset" # Water pump reset
SHAKE = "shake" # Oscillating SHAKE = "shake" # Oscillating

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
DEVICE_CLASS_CURRENT, DEVICE_CLASS_CURRENT,
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
DEVICE_CLASS_VOLTAGE, DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
PERCENTAGE, PERCENTAGE,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -74,6 +75,23 @@ SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = {
entity_registry_enabled_default=False, 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`) # Socket (duplicate of `kg`)