Skip to content

Commit

Permalink
Adds filter wrong values for sensors #683
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 23, 2022
1 parent 8866404 commit d9de37b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 3 additions & 1 deletion custom_components/sonoff/core/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
14: SPEC_SWITCH, # Sonoff Basic (3rd party)
15: [
XSwitchTH, LED, RSSI,
spec(XSensor, param="currentTemperature", uid="temperature", round=1),
# https://github.com/AlexxIT/SonoffLAN/issues/683
spec(XSensor, param="currentTemperature", uid="temperature",
round=1, min=-270, max=270),
spec(XSensor, param="currentHumidity", uid="humidity"),
], # Sonoff TH16
18: [
Expand Down
8 changes: 6 additions & 2 deletions custom_components/sonoff/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ class XSensor(XEntity, SensorEntity):
needed. Also class can filter incoming values using zigbee-like reporting
logic: min report interval, max report interval, reportable change value.
"""
multiply = None
round = None
max: float = float("inf")
min: float = -float("inf")
multiply: float = None
round: int = None

report_ts = None
report_mint = None
Expand Down Expand Up @@ -92,6 +94,8 @@ def set_state(self, params: dict = None):
if self.round is not None:
# convert to int when round is zero
value = round(value, self.round or None)
if value < self.min or value > self.max:
return
except (TypeError, ValueError):
value = self.report_value

Expand Down
26 changes: 14 additions & 12 deletions tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,39 @@ def test_sonoff_th():
assert temp.state == 14.6

# test round to 1 digit
msg = {
reg.local.dispatcher_send(SIGNAL_UPDATE, {
"deviceid": DEVICEID, "host": "",
"params": {"deviceType": "normal", "temperature": 12.34}
}
reg.local.dispatcher_send(SIGNAL_UPDATE, msg)
})
assert temp.state == 12.3

reg.cloud.dispatcher_send(SIGNAL_UPDATE, {
"deviceid": DEVICEID,
"params": {"deviceType": "normal", "temperature": -273}
})
assert temp.state == 12.3

hum: XSensor = next(e for e in entities if e.uid == "humidity")
assert hum.state == 42

# check TH v3.4.0 param name
msg = {
reg.local.dispatcher_send(SIGNAL_UPDATE, {
"deviceid": DEVICEID, "host": "",
"params": {"deviceType": "normal", "humidity": 48}
}
reg.local.dispatcher_send(SIGNAL_UPDATE, msg)
})
assert hum.state == 48

# check TH v3.4.0 zero humidity bug (skip value)
msg = {
reg.local.dispatcher_send(SIGNAL_UPDATE, {
"deviceid": DEVICEID, "host": "",
"params": {"deviceType": "normal", "humidity": 0}
}
reg.local.dispatcher_send(SIGNAL_UPDATE, msg)
})
assert hum.state == 48

msg = {
reg.local.dispatcher_send(SIGNAL_UPDATE, {
"deviceid": DEVICEID, "host": "",
"params": {"deviceType": "normal", "currentHumidity": "unavailable"}
}
reg.local.dispatcher_send(SIGNAL_UPDATE, msg)
})
assert hum.state is None


Expand Down

0 comments on commit d9de37b

Please sign in to comment.