mirror of
https://github.com/home-assistant/core.git
synced 2025-09-08 14:21:33 +02:00
Handle empty electricity RAW sensors in Tuya (#150406)
This commit is contained in:
@@ -108,7 +108,7 @@ class ComplexTypeData:
|
||||
raise NotImplementedError("from_json is not implemented for this type")
|
||||
|
||||
@classmethod
|
||||
def from_raw(cls, data: str) -> Self:
|
||||
def from_raw(cls, data: str) -> Self | None:
|
||||
"""Decode base64 string and return a ComplexTypeData object."""
|
||||
raise NotImplementedError("from_raw is not implemented for this type")
|
||||
|
||||
@@ -127,9 +127,11 @@ class ElectricityTypeData(ComplexTypeData):
|
||||
return cls(**json.loads(data.lower()))
|
||||
|
||||
@classmethod
|
||||
def from_raw(cls, data: str) -> Self:
|
||||
def from_raw(cls, data: str) -> Self | None:
|
||||
"""Decode base64 string and return a ElectricityTypeData object."""
|
||||
raw = base64.b64decode(data)
|
||||
if len(raw) == 0:
|
||||
return None
|
||||
voltage = struct.unpack(">H", raw[0:2])[0] / 10.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
|
||||
|
@@ -1555,10 +1555,11 @@ class TuyaSensorEntity(TuyaEntity, SensorEntity):
|
||||
if (
|
||||
self.entity_description.complex_type is None
|
||||
or self.entity_description.subkey is None
|
||||
or (raw_values := self.entity_description.complex_type.from_raw(value))
|
||||
is None
|
||||
):
|
||||
return None
|
||||
values = self.entity_description.complex_type.from_raw(value)
|
||||
return getattr(values, self.entity_description.subkey)
|
||||
return getattr(raw_values, self.entity_description.subkey)
|
||||
|
||||
# Valid string or enum value
|
||||
return value
|
||||
|
Reference in New Issue
Block a user