Move Tuya get_dptype method out of base entity

This commit is contained in:
epenet
2025-08-06 13:46:36 +00:00
parent 260ea9a3be
commit aa58d49903
4 changed files with 35 additions and 38 deletions

View File

@@ -13,16 +13,6 @@ from homeassistant.helpers.entity import Entity
from .const import DOMAIN, LOGGER, TUYA_HA_SIGNAL_UPDATE_ENTITY, DPCode, DPType
from .models import EnumTypeData, IntegerTypeData
_DPTYPE_MAPPING: dict[str, DPType] = {
"bitmap": DPType.BITMAP,
"bool": DPType.BOOLEAN,
"enum": DPType.ENUM,
"json": DPType.JSON,
"raw": DPType.RAW,
"string": DPType.STRING,
"value": DPType.INTEGER,
}
class TuyaEntity(Entity):
"""Tuya base device."""
@@ -125,28 +115,6 @@ class TuyaEntity(Entity):
return None
def get_dptype(
self, dpcode: DPCode | None, prefer_function: bool = False
) -> DPType | None:
"""Find a matching DPCode data type available on for this device."""
if dpcode is None:
return None
order = ["status_range", "function"]
if prefer_function:
order = ["function", "status_range"]
for key in order:
if dpcode in getattr(self.device, key):
current_type = getattr(self.device, key)[dpcode].type
try:
return DPType(current_type)
except ValueError:
# Sometimes, we get ill-formed DPTypes from the cloud,
# this fixes them and maps them to the correct DPType.
return _DPTYPE_MAPPING.get(current_type)
return None
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
self.async_on_remove(

View File

@@ -29,7 +29,7 @@ from . import TuyaConfigEntry
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType, WorkMode
from .entity import TuyaEntity
from .models import IntegerTypeData
from .util import get_dpcode, remap_value
from .util import get_dpcode, get_dptype, remap_value
@dataclass
@@ -529,9 +529,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
description.brightness_min, dptype=DPType.INTEGER
)
if (
dpcode := get_dpcode(self.device, description.color_data)
) and self.get_dptype(dpcode) == DPType.JSON:
if (dpcode := get_dpcode(self.device, description.color_data)) and get_dptype(
self.device, dpcode
) == DPType.JSON:
self._color_data_dpcode = dpcode
color_modes.add(ColorMode.HS)
if dpcode in self.device.function:

View File

@@ -41,6 +41,7 @@ from .const import (
)
from .entity import TuyaEntity
from .models import ComplexTypeData, ElectricityTypeData, EnumTypeData, IntegerTypeData
from .util import get_dptype
@dataclass(frozen=True)
@@ -1531,7 +1532,7 @@ class TuyaSensorEntity(TuyaEntity, SensorEntity):
self._type_data = enum_type
self._type = DPType.ENUM
else:
self._type = self.get_dptype(DPCode(description.key))
self._type = get_dptype(self.device, DPCode(description.key))
# Logic to ensure the set device class and API received Unit Of Measurement
# match Home Assistants requirements.

View File

@@ -6,7 +6,17 @@ from tuya_sharing import CustomerDevice
from homeassistant.exceptions import ServiceValidationError
from .const import DOMAIN, DPCode
from .const import DOMAIN, DPCode, DPType
_DPTYPE_MAPPING: dict[str, DPType] = {
"bitmap": DPType.BITMAP,
"bool": DPType.BOOLEAN,
"enum": DPType.ENUM,
"json": DPType.JSON,
"raw": DPType.RAW,
"string": DPType.STRING,
"value": DPType.INTEGER,
}
def get_dpcode(
@@ -32,6 +42,24 @@ def get_dpcode(
return None
def get_dptype(device: CustomerDevice, dpcode: DPCode | None) -> DPType | None:
"""Find a matching DPType type information for this device DPCode."""
if dpcode is None:
return None
for device_specs in (device.status_range, device.function):
if current_definition := device_specs.get(dpcode):
current_type = current_definition.type
try:
return DPType(current_type)
except ValueError:
# Sometimes, we get ill-formed DPTypes from the cloud,
# this fixes them and maps them to the correct DPType.
return _DPTYPE_MAPPING.get(current_type)
return None
def remap_value(
value: float,
from_min: float = 0,