1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 08:26:41 +01:00

Migrate Tuya binary sensor to TuyaBinarySensorDefinition (#166330)

This commit is contained in:
epenet
2026-03-24 14:48:16 +01:00
committed by GitHub
parent ca63f299ff
commit d3a8a7e9be

View File

@@ -4,12 +4,10 @@ from __future__ import annotations
from dataclasses import dataclass
from tuya_device_handlers.device_wrapper.base import DeviceWrapper
from tuya_device_handlers.device_wrapper.binary_sensor import (
DPCodeBitmapBitWrapper,
DPCodeInSetWrapper,
from tuya_device_handlers.definition.binary_sensor import (
TuyaBinarySensorDefinition,
get_default_definition,
)
from tuya_device_handlers.device_wrapper.common import DPCodeBooleanWrapper
from tuya_sharing import CustomerDevice, Manager
from homeassistant.components.binary_sensor import (
@@ -381,31 +379,6 @@ BINARY_SENSORS: dict[DeviceCategory, tuple[TuyaBinarySensorEntityDescription, ..
}
def _get_dpcode_wrapper(
device: CustomerDevice,
description: TuyaBinarySensorEntityDescription,
) -> DeviceWrapper[bool] | None:
"""Get DPCode wrapper for an entity description."""
dpcode = description.dpcode or description.key
if description.bitmap_key is not None:
return DPCodeBitmapBitWrapper.find_dpcode(
device, dpcode, bitmap_key=description.bitmap_key
)
if bool_type := DPCodeBooleanWrapper.find_dpcode(device, dpcode):
return bool_type
# Legacy / compatibility
if dpcode not in device.status:
return None
return DPCodeInSetWrapper(
dpcode,
description.on_value
if isinstance(description.on_value, set)
else {description.on_value},
)
async def async_setup_entry(
hass: HomeAssistant,
entry: TuyaConfigEntry,
@@ -422,9 +395,16 @@ async def async_setup_entry(
device = manager.device_map[device_id]
if descriptions := BINARY_SENSORS.get(device.category):
entities.extend(
TuyaBinarySensorEntity(device, manager, description, dpcode_wrapper)
TuyaBinarySensorEntity(device, manager, description, definition)
for description in descriptions
if (dpcode_wrapper := _get_dpcode_wrapper(device, description))
if (
definition := get_default_definition(
device,
description.dpcode or description.key,
description.bitmap_key,
description.on_value,
)
)
)
async_add_entities(entities)
@@ -446,11 +426,11 @@ class TuyaBinarySensorEntity(TuyaEntity, BinarySensorEntity):
device: CustomerDevice,
device_manager: Manager,
description: TuyaBinarySensorEntityDescription,
dpcode_wrapper: DeviceWrapper[bool],
definition: TuyaBinarySensorDefinition,
) -> None:
"""Init Tuya binary sensor."""
super().__init__(device, device_manager, description)
self._dpcode_wrapper = dpcode_wrapper
self._dpcode_wrapper = definition.binary_sensor_wrapper
@property
def is_on(self) -> bool | None: