From 0ea5a73e8dbbd65067370cfa99a142b577c0a08f Mon Sep 17 00:00:00 2001 From: Zhong Jianxin Date: Fri, 12 Oct 2018 14:34:32 +0800 Subject: [PATCH] Fix motion sensor in Aqara LAN protocol V2 (#17240) --- .../components/binary_sensor/xiaomi_aqara.py | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 2b4934ff824..e082c886f03 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -4,6 +4,8 @@ import logging from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.xiaomi_aqara import (PY_XIAOMI_GATEWAY, XiaomiDevice) +from homeassistant.core import callback +from homeassistant.helpers.event import async_call_later _LOGGER = logging.getLogger(__name__) @@ -153,6 +155,7 @@ class XiaomiMotionSensor(XiaomiBinarySensor): """Initialize the XiaomiMotionSensor.""" self._hass = hass self._no_motion_since = 0 + self._unsub_set_no_motion = None if 'proto' not in device or int(device['proto'][0:1]) == 1: data_key = 'status' else: @@ -167,6 +170,13 @@ class XiaomiMotionSensor(XiaomiBinarySensor): attrs.update(super().device_state_attributes) return attrs + @callback + def _async_set_no_motion(self, now): + """Set state to False.""" + self._unsub_set_no_motion = None + self._state = False + self.async_schedule_update_ha_state() + def parse_data(self, data, raw_data): """Parse data sent by gateway.""" if raw_data['cmd'] == 'heartbeat': @@ -188,11 +198,20 @@ class XiaomiMotionSensor(XiaomiBinarySensor): return False if value == MOTION: - self._should_poll = True - if self.entity_id is not None: - self._hass.bus.fire('motion', { - 'entity_id': self.entity_id - }) + if self._data_key == 'motion_status': + if self._unsub_set_no_motion: + self._unsub_set_no_motion() + self._unsub_set_no_motion = async_call_later( + self._hass, + 180, + self._async_set_no_motion + ) + else: + self._should_poll = True + if self.entity_id is not None: + self._hass.bus.fire('motion', { + 'entity_id': self.entity_id + }) self._no_motion_since = 0 if self._state: