Skip to content

Commit 130647c

Browse files
committed
Add support new SNZB-02WD #1612
1 parent 40b1303 commit 130647c

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

custom_components/sonoff/core/devices.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@
5757
XEnergySensorPOWR3,
5858
XEnergyTotal,
5959
XHexVoltageTRVZB,
60+
XHumCorrection,
6061
XHumidityTH,
6162
XOutdoorTempNS,
6263
XRemoteButton,
6364
XSensor,
6465
XT5Action,
65-
XTemperatureNS,
66+
XTempCorrection,
6667
XTemperatureTH,
6768
XTodayWaterUsage,
6869
XUnknown,
@@ -322,7 +323,7 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
322323
Switch1,
323324
Switch2,
324325
XClimateNS,
325-
XTemperatureNS,
326+
XTempCorrection,
326327
XOutdoorTempNS,
327328
], # Sonoff NS Panel
328329
# https://github.com/AlexxIT/SonoffLAN/issues/1026
@@ -573,6 +574,8 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
573574
spec(XSensor100, param="current"),
574575
spec(XSensor100, param="voltage"),
575576
],
577+
# SNZB-02WD https://github.com/AlexxIT/SonoffLAN/issues/1612
578+
7033: [XTempCorrection, XHumCorrection, Battery, ZRSSI],
576579
}
577580

578581

custom_components/sonoff/sensor.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,40 @@ class XEnergyTotal(XSensor):
267267
_attr_state_class = SensorStateClass.TOTAL_INCREASING
268268

269269

270-
class XTemperatureNS(XSensor):
270+
def parse_float(v: int | float | str):
271+
return float(v) if isinstance(v, str) else v
272+
273+
274+
class XTempCorrection(XSensor):
271275
params = {"temperature", "tempCorrection"}
272276
uid = "temperature"
273277

274278
def set_state(self, params: dict = None, value: float = None):
275-
if params:
276-
# cache updated in XClimateNS entity
277-
cache = self.device["params"]
278-
value = cache["temperature"]
279-
# fix str https://github.com/AlexxIT/SonoffLAN/issues/1628
279+
try:
280+
if (cache := self.device["params"]) != params:
281+
cache.update(params)
282+
value = parse_float(cache["temperature"])
280283
if v := cache.get("tempCorrection"):
281-
value += float(v)
282-
XSensor.set_state(self, value=value)
284+
value += parse_float(v)
285+
XSensor.set_state(self, value=value)
286+
except:
287+
pass
288+
289+
290+
class XHumCorrection(XSensor):
291+
params = {"humidity", "humCorrection"}
292+
uid = "humidity"
293+
294+
def set_state(self, params: dict = None, value: float = None):
295+
try:
296+
if (cache := self.device["params"]) != params:
297+
cache.update(params)
298+
value = parse_float(cache["humidity"])
299+
if v := cache.get("humCorrection"):
300+
value += parse_float(v)
301+
XSensor.set_state(self, value=value)
302+
except:
303+
pass
283304

284305

285306
class XOutdoorTempNS(XSensor):

tests/test_entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
XRemoteButton,
5454
XSensor,
5555
XT5Action,
56-
XTemperatureNS,
56+
XTempCorrection,
5757
XUnknown,
5858
)
5959
from custom_components.sonoff.switch import (
@@ -1207,7 +1207,7 @@ def test_ns_panel():
12071207
for uid in ("1", "2"):
12081208
assert any(e.uid == uid for e in entities)
12091209

1210-
temp = next(e for e in entities if isinstance(e, XTemperatureNS))
1210+
temp = next(e for e in entities if isinstance(e, XTempCorrection))
12111211
state = temp.hass.states.get(temp.entity_id)
12121212
assert state.state == "20"
12131213
assert state.attributes == {

0 commit comments

Comments
 (0)