Skip to content

Commit

Permalink
Fix missed motion event for Aqara sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Dec 9, 2020
1 parent d1d9e9c commit 52b96d5
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions custom_components/xiaomi_gateway3/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def update(self, data: dict = None):


class Gateway3MotionSensor(Gateway3BinarySensor):
_last_on = 0
_last_off = 0
_state = STATE_OFF
_timeout_pos = 0
Expand All @@ -90,18 +91,30 @@ def _set_no_motion(self, *args):
self.async_write_ha_state()

def update(self, data: dict = None):
if self._attr in data:
self._state = STATE_ON if data[self._attr] else STATE_OFF
if self._state:
self._attrs[ATTR_LAST_TRIGGERED] = now().strftime(DT_FORMAT)
# https://github.com/AlexxIT/XiaomiGateway3/issues/135
if 'illumination' in data and len(data) == 1:
data[self._attr] = 1

# handle available change
self.async_write_ha_state()
if self._attr not in data:
# handle available change
self.async_write_ha_state()
return

# check only motion=1
assert data[self._attr] == 1, data

# continue only if motion=1 arrived
if not data.get(self._attr):
# don't trigger motion right after illumination
t = time.time()
if t - self._last_on < 1:
return

self._state = STATE_ON
self._attrs[ATTR_LAST_TRIGGERED] = now().strftime(DT_FORMAT)
self._last_on = t

# handle available change
self.async_write_ha_state()

if self._unsub_set_no_motion:
self._unsub_set_no_motion()

Expand All @@ -115,7 +128,7 @@ def update(self, data: dict = None):
else:
delay = timeout

if delay < 0 and time.time() + delay < self._last_off:
if delay < 0 and t + delay < self._last_off:
delay *= 2

self.debug(f"Extend delay: {delay} seconds")
Expand Down

0 comments on commit 52b96d5

Please sign in to comment.