mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 09:38:58 +01:00
Use external library wrapper in Tuya binary_sensor (#165465)
This commit is contained in:
@@ -5,11 +5,11 @@ from __future__ import annotations
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from tuya_device_handlers.device_wrapper.base import DeviceWrapper
|
from tuya_device_handlers.device_wrapper.base import DeviceWrapper
|
||||||
from tuya_device_handlers.device_wrapper.binary_sensor import DPCodeBitmapBitWrapper
|
from tuya_device_handlers.device_wrapper.binary_sensor import (
|
||||||
from tuya_device_handlers.device_wrapper.common import (
|
DPCodeBitmapBitWrapper,
|
||||||
DPCodeBooleanWrapper,
|
DPCodeInSetWrapper,
|
||||||
DPCodeWrapper,
|
|
||||||
)
|
)
|
||||||
|
from tuya_device_handlers.device_wrapper.common import DPCodeBooleanWrapper
|
||||||
from tuya_sharing import CustomerDevice, Manager
|
from tuya_sharing import CustomerDevice, Manager
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
@@ -376,29 +376,10 @@ BINARY_SENSORS: dict[DeviceCategory, tuple[TuyaBinarySensorEntityDescription, ..
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class _CustomDPCodeWrapper(DPCodeWrapper[bool]):
|
|
||||||
"""Custom DPCode Wrapper to check for values in a set."""
|
|
||||||
|
|
||||||
_valid_values: set[bool | float | int | str]
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, dpcode: str, valid_values: set[bool | float | int | str]
|
|
||||||
) -> None:
|
|
||||||
"""Init CustomDPCodeBooleanWrapper."""
|
|
||||||
super().__init__(dpcode)
|
|
||||||
self._valid_values = valid_values
|
|
||||||
|
|
||||||
def read_device_status(self, device: CustomerDevice) -> bool | None:
|
|
||||||
"""Read the device value for the dpcode."""
|
|
||||||
if (raw_value := device.status.get(self.dpcode)) is None:
|
|
||||||
return None
|
|
||||||
return raw_value in self._valid_values
|
|
||||||
|
|
||||||
|
|
||||||
def _get_dpcode_wrapper(
|
def _get_dpcode_wrapper(
|
||||||
device: CustomerDevice,
|
device: CustomerDevice,
|
||||||
description: TuyaBinarySensorEntityDescription,
|
description: TuyaBinarySensorEntityDescription,
|
||||||
) -> DPCodeWrapper | None:
|
) -> DeviceWrapper[bool] | None:
|
||||||
"""Get DPCode wrapper for an entity description."""
|
"""Get DPCode wrapper for an entity description."""
|
||||||
dpcode = description.dpcode or description.key
|
dpcode = description.dpcode or description.key
|
||||||
if description.bitmap_key is not None:
|
if description.bitmap_key is not None:
|
||||||
@@ -412,7 +393,7 @@ def _get_dpcode_wrapper(
|
|||||||
# Legacy / compatibility
|
# Legacy / compatibility
|
||||||
if dpcode not in device.status:
|
if dpcode not in device.status:
|
||||||
return None
|
return None
|
||||||
return _CustomDPCodeWrapper(
|
return DPCodeInSetWrapper(
|
||||||
dpcode,
|
dpcode,
|
||||||
description.on_value
|
description.on_value
|
||||||
if isinstance(description.on_value, set)
|
if isinstance(description.on_value, set)
|
||||||
|
|||||||
Reference in New Issue
Block a user