From 09ff2722906cbe4198202186e11e080859282194 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 5 Jan 2019 22:31:41 +0100 Subject: [PATCH 1/2] 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): From 32faf5b70947b0c8c90f5bedf5498e9ae6dd23aa Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 5 Jan 2019 22:39:02 +0100 Subject: [PATCH 2/2] Improve debug output --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 1b0548ddfaf..afffb71bae5 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -107,7 +107,7 @@ class XiaomiBinarySensor(XiaomiDevice, BinarySensorDevice): def update(self): """Update the sensor state.""" - _LOGGER.debug('Updating xiaomi sensor by polling') + _LOGGER.debug('Updating xiaomi sensor (%s) by polling', self._sid) self._get_from_hub(self._sid)