From 478eb48e93a44cb417e0b9cf446eb2d4ab28e7cb Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Wed, 22 Aug 2018 15:29:43 +0200 Subject: [PATCH] Fix the protocol v2 data_key of several aqara devices (#16112) * Fix the protocol v2 data_key of several aqara devices * Incorporate review --- .../components/binary_sensor/xiaomi_aqara.py | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index f53d07f2995..c1641c5f1ec 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -28,11 +28,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if model in ['motion', 'sensor_motion', 'sensor_motion.aq2']: devices.append(XiaomiMotionSensor(device, hass, gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: - if 'proto' not in device or int(device['proto'][0:1]) == 1: - data_key = 'status' - else: - data_key = 'window_status' - devices.append(XiaomiDoorSensor(device, data_key, gateway)) + devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': devices.append(XiaomiWaterLeakSensor(device, gateway)) elif model in ['smoke', 'sensor_smoke']: @@ -44,17 +40,27 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if 'proto' not in device or int(device['proto'][0:1]) == 1: data_key = 'status' else: - data_key = 'channel_0' + data_key = 'button_0' devices.append(XiaomiButton(device, 'Switch', data_key, hass, gateway)) elif model in ['86sw1', 'sensor_86sw1', 'sensor_86sw1.aq1']: - devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'channel_0' + else: + data_key = 'button_0' + devices.append(XiaomiButton(device, 'Wall Switch', data_key, hass, gateway)) elif model in ['86sw2', 'sensor_86sw2', 'sensor_86sw2.aq1']: + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key_left = 'channel_0' + data_key_right = 'channel_1' + else: + data_key_left = 'button_0' + data_key_right = 'button_1' devices.append(XiaomiButton(device, 'Wall Switch (Left)', - 'channel_0', hass, gateway)) + data_key_left, hass, gateway)) devices.append(XiaomiButton(device, 'Wall Switch (Right)', - 'channel_1', hass, gateway)) + data_key_right, hass, gateway)) devices.append(XiaomiButton(device, 'Wall Switch (Both)', 'dual_channel', hass, gateway)) elif model in ['cube', 'sensor_cube', 'sensor_cube.aqgl01']: @@ -119,7 +125,7 @@ class XiaomiNatgasSensor(XiaomiBinarySensor): if value is None: return False - if value == '1': + if value in ('1', '2'): if self._state: return False self._state = True @@ -194,9 +200,13 @@ class XiaomiMotionSensor(XiaomiBinarySensor): class XiaomiDoorSensor(XiaomiBinarySensor): """Representation of a XiaomiDoorSensor.""" - def __init__(self, device, data_key, xiaomi_hub): + def __init__(self, device, xiaomi_hub): """Initialize the XiaomiDoorSensor.""" self._open_since = 0 + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'window_status' XiaomiBinarySensor.__init__(self, device, 'Door Window Sensor', xiaomi_hub, data_key, 'opening') @@ -237,8 +247,12 @@ class XiaomiWaterLeakSensor(XiaomiBinarySensor): def __init__(self, device, xiaomi_hub): """Initialize the XiaomiWaterLeakSensor.""" + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'wleak_status' XiaomiBinarySensor.__init__(self, device, 'Water Leak Sensor', - xiaomi_hub, 'status', 'moisture') + xiaomi_hub, data_key, 'moisture') def parse_data(self, data, raw_data): """Parse data sent by gateway.""" @@ -285,7 +299,7 @@ class XiaomiSmokeSensor(XiaomiBinarySensor): if value is None: return False - if value == '1': + if value in ('1', '2'): if self._state: return False self._state = True @@ -359,6 +373,10 @@ class XiaomiCube(XiaomiBinarySensor): self._hass = hass self._last_action = None self._state = False + if 'proto' not in device or int(device['proto'][0:1]) == 1: + self._data_key = 'status' + else: + self._data_key = 'cube_status' XiaomiBinarySensor.__init__(self, device, 'Cube', xiaomi_hub, None, None) @@ -371,19 +389,12 @@ class XiaomiCube(XiaomiBinarySensor): def parse_data(self, data, raw_data): """Parse data sent by gateway.""" - if 'status' in data: + if self._data_key in data: self._hass.bus.fire('cube_action', { 'entity_id': self.entity_id, - 'action_type': data['status'] + 'action_type': data[self._data_key] }) - self._last_action = data['status'] - - if 'cube_status' in data: - self._hass.bus.fire('cube_action', { - 'entity_id': self.entity_id, - 'action_type': data['cube_status'] - }) - self._last_action = data['cube_status'] + self._last_action = data[self._data_key] if 'rotate' in data: self._hass.bus.fire('cube_action', {