Handle empty electricity RAW sensors in Tuya (#150406)

This commit is contained in:
epenet
2025-08-11 11:55:47 +02:00
committed by Franck Nijhof
parent 6f5d72fd81
commit 5fdd04b860
2 changed files with 7 additions and 4 deletions

View File

@@ -108,7 +108,7 @@ class ComplexTypeData:
raise NotImplementedError("from_json is not implemented for this type") raise NotImplementedError("from_json is not implemented for this type")
@classmethod @classmethod
def from_raw(cls, data: str) -> Self: def from_raw(cls, data: str) -> Self | None:
"""Decode base64 string and return a ComplexTypeData object.""" """Decode base64 string and return a ComplexTypeData object."""
raise NotImplementedError("from_raw is not implemented for this type") raise NotImplementedError("from_raw is not implemented for this type")
@@ -127,9 +127,11 @@ class ElectricityTypeData(ComplexTypeData):
return cls(**json.loads(data.lower())) return cls(**json.loads(data.lower()))
@classmethod @classmethod
def from_raw(cls, data: str) -> Self: def from_raw(cls, data: str) -> Self | None:
"""Decode base64 string and return a ElectricityTypeData object.""" """Decode base64 string and return a ElectricityTypeData object."""
raw = base64.b64decode(data) raw = base64.b64decode(data)
if len(raw) == 0:
return None
voltage = struct.unpack(">H", raw[0:2])[0] / 10.0 voltage = struct.unpack(">H", raw[0:2])[0] / 10.0
electriccurrent = struct.unpack(">L", b"\x00" + raw[2:5])[0] / 1000.0 electriccurrent = struct.unpack(">L", b"\x00" + raw[2:5])[0] / 1000.0
power = struct.unpack(">L", b"\x00" + raw[5:8])[0] / 1000.0 power = struct.unpack(">L", b"\x00" + raw[5:8])[0] / 1000.0

View File

@@ -1555,10 +1555,11 @@ class TuyaSensorEntity(TuyaEntity, SensorEntity):
if ( if (
self.entity_description.complex_type is None self.entity_description.complex_type is None
or self.entity_description.subkey is None or self.entity_description.subkey is None
or (raw_values := self.entity_description.complex_type.from_raw(value))
is None
): ):
return None return None
values = self.entity_description.complex_type.from_raw(value) return getattr(raw_values, self.entity_description.subkey)
return getattr(values, self.entity_description.subkey)
# Valid string or enum value # Valid string or enum value
return value return value