|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from dataclasses import dataclass |
6 | | -import struct |
7 | 6 |
|
8 | 7 | from tuya_sharing import CustomerDevice, Manager |
9 | 8 |
|
|
49 | 48 | DPCodeWrapper, |
50 | 49 | EnumTypeData, |
51 | 50 | ) |
| 51 | +from .raw_data_models import ElectricityData |
52 | 52 |
|
53 | 53 |
|
54 | 54 | class _WindDirectionWrapper(DPCodeTypeInformationWrapper[EnumTypeData]): |
@@ -120,42 +120,52 @@ def read_device_status(self, device: CustomerDevice) -> float | None: |
120 | 120 | return raw_value.get("voltage") |
121 | 121 |
|
122 | 122 |
|
123 | | -class _RawElectricityCurrentWrapper(DPCodeBase64Wrapper): |
124 | | - """Custom DPCode Wrapper for extracting electricity current from base64.""" |
| 123 | +class _RawElectricityDataWrapper(DPCodeBase64Wrapper): |
| 124 | + """Custom DPCode Wrapper for extracting ElectricityData from base64.""" |
125 | 125 |
|
126 | | - native_unit = UnitOfElectricCurrent.MILLIAMPERE |
127 | | - suggested_unit = UnitOfElectricCurrent.AMPERE |
| 126 | + def _convert(self, value: ElectricityData) -> float: |
| 127 | + """Extract specific value from T.""" |
| 128 | + raise NotImplementedError |
128 | 129 |
|
129 | 130 | def read_device_status(self, device: CustomerDevice) -> float | None: |
130 | 131 | """Read the device value for the dpcode.""" |
131 | | - if (raw_value := super().read_bytes(device)) is None: |
| 132 | + if (raw_value := super().read_bytes(device)) is None or ( |
| 133 | + value := ElectricityData.from_bytes(raw_value) |
| 134 | + ) is None: |
132 | 135 | return None |
133 | | - return struct.unpack(">L", b"\x00" + raw_value[2:5])[0] |
| 136 | + return self._convert(value) |
| 137 | + |
| 138 | + |
| 139 | +class _RawElectricityCurrentWrapper(_RawElectricityDataWrapper): |
| 140 | + """Custom DPCode Wrapper for extracting electricity current from base64.""" |
| 141 | + |
| 142 | + native_unit = UnitOfElectricCurrent.MILLIAMPERE |
| 143 | + suggested_unit = UnitOfElectricCurrent.AMPERE |
| 144 | + |
| 145 | + def _convert(self, value: ElectricityData) -> float: |
| 146 | + """Extract specific value from ElectricityData.""" |
| 147 | + return value.current |
134 | 148 |
|
135 | 149 |
|
136 | | -class _RawElectricityPowerWrapper(DPCodeBase64Wrapper): |
| 150 | +class _RawElectricityPowerWrapper(_RawElectricityDataWrapper): |
137 | 151 | """Custom DPCode Wrapper for extracting electricity power from base64.""" |
138 | 152 |
|
139 | 153 | native_unit = UnitOfPower.WATT |
140 | 154 | suggested_unit = UnitOfPower.KILO_WATT |
141 | 155 |
|
142 | | - def read_device_status(self, device: CustomerDevice) -> float | None: |
143 | | - """Read the device value for the dpcode.""" |
144 | | - if (raw_value := super().read_bytes(device)) is None: |
145 | | - return None |
146 | | - return struct.unpack(">L", b"\x00" + raw_value[5:8])[0] |
| 156 | + def _convert(self, value: ElectricityData) -> float: |
| 157 | + """Extract specific value from ElectricityData.""" |
| 158 | + return value.power |
147 | 159 |
|
148 | 160 |
|
149 | | -class _RawElectricityVoltageWrapper(DPCodeBase64Wrapper): |
| 161 | +class _RawElectricityVoltageWrapper(_RawElectricityDataWrapper): |
150 | 162 | """Custom DPCode Wrapper for extracting electricity voltage from base64.""" |
151 | 163 |
|
152 | 164 | native_unit = UnitOfElectricPotential.VOLT |
153 | 165 |
|
154 | | - def read_device_status(self, device: CustomerDevice) -> float | None: |
155 | | - """Read the device value for the dpcode.""" |
156 | | - if (raw_value := super().read_bytes(device)) is None: |
157 | | - return None |
158 | | - return struct.unpack(">H", raw_value[0:2])[0] / 10.0 |
| 166 | + def _convert(self, value: ElectricityData) -> float: |
| 167 | + """Extract specific value from ElectricityData.""" |
| 168 | + return value.voltage |
159 | 169 |
|
160 | 170 |
|
161 | 171 | CURRENT_WRAPPER = (_RawElectricityCurrentWrapper, _JsonElectricityCurrentWrapper) |
|
0 commit comments