Skip to content

Commit d0e33a6

Browse files
authored
Use process_raw_value in Tuya JsonTypeInformation (home-assistant#158517)
1 parent f55fc78 commit d0e33a6

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

homeassistant/components/tuya/light.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,6 @@ class _ColorDataWrapper(DPCodeJsonWrapper):
167167
s_type = DEFAULT_S_TYPE
168168
v_type = DEFAULT_V_TYPE
169169

170-
def read_device_status(self, device: CustomerDevice) -> dict[str, Any] | None:
171-
"""Read the color data for the dpcode."""
172-
if (status_data := self._read_device_status_raw(device)) is None or not (
173-
status := json_loads_object(status_data)
174-
):
175-
return None
176-
return status
177-
178170
def read_hs_color(self, device: CustomerDevice) -> tuple[float, float] | None:
179171
"""Get the HS value from this color data."""
180172
if (status := self.read_device_status(device)) is None:

homeassistant/components/tuya/models.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
from tuya_sharing import CustomerDevice
88

9-
from homeassistant.util.json import json_loads
10-
119
from .type_information import (
1210
BitmapTypeInformation,
1311
BooleanTypeInformation,
@@ -137,12 +135,6 @@ class DPCodeJsonWrapper(DPCodeTypeInformationWrapper[JsonTypeInformation]):
137135

138136
_DPTYPE = JsonTypeInformation
139137

140-
def read_json(self, device: CustomerDevice) -> Any | None:
141-
"""Read the device value for the dpcode."""
142-
if (raw_value := self._read_device_status_raw(device)) is None:
143-
return None
144-
return json_loads(raw_value)
145-
146138

147139
class DPCodeEnumWrapper(DPCodeTypeInformationWrapper[EnumTypeInformation]):
148140
"""Simple wrapper for EnumTypeInformation values."""

homeassistant/components/tuya/sensor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class _JsonElectricityCurrentWrapper(DPCodeJsonWrapper):
9090

9191
def read_device_status(self, device: CustomerDevice) -> float | None:
9292
"""Read the device value for the dpcode."""
93-
if (raw_value := super().read_json(device)) is None:
93+
if (status := super().read_device_status(device)) is None:
9494
return None
95-
return raw_value.get("electricCurrent")
95+
return status.get("electricCurrent")
9696

9797

9898
class _JsonElectricityPowerWrapper(DPCodeJsonWrapper):
@@ -102,9 +102,9 @@ class _JsonElectricityPowerWrapper(DPCodeJsonWrapper):
102102

103103
def read_device_status(self, device: CustomerDevice) -> float | None:
104104
"""Read the device value for the dpcode."""
105-
if (raw_value := super().read_json(device)) is None:
105+
if (status := super().read_device_status(device)) is None:
106106
return None
107-
return raw_value.get("power")
107+
return status.get("power")
108108

109109

110110
class _JsonElectricityVoltageWrapper(DPCodeJsonWrapper):
@@ -114,9 +114,9 @@ class _JsonElectricityVoltageWrapper(DPCodeJsonWrapper):
114114

115115
def read_device_status(self, device: CustomerDevice) -> float | None:
116116
"""Read the device value for the dpcode."""
117-
if (raw_value := super().read_json(device)) is None:
117+
if (status := super().read_device_status(device)) is None:
118118
return None
119-
return raw_value.get("voltage")
119+
return status.get("voltage")
120120

121121

122122
class _RawElectricityDataWrapper(DPCodeRawWrapper):

homeassistant/components/tuya/type_information.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,19 @@ def _from_json(cls, dpcode: str, type_data: str) -> Self | None:
286286

287287

288288
@dataclass(kw_only=True)
289-
class JsonTypeInformation(TypeInformation[Any]):
289+
class JsonTypeInformation(TypeInformation[dict[str, Any]]):
290290
"""Json type information."""
291291

292292
_DPTYPE = DPType.JSON
293293

294+
def process_raw_value(
295+
self, raw_value: Any | None, device: CustomerDevice
296+
) -> dict[str, Any] | None:
297+
"""Read and process raw value against this type information."""
298+
if raw_value is None:
299+
return None
300+
return json_loads_object(raw_value)
301+
294302

295303
@dataclass(kw_only=True)
296304
class RawTypeInformation(TypeInformation[bytes]):

0 commit comments

Comments
 (0)