mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Add binary sensor platform to Tuya (#57623)
This commit is contained in:
parent
835e07f63e
commit
b2cef78d90
@ -1113,6 +1113,7 @@ omit =
|
|||||||
homeassistant/components/travisci/sensor.py
|
homeassistant/components/travisci/sensor.py
|
||||||
homeassistant/components/tuya/__init__.py
|
homeassistant/components/tuya/__init__.py
|
||||||
homeassistant/components/tuya/base.py
|
homeassistant/components/tuya/base.py
|
||||||
|
homeassistant/components/tuya/binary_sensor.py
|
||||||
homeassistant/components/tuya/climate.py
|
homeassistant/components/tuya/climate.py
|
||||||
homeassistant/components/tuya/const.py
|
homeassistant/components/tuya/const.py
|
||||||
homeassistant/components/tuya/fan.py
|
homeassistant/components/tuya/fan.py
|
||||||
|
98
homeassistant/components/tuya/binary_sensor.py
Normal file
98
homeassistant/components/tuya/binary_sensor.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
"""Support for Tuya binary sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from tuya_iot import TuyaDevice, TuyaDeviceManager
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
DEVICE_CLASS_DOOR,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import HomeAssistantTuyaData
|
||||||
|
from .base import TuyaHaEntity
|
||||||
|
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
|
||||||
|
|
||||||
|
# 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, ...]] = {
|
||||||
|
# Door Window Sensor
|
||||||
|
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48hm02l8m
|
||||||
|
"mcs": (
|
||||||
|
BinarySensorEntityDescription(
|
||||||
|
key=DPCode.DOORCONTACT_STATE,
|
||||||
|
device_class=DEVICE_CLASS_DOOR,
|
||||||
|
),
|
||||||
|
BinarySensorEntityDescription(
|
||||||
|
key=DPCode.TEMPER_ALARM,
|
||||||
|
name="Tamper",
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up Tuya binary sensor dynamically through Tuya discovery."""
|
||||||
|
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_discover_device(device_ids: list[str]) -> None:
|
||||||
|
"""Discover and add a discovered Tuya binary sensor."""
|
||||||
|
entities: list[TuyaBinarySensorEntity] = []
|
||||||
|
for device_id in device_ids:
|
||||||
|
device = hass_data.device_manager.device_map[device_id]
|
||||||
|
if descriptions := BINARY_SENSORS.get(device.category):
|
||||||
|
for description in descriptions:
|
||||||
|
if (
|
||||||
|
description.key in device.function
|
||||||
|
or description.key in device.status
|
||||||
|
):
|
||||||
|
entities.append(
|
||||||
|
TuyaBinarySensorEntity(
|
||||||
|
device, hass_data.device_manager, description
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
async_discover_device([*hass_data.device_manager.device_map])
|
||||||
|
|
||||||
|
entry.async_on_unload(
|
||||||
|
async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TuyaBinarySensorEntity(TuyaHaEntity, BinarySensorEntity):
|
||||||
|
"""Tuya Binary Sensor Entity."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: TuyaDevice,
|
||||||
|
device_manager: TuyaDeviceManager,
|
||||||
|
description: BinarySensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Init Tuya binary sensor."""
|
||||||
|
super().__init__(device, device_manager)
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = f"{super().unique_id}{description.key}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str | None:
|
||||||
|
"""Return Tuya device name."""
|
||||||
|
if self.entity_description.name is not None:
|
||||||
|
return f"{self.tuya_device.name} {self.entity_description.name}"
|
||||||
|
return self.tuya_device.name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return true if sensor is on."""
|
||||||
|
return self.tuya_device.status.get(self.entity_description.key, False)
|
@ -41,6 +41,7 @@ TUYA_SUPPORTED_PRODUCT_CATEGORIES = (
|
|||||||
"kj", # Air Purifier
|
"kj", # Air Purifier
|
||||||
"kj", # Air Purifier
|
"kj", # Air Purifier
|
||||||
"kt", # Air conditioner
|
"kt", # Air conditioner
|
||||||
|
"mcs", # Door Window Sensor
|
||||||
"pc", # Power Strip
|
"pc", # Power Strip
|
||||||
"qn", # Heater
|
"qn", # Heater
|
||||||
"wk", # Thermostat
|
"wk", # Thermostat
|
||||||
@ -52,7 +53,7 @@ TUYA_SUPPORTED_PRODUCT_CATEGORIES = (
|
|||||||
TUYA_SMART_APP = "tuyaSmart"
|
TUYA_SMART_APP = "tuyaSmart"
|
||||||
SMARTLIFE_APP = "smartlife"
|
SMARTLIFE_APP = "smartlife"
|
||||||
|
|
||||||
PLATFORMS = ["climate", "fan", "light", "scene", "switch"]
|
PLATFORMS = ["binary_sensor", "climate", "fan", "light", "scene", "switch"]
|
||||||
|
|
||||||
|
|
||||||
class DPCode(str, Enum):
|
class DPCode(str, Enum):
|
||||||
@ -67,6 +68,7 @@ class DPCode(str, Enum):
|
|||||||
CHILD_LOCK = "child_lock" # Child lock
|
CHILD_LOCK = "child_lock" # Child lock
|
||||||
COLOUR_DATA = "colour_data" # Colored light mode
|
COLOUR_DATA = "colour_data" # Colored light mode
|
||||||
COLOUR_DATA_V2 = "colour_data_v2" # Colored light mode
|
COLOUR_DATA_V2 = "colour_data_v2" # Colored light mode
|
||||||
|
DOORCONTACT_STATE = "doorcontact_state" # Status of door window sensor
|
||||||
FAN_DIRECTION = "fan_direction" # Fan direction
|
FAN_DIRECTION = "fan_direction" # Fan direction
|
||||||
FAN_SPEED_ENUM = "fan_speed_enum" # Speed mode
|
FAN_SPEED_ENUM = "fan_speed_enum" # Speed mode
|
||||||
FAN_SPEED_PERCENT = "fan_speed_percent" # Stepless speed
|
FAN_SPEED_PERCENT = "fan_speed_percent" # Stepless speed
|
||||||
@ -106,6 +108,7 @@ class DPCode(str, Enum):
|
|||||||
TEMP_SET_F = "temp_set_f" # Set the temperature in °F
|
TEMP_SET_F = "temp_set_f" # Set the temperature in °F
|
||||||
TEMP_UNIT_CONVERT = "temp_unit_convert" # Temperature unit switching
|
TEMP_UNIT_CONVERT = "temp_unit_convert" # Temperature unit switching
|
||||||
TEMP_VALUE = "temp_value" # Color temperature
|
TEMP_VALUE = "temp_value" # Color temperature
|
||||||
|
TEMPER_ALARM = "temper_alarm" # Tamper alarm
|
||||||
UV = "uv" # UV sterilization
|
UV = "uv" # UV sterilization
|
||||||
WARM = "warm" # Heat preservation
|
WARM = "warm" # Heat preservation
|
||||||
WATER_RESET = "water_reset" # Resetting of water usage days
|
WATER_RESET = "water_reset" # Resetting of water usage days
|
||||||
|
Loading…
x
Reference in New Issue
Block a user