From 09ff2722906cbe4198202186e11e080859282194 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 5 Jan 2019 22:31:41 +0100 Subject: [PATCH] Refactor motion sensor --- .../components/binary_sensor/xiaomi_aqara.py | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index de259c718f4..1b0548ddfaf 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -178,7 +178,28 @@ class XiaomiMotionSensor(XiaomiBinarySensor): self.async_schedule_update_ha_state() def parse_data(self, data, raw_data): - """Parse data sent by gateway.""" + """Parse data sent by gateway. + + Polling (proto v1, firmware version 1.4.1_159.0143) + + >> { "cmd":"read","sid":"158..."} + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'read_ack', 'data': '{"voltage":3005}'} + + Multicast messages (proto v1, firmware version 1.4.1_159.0143) + + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'report', 'data': '{"status":"motion"}'} + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'report', 'data': '{"no_motion":"120"}'} + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'report', 'data': '{"no_motion":"180"}'} + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'report', 'data': '{"no_motion":"300"}'} + << {'model': 'motion', 'sid': '158...', 'short_id': 26331, + 'cmd': 'heartbeat', 'data': '{"voltage":3005}'} + + """ if raw_data['cmd'] == 'heartbeat': _LOGGER.debug( 'Skipping heartbeat of the motion sensor. ' @@ -187,8 +208,7 @@ class XiaomiMotionSensor(XiaomiBinarySensor): '11631#issuecomment-357507744).') return - self._should_poll = False - if NO_MOTION in data: # handle push from the hub + if NO_MOTION in data: self._no_motion_since = data[NO_MOTION] self._state = False return True @@ -203,26 +223,20 @@ class XiaomiMotionSensor(XiaomiBinarySensor): self._unsub_set_no_motion() self._unsub_set_no_motion = async_call_later( self._hass, - 180, + 120, self._async_set_no_motion ) - else: - self._should_poll = True - if self.entity_id is not None: - self._hass.bus.fire('xiaomi_aqara.motion', { - 'entity_id': self.entity_id - }) + + if self.entity_id is not None: + self._hass.bus.fire('xiaomi_aqara.motion', { + 'entity_id': self.entity_id + }) self._no_motion_since = 0 if self._state: return False self._state = True return True - if value == NO_MOTION: - if not self._state: - return False - self._state = False - return True class XiaomiDoorSensor(XiaomiBinarySensor):