Skip to content

Commit 5fdd04b

Browse files
epenetfrenck
authored andcommitted
Handle empty electricity RAW sensors in Tuya (home-assistant#150406)
1 parent 6f5d72f commit 5fdd04b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

homeassistant/components/tuya/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def from_json(cls, data: str) -> Self:
108108
raise NotImplementedError("from_json is not implemented for this type")
109109

110110
@classmethod
111-
def from_raw(cls, data: str) -> Self:
111+
def from_raw(cls, data: str) -> Self | None:
112112
"""Decode base64 string and return a ComplexTypeData object."""
113113
raise NotImplementedError("from_raw is not implemented for this type")
114114

@@ -127,9 +127,11 @@ def from_json(cls, data: str) -> Self:
127127
return cls(**json.loads(data.lower()))
128128

129129
@classmethod
130-
def from_raw(cls, data: str) -> Self:
130+
def from_raw(cls, data: str) -> Self | None:
131131
"""Decode base64 string and return a ElectricityTypeData object."""
132132
raw = base64.b64decode(data)
133+
if len(raw) == 0:
134+
return None
133135
voltage = struct.unpack(">H", raw[0:2])[0] / 10.0
134136
electriccurrent = struct.unpack(">L", b"\x00" + raw[2:5])[0] / 1000.0
135137
power = struct.unpack(">L", b"\x00" + raw[5:8])[0] / 1000.0

homeassistant/components/tuya/sensor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,10 +1555,11 @@ def native_value(self) -> StateType:
15551555
if (
15561556
self.entity_description.complex_type is None
15571557
or self.entity_description.subkey is None
1558+
or (raw_values := self.entity_description.complex_type.from_raw(value))
1559+
is None
15581560
):
15591561
return None
1560-
values = self.entity_description.complex_type.from_raw(value)
1561-
return getattr(values, self.entity_description.subkey)
1562+
return getattr(raw_values, self.entity_description.subkey)
15621563

15631564
# Valid string or enum value
15641565
return value

0 commit comments

Comments
 (0)