Skip to content

Commit

Permalink
Fix negative lastseen time change
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 28, 2022
1 parent 6c49e5e commit 183001c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 8 additions & 3 deletions custom_components/xiaomi_gateway3/core/converters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,17 @@ def decode(self, device: "XDevice", payload: dict, value: Any):

class OnlineConv(Converter):
def decode(self, device: "XDevice", payload: dict, value: dict):
payload[self.attr] = value["status"] == "online"

dt = value["time"]
new_ts = time.time() - dt
# if the device will be on two gateways (by accident), the online time
# from "wrong" gateway could be wrong
if new_ts < device.decode_ts:
return

device.available = dt < device.available_timeout
device.decode_ts = time.time() - dt
device.decode_ts = new_ts

payload[self.attr] = value["status"] == "online"
payload["zigbee"] = datetime.now(timezone.utc) - timedelta(seconds=dt)


Expand Down
24 changes: 24 additions & 0 deletions tests/test_conv_lumi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from homeassistant.components.sensor import DOMAIN

from custom_components.xiaomi_gateway3.core.converters import ZIGBEE, GATEWAY
from custom_components.xiaomi_gateway3.core.device import XDevice

assert DOMAIN # fix circular import

ZDID = "lumi.112233aabbcc"
ZMAC = "0x0000112233aabbcc"
ZNWK = "0x12ab"
Expand Down Expand Up @@ -334,3 +338,23 @@ def test_resets():

params = [{"res_name": "8.0.2002", "value": 27}]
assert device.decode_lumi(params) == {'resets': 27, 'new_resets': 3}


def test_online():
device = XDevice(ZIGBEE, 'lumi.plug', ZDID, ZMAC, ZNWK)
device.setup_converters()

assert device.decode_ts == 0

params = [
{"res_name": "8.0.2102", "value": {"status": "online", "time": 60}}
]
assert device.decode_lumi(params)
assert device.decode_ts > 0

old_ts = device.decode_ts
params = [
{"res_name": "8.0.2102", "value": {"status": "offline", "time": 1000}}
]
assert device.decode_lumi(params) == {}
assert device.decode_ts == old_ts

0 comments on commit 183001c

Please sign in to comment.