Add Vibration Sensor (zd) device support to Tuya (#57795)

This commit is contained in:
Franck Nijhof 2021-10-17 20:56:15 +02:00 committed by GitHub
parent f390812183
commit 4fd8b27ce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 10 deletions

View file

@ -9,6 +9,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_DOOR, DEVICE_CLASS_DOOR,
DEVICE_CLASS_MOTION, DEVICE_CLASS_MOTION,
DEVICE_CLASS_SAFETY, DEVICE_CLASS_SAFETY,
DEVICE_CLASS_VIBRATION,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
@ -27,6 +28,10 @@ from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription): class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes a Tuya binary sensor.""" """Describes a Tuya binary sensor."""
# DPCode, to use. If None, the key will be used as DPCode
dpcode: DPCode | None = None
# Value to consider binary sensor to be "on"
on_value: bool | float | int | str = True on_value: bool | float | int | str = True
@ -84,6 +89,31 @@ BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = {
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
), ),
# Vibration Sensor
# https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno
"zd": (
TuyaBinarySensorEntityDescription(
key=f"{DPCode.SHOCK_STATE}_vibration",
dpcode=DPCode.SHOCK_STATE,
name="Vibration",
device_class=DEVICE_CLASS_VIBRATION,
on_value="vibration",
),
TuyaBinarySensorEntityDescription(
key=f"{DPCode.SHOCK_STATE}_drop",
dpcode=DPCode.SHOCK_STATE,
name="Drop",
icon="mdi:icon=package-down",
on_value="drop",
),
TuyaBinarySensorEntityDescription(
key=f"{DPCode.SHOCK_STATE}_tilt",
dpcode=DPCode.SHOCK_STATE,
name="Tilt",
icon="mdi:spirit-level",
on_value="tilt",
),
),
} }
@ -101,10 +131,8 @@ async def async_setup_entry(
device = hass_data.device_manager.device_map[device_id] device = hass_data.device_manager.device_map[device_id]
if descriptions := BINARY_SENSORS.get(device.category): if descriptions := BINARY_SENSORS.get(device.category):
for description in descriptions: for description in descriptions:
if ( dpcode = description.dpcode or description.key
description.key in device.function if dpcode in device.status:
or description.key in device.status
):
entities.append( entities.append(
TuyaBinarySensorEntity( TuyaBinarySensorEntity(
device, hass_data.device_manager, description device, hass_data.device_manager, description
@ -139,9 +167,7 @@ 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."""
if self.entity_description.key not in self.device.status: dpcode = self.entity_description.dpcode or self.entity_description.key
if dpcode not in self.device.status:
return False return False
return ( return self.device.status[dpcode] == self.entity_description.on_value
self.device.status[self.entity_description.key]
== self.entity_description.on_value
)

View file

@ -111,6 +111,7 @@ TUYA_SUPPORTED_PRODUCT_CATEGORIES = (
"wk", # Thermostat "wk", # Thermostat
"xdd", # Ceiling Light "xdd", # Ceiling Light
"xxj", # Diffuser "xxj", # Diffuser
"zd", # Vibration Sensor
) )
TUYA_SMART_APP = "tuyaSmart" TUYA_SMART_APP = "tuyaSmart"
@ -173,9 +174,11 @@ class DPCode(str, Enum):
POWDER_SET = "powder_set" # Powder POWDER_SET = "powder_set" # Powder
PUMP_RESET = "pump_reset" # Water pump reset PUMP_RESET = "pump_reset" # Water pump reset
RECORD_SWITCH = "record_switch" # Recording switch RECORD_SWITCH = "record_switch" # Recording switch
SENSITIVITY = "sensitivity" # Sensitivity
SHAKE = "shake" # Oscillating SHAKE = "shake" # Oscillating
SOS = "sos" # Emergency State SOS = "sos" # Emergency State
SOS_STATE = "sos_state" # Emergency mode SOS_STATE = "sos_state" # Emergency mode
SHOCK_STATE = "shock_state" # Vibration status
SPEED = "speed" # Speed level SPEED = "speed" # Speed level
START = "start" # Start START = "start" # Start
SWING = "swing" # Swing mode SWING = "swing" # Swing mode

View file

@ -57,6 +57,15 @@ NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
), ),
), ),
# Vibration Sensor
# https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno
"zd": (
NumberEntityDescription(
key=DPCode.SENSITIVITY,
name="Sensitivity",
entity_category=ENTITY_CATEGORY_CONFIG,
),
),
} }

View file

@ -53,11 +53,13 @@ SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = {
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_BATTERY, device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
SensorEntityDescription( SensorEntityDescription(
key=DPCode.BATTERY_STATE, key=DPCode.BATTERY_STATE,
name="Battery State", name="Battery State",
entity_registry_enabled_default=False, icon="mdi:battery",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
), ),
# Switch # Switch
@ -146,6 +148,25 @@ SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = {
SensorEntityDescription( SensorEntityDescription(
key=DPCode.BATTERY_STATE, key=DPCode.BATTERY_STATE,
name="Battery State", name="Battery State",
icon="mdi:battery",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
),
# Vibration Sensor
# https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno
"zd": (
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",
icon="mdi:battery",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
), ),
), ),