1 """Support for Tuya binary sensors."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
7 from tuya_sharing
import CustomerDevice, Manager
10 BinarySensorDeviceClass,
12 BinarySensorEntityDescription,
19 from .
import TuyaConfigEntry
20 from .const
import TUYA_DISCOVERY_NEW, DPCode
21 from .entity
import TuyaEntity
24 @dataclass(frozen=True)
26 """Describes a Tuya binary sensor."""
29 dpcode: DPCode |
None =
None
32 on_value: bool | float | int | str | set[bool | float | int | str] =
True
37 key=DPCode.TEMPER_ALARM,
39 device_class=BinarySensorDeviceClass.TAMPER,
40 entity_category=EntityCategory.DIAGNOSTIC,
48 BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = {
53 key=DPCode.GAS_SENSOR_STATE,
54 device_class=BinarySensorDeviceClass.GAS,
58 key=DPCode.CH4_SENSOR_STATE,
59 translation_key=
"methane",
60 device_class=BinarySensorDeviceClass.GAS,
65 translation_key=
"voc",
66 device_class=BinarySensorDeviceClass.SAFETY,
70 key=DPCode.PM25_STATE,
71 translation_key=
"pm25",
72 device_class=BinarySensorDeviceClass.SAFETY,
77 translation_key=
"carbon_monoxide",
78 device_class=BinarySensorDeviceClass.SAFETY,
83 translation_key=
"carbon_dioxide",
84 device_class=BinarySensorDeviceClass.SAFETY,
88 key=DPCode.CH2O_STATE,
89 translation_key=
"formaldehyde",
90 device_class=BinarySensorDeviceClass.SAFETY,
94 key=DPCode.DOORCONTACT_STATE,
95 device_class=BinarySensorDeviceClass.DOOR,
98 key=DPCode.WATERSENSOR_STATE,
99 device_class=BinarySensorDeviceClass.MOISTURE,
103 key=DPCode.PRESSURE_STATE,
104 translation_key=
"pressure",
108 key=DPCode.SMOKE_SENSOR_STATE,
109 device_class=BinarySensorDeviceClass.SMOKE,
112 TAMPER_BINARY_SENSOR,
118 key=DPCode.CO2_STATE,
119 device_class=BinarySensorDeviceClass.SAFETY,
122 TAMPER_BINARY_SENSOR,
129 device_class=BinarySensorDeviceClass.SAFETY,
133 key=DPCode.CO_STATUS,
134 device_class=BinarySensorDeviceClass.SAFETY,
137 TAMPER_BINARY_SENSOR,
143 key=DPCode.FEED_STATE,
144 translation_key=
"feeding",
152 key=DPCode.PRESENCE_STATE,
153 device_class=BinarySensorDeviceClass.OCCUPANCY,
154 on_value={
"presence",
"small_move",
"large_move",
"peaceful"},
161 key=DPCode.CH2O_STATE,
162 device_class=BinarySensorDeviceClass.SAFETY,
165 TAMPER_BINARY_SENSOR,
171 key=DPCode.CH4_SENSOR_STATE,
172 device_class=BinarySensorDeviceClass.GAS,
175 TAMPER_BINARY_SENSOR,
182 device_class=BinarySensorDeviceClass.DOOR,
183 on_value={
"open",
"opened"},
190 key=DPCode.DOORCONTACT_STATE,
191 device_class=BinarySensorDeviceClass.DOOR,
195 device_class=BinarySensorDeviceClass.DOOR,
197 TAMPER_BINARY_SENSOR,
203 key=DPCode.CLOSED_OPENED_KIT,
204 device_class=BinarySensorDeviceClass.LOCK,
212 key=DPCode.TEMPER_ALARM,
213 device_class=BinarySensorDeviceClass.TAMPER,
214 entity_category=EntityCategory.DIAGNOSTIC,
216 TAMPER_BINARY_SENSOR,
223 device_class=BinarySensorDeviceClass.MOTION,
226 TAMPER_BINARY_SENSOR,
232 key=DPCode.PM25_STATE,
233 device_class=BinarySensorDeviceClass.SAFETY,
236 TAMPER_BINARY_SENSOR,
242 key=DPCode.GAS_SENSOR_STATUS,
243 device_class=BinarySensorDeviceClass.GAS,
247 key=DPCode.GAS_SENSOR_STATE,
248 device_class=BinarySensorDeviceClass.GAS,
251 TAMPER_BINARY_SENSOR,
257 key=DPCode.WATERSENSOR_STATE,
258 device_class=BinarySensorDeviceClass.MOISTURE,
261 TAMPER_BINARY_SENSOR,
267 key=DPCode.SOS_STATE,
268 device_class=BinarySensorDeviceClass.SAFETY,
270 TAMPER_BINARY_SENSOR,
276 key=DPCode.VOC_STATE,
277 device_class=BinarySensorDeviceClass.SAFETY,
280 TAMPER_BINARY_SENSOR,
286 key=DPCode.WINDOW_STATE,
287 device_class=BinarySensorDeviceClass.WINDOW,
293 "wsdcg": (TAMPER_BINARY_SENSOR,),
298 key=DPCode.PRESSURE_STATE,
301 TAMPER_BINARY_SENSOR,
307 key=DPCode.SMOKE_SENSOR_STATUS,
308 device_class=BinarySensorDeviceClass.SMOKE,
312 key=DPCode.SMOKE_SENSOR_STATE,
313 device_class=BinarySensorDeviceClass.SMOKE,
314 on_value={
"1",
"alarm"},
316 TAMPER_BINARY_SENSOR,
322 key=f
"{DPCode.SHOCK_STATE}_vibration",
323 dpcode=DPCode.SHOCK_STATE,
324 device_class=BinarySensorDeviceClass.VIBRATION,
325 on_value=
"vibration",
328 key=f
"{DPCode.SHOCK_STATE}_drop",
329 dpcode=DPCode.SHOCK_STATE,
330 translation_key=
"drop",
334 key=f
"{DPCode.SHOCK_STATE}_tilt",
335 dpcode=DPCode.SHOCK_STATE,
336 translation_key=
"tilt",
344 hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
346 """Set up Tuya binary sensor dynamically through Tuya discovery."""
347 hass_data = entry.runtime_data
351 """Discover and add a discovered Tuya binary sensor."""
352 entities: list[TuyaBinarySensorEntity] = []
353 for device_id
in device_ids:
354 device = hass_data.manager.device_map[device_id]
355 if descriptions := BINARY_SENSORS.get(device.category):
356 for description
in descriptions:
357 dpcode = description.dpcode
or description.key
358 if dpcode
in device.status:
361 device, hass_data.manager, description
369 entry.async_on_unload(
375 """Tuya Binary Sensor Entity."""
377 entity_description: TuyaBinarySensorEntityDescription
381 device: CustomerDevice,
382 device_manager: Manager,
383 description: TuyaBinarySensorEntityDescription,
385 """Init Tuya binary sensor."""
386 super().
__init__(device, device_manager)
392 """Return true if sensor is on."""
394 if dpcode
not in self.
devicedevice.status:
None __init__(self, CustomerDevice device, Manager device_manager, TuyaBinarySensorEntityDescription description)
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
None async_setup_entry(HomeAssistant hass, TuyaConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)