From 17ef258425aed6baa51795b62953ea4784fec5b6 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:02:24 +0800 Subject: [PATCH 01/16] support new device model;support new LAN protocol 2.0 --- .../components/binary_sensor/xiaomi_aqara.py | 18 +++++++++--------- homeassistant/components/light/xiaomi_aqara.py | 2 +- .../components/sensor/xiaomi_aqara.py | 4 ++-- .../components/switch/xiaomi_aqara.py | 4 ++-- homeassistant/components/xiaomi_aqara.py | 1 + 5 files changed, 15 insertions(+), 14 deletions(-) mode change 100644 => 100755 homeassistant/components/binary_sensor/xiaomi_aqara.py mode change 100644 => 100755 homeassistant/components/light/xiaomi_aqara.py mode change 100644 => 100755 homeassistant/components/sensor/xiaomi_aqara.py mode change 100644 => 100755 homeassistant/components/switch/xiaomi_aqara.py mode change 100644 => 100755 homeassistant/components/xiaomi_aqara.py diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py old mode 100644 new mode 100755 index 2ed0de66b186b0..f53d460d9b8f99 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -25,30 +25,30 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for (_, gateway) in hass.data[PY_XIAOMI_GATEWAY].gateways.items(): for device in gateway.devices['binary_sensor']: model = device['model'] - if model in ['motion', 'sensor_motion.aq2']: + if model in ['motion', 'sensor_motion', 'sensor_motion.aq2']: devices.append(XiaomiMotionSensor(device, hass, gateway)) - elif model in ['magnet', 'sensor_magnet.aq2']: + elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': devices.append(XiaomiWaterLeakSensor(device, gateway)) - elif model == 'smoke': + elif model in ['smoke', 'sensor_smoke']: devices.append(XiaomiSmokeSensor(device, gateway)) - elif model == 'natgas': + elif model in ['natgas', 'sensor_natgas']: devices.append(XiaomiNatgasSensor(device, gateway)) - elif model in ['switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: - devices.append(XiaomiButton(device, 'Switch', 'status', + elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: + devices.append(XiaomiButton(device, 'Switch', 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', hass, gateway)) - elif model == '86sw1': + elif model in ['86sw1', 'sensor_86sw1.aq1']: devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', hass, gateway)) - elif model == '86sw2': + elif model in ['86sw2', 'sensor_86sw2.aq1']: devices.append(XiaomiButton(device, 'Wall Switch (Left)', 'channel_0', hass, gateway)) devices.append(XiaomiButton(device, 'Wall Switch (Right)', 'channel_1', hass, gateway)) devices.append(XiaomiButton(device, 'Wall Switch (Both)', 'dual_channel', hass, gateway)) - elif model == 'cube': + elif model in ['cube', 'sensor_cube']: devices.append(XiaomiCube(device, hass, gateway)) add_devices(devices) diff --git a/homeassistant/components/light/xiaomi_aqara.py b/homeassistant/components/light/xiaomi_aqara.py old mode 100644 new mode 100755 index 125e791829fac5..37ae60e3494dbb --- a/homeassistant/components/light/xiaomi_aqara.py +++ b/homeassistant/components/light/xiaomi_aqara.py @@ -18,7 +18,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for (_, gateway) in hass.data[PY_XIAOMI_GATEWAY].gateways.items(): for device in gateway.devices['light']: model = device['model'] - if model == 'gateway': + if model in ['gateway', 'gateway.v3']: devices.append(XiaomiGatewayLight(device, 'Gateway Light', gateway)) add_devices(devices) diff --git a/homeassistant/components/sensor/xiaomi_aqara.py b/homeassistant/components/sensor/xiaomi_aqara.py old mode 100644 new mode 100755 index 33bbdc323088a2..497a3915154d26 --- a/homeassistant/components/sensor/xiaomi_aqara.py +++ b/homeassistant/components/sensor/xiaomi_aqara.py @@ -26,7 +26,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 'temperature', gateway)) devices.append(XiaomiSensor(device, 'Humidity', 'humidity', gateway)) - elif device['model'] == 'weather.v1': + elif device['model'] in ['weather', 'weather.v1']: devices.append(XiaomiSensor(device, 'Temperature', 'temperature', gateway)) devices.append(XiaomiSensor(device, 'Humidity', @@ -36,7 +36,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): elif device['model'] == 'sensor_motion.aq2': devices.append(XiaomiSensor(device, 'Illumination', 'lux', gateway)) - elif device['model'] == 'gateway': + elif device['model'] in ['gateway', 'gateway.v3', 'acpartner.v3']: devices.append(XiaomiSensor(device, 'Illumination', 'illumination', gateway)) add_devices(devices) diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py old mode 100644 new mode 100755 index 939fc70660ad7f..da15578c17b123 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -26,7 +26,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for device in gateway.devices['switch']: model = device['model'] if model == 'plug': - devices.append(XiaomiGenericSwitch(device, "Plug", 'status', + devices.append(XiaomiGenericSwitch(device, "Plug", 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Switch', @@ -52,7 +52,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 'Wall Switch LN Right', 'channel_1', False, gateway)) - elif model in ['86plug', 'ctrl_86plug.aq1']: + elif model in ['86plug', 'ctrl_86plug', 'ctrl_86plug.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Plug', 'status', True, gateway)) add_devices(devices) diff --git a/homeassistant/components/xiaomi_aqara.py b/homeassistant/components/xiaomi_aqara.py old mode 100644 new mode 100755 index 244605a7b97334..96ba090ad89105 --- a/homeassistant/components/xiaomi_aqara.py +++ b/homeassistant/components/xiaomi_aqara.py @@ -211,6 +211,7 @@ def __init__(self, device, device_type, xiaomi_hub): self._sid = device['sid'] self._name = '{}_{}'.format(device_type, self._sid) self._type = device_type + self._proto = xiaomi_hub.proto self._write_to_hub = xiaomi_hub.write_to_hub self._get_from_hub = xiaomi_hub.get_from_hub self._device_state_attributes = {} From 7ace03856ff4364f03d522f00aef7d23120ce7b8 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:23:04 +0800 Subject: [PATCH 02/16] format code style --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 6 ++++-- homeassistant/components/switch/xiaomi_aqara.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index f53d460d9b8f99..6ec0ec878fbabc 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -35,8 +35,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices.append(XiaomiSmokeSensor(device, gateway)) elif model in ['natgas', 'sensor_natgas']: devices.append(XiaomiNatgasSensor(device, gateway)) - elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: - devices.append(XiaomiButton(device, 'Switch', 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + elif model in ['switch', 'sensor_switch', + 'sensor_switch.aq2', 'sensor_switch.aq3']: + devices.append(XiaomiButton(device, 'Switch', + 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', hass, gateway)) elif model in ['86sw1', 'sensor_86sw1.aq1']: devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py index da15578c17b123..408b278e4614da 100755 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -26,7 +26,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for device in gateway.devices['switch']: model = device['model'] if model == 'plug': - devices.append(XiaomiGenericSwitch(device, "Plug", 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + devices.append(XiaomiGenericSwitch(device, "Plug", + 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Switch', From 54dad521aa54d464a37b8f20dc9e35887d303eb7 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:25:16 +0800 Subject: [PATCH 03/16] format code style --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 2 +- homeassistant/components/switch/xiaomi_aqara.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 6ec0ec878fbabc..2e5b5f7eacb86c 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -38,7 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: devices.append(XiaomiButton(device, 'Switch', - 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', hass, gateway)) elif model in ['86sw1', 'sensor_86sw1.aq1']: devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py index 408b278e4614da..997febdf26b5fc 100755 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -27,7 +27,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): model = device['model'] if model == 'plug': devices.append(XiaomiGenericSwitch(device, "Plug", - 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Switch', From 77cc7dfb3c8c1e34bb41e28b9b91d736667f3d06 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:52:36 +0800 Subject: [PATCH 04/16] format code style --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 4 +++- homeassistant/components/switch/xiaomi_aqara.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 2e5b5f7eacb86c..4e07585a5f4fbe 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -38,7 +38,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: devices.append(XiaomiButton(device, 'Switch', - 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + 'status' if 'proto' not in device + or int(device['proto'][0:1]) == 1 + else 'channel_0', hass, gateway)) elif model in ['86sw1', 'sensor_86sw1.aq1']: devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py index 997febdf26b5fc..3d66f33ad890d2 100755 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -27,7 +27,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): model = device['model'] if model == 'plug': devices.append(XiaomiGenericSwitch(device, "Plug", - 'status' if int(device['proto'][0:1]) == 1 else 'channel_0', + 'status' if 'proto' not in device + or int(device['proto'][0:1]) == 1 + else 'channel_0', True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Switch', From 29b2d979b809ec9d76bf3f57d3281a1c0fe2924e Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:55:09 +0800 Subject: [PATCH 05/16] format code style --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 3 +-- homeassistant/components/switch/xiaomi_aqara.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 4e07585a5f4fbe..b59132fa866923 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -38,8 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: devices.append(XiaomiButton(device, 'Switch', - 'status' if 'proto' not in device - or int(device['proto'][0:1]) == 1 + 'status' if 'proto' not in device or int(device['proto'][0:1]) == 1 else 'channel_0', hass, gateway)) elif model in ['86sw1', 'sensor_86sw1.aq1']: diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py index 3d66f33ad890d2..51c3c9cfd0ce7b 100755 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -27,8 +27,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): model = device['model'] if model == 'plug': devices.append(XiaomiGenericSwitch(device, "Plug", - 'status' if 'proto' not in device - or int(device['proto'][0:1]) == 1 + 'status' if 'proto' not in device or int(device['proto'][0:1]) == 1 else 'channel_0', True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: From 8a116750303648dbe731692db8c0481230f16f9e Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 30 Mar 2018 15:58:38 +0800 Subject: [PATCH 06/16] format code style --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 8 +++++--- homeassistant/components/switch/xiaomi_aqara.py | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index b59132fa866923..19a02064ca8bd6 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -37,9 +37,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices.append(XiaomiNatgasSensor(device, gateway)) elif model in ['switch', 'sensor_switch', 'sensor_switch.aq2', 'sensor_switch.aq3']: - devices.append(XiaomiButton(device, 'Switch', - 'status' if 'proto' not in device or int(device['proto'][0:1]) == 1 - else 'channel_0', + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'channel_0' + devices.append(XiaomiButton(device, 'Switch', data_key, hass, gateway)) elif model in ['86sw1', 'sensor_86sw1.aq1']: devices.append(XiaomiButton(device, 'Wall Switch', 'channel_0', diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py index 51c3c9cfd0ce7b..4c44d6b2592f2e 100755 --- a/homeassistant/components/switch/xiaomi_aqara.py +++ b/homeassistant/components/switch/xiaomi_aqara.py @@ -26,9 +26,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): for device in gateway.devices['switch']: model = device['model'] if model == 'plug': - devices.append(XiaomiGenericSwitch(device, "Plug", - 'status' if 'proto' not in device or int(device['proto'][0:1]) == 1 - else 'channel_0', + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'channel_0' + devices.append(XiaomiGenericSwitch(device, "Plug", data_key, True, gateway)) elif model in ['ctrl_neutral1', 'ctrl_neutral1.aq1']: devices.append(XiaomiGenericSwitch(device, 'Wall Switch', From 5dc40bf2e12b4bec885e34879ea8b59ae472656a Mon Sep 17 00:00:00 2001 From: ileler Date: Sat, 31 Mar 2018 00:10:22 +0800 Subject: [PATCH 07/16] support motion 2.0 --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 19a02064ca8bd6..49f716b9eb7b99 100755 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -134,8 +134,12 @@ def __init__(self, device, hass, xiaomi_hub): """Initialize the XiaomiMotionSensor.""" self._hass = hass self._no_motion_since = 0 + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'motion_status' XiaomiBinarySensor.__init__(self, device, 'Motion Sensor', xiaomi_hub, - 'status', 'motion') + data_key, 'motion') @property def device_state_attributes(self): From e1f4036b416e6b2e3c4a03dac80db2871514efd2 Mon Sep 17 00:00:00 2001 From: ileler Date: Mon, 2 Apr 2018 00:34:42 +0800 Subject: [PATCH 08/16] remove file executable permissions --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 0 homeassistant/components/light/xiaomi_aqara.py | 0 homeassistant/components/sensor/xiaomi_aqara.py | 0 homeassistant/components/switch/xiaomi_aqara.py | 0 homeassistant/components/xiaomi_aqara.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 homeassistant/components/binary_sensor/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/light/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/sensor/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/switch/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/xiaomi_aqara.py diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py old mode 100755 new mode 100644 diff --git a/homeassistant/components/light/xiaomi_aqara.py b/homeassistant/components/light/xiaomi_aqara.py old mode 100755 new mode 100644 diff --git a/homeassistant/components/sensor/xiaomi_aqara.py b/homeassistant/components/sensor/xiaomi_aqara.py old mode 100755 new mode 100644 diff --git a/homeassistant/components/switch/xiaomi_aqara.py b/homeassistant/components/switch/xiaomi_aqara.py old mode 100755 new mode 100644 diff --git a/homeassistant/components/xiaomi_aqara.py b/homeassistant/components/xiaomi_aqara.py old mode 100755 new mode 100644 From d8c103f3822f37005509e395fa51777d4bbc37bc Mon Sep 17 00:00:00 2001 From: ileler Date: Mon, 2 Apr 2018 09:13:35 +0800 Subject: [PATCH 09/16] remove unused variables --- homeassistant/components/xiaomi_aqara.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/xiaomi_aqara.py b/homeassistant/components/xiaomi_aqara.py index 96ba090ad89105..244605a7b97334 100755 --- a/homeassistant/components/xiaomi_aqara.py +++ b/homeassistant/components/xiaomi_aqara.py @@ -211,7 +211,6 @@ def __init__(self, device, device_type, xiaomi_hub): self._sid = device['sid'] self._name = '{}_{}'.format(device_type, self._sid) self._type = device_type - self._proto = xiaomi_hub.proto self._write_to_hub = xiaomi_hub.write_to_hub self._get_from_hub = xiaomi_hub.get_from_hub self._device_state_attributes = {} From ad14d40263641f34a4b3d2c3588e579950b4571b Mon Sep 17 00:00:00 2001 From: ileler Date: Mon, 2 Apr 2018 09:55:48 +0800 Subject: [PATCH 10/16] support aqara-lock --- .../components/binary_sensor/xiaomi_aqara.py | 121 +++++++++++++++++- .../components/sensor/xiaomi_aqara.py | 6 +- homeassistant/components/xiaomi_aqara.py | 6 + 3 files changed, 130 insertions(+), 3 deletions(-) mode change 100755 => 100644 homeassistant/components/binary_sensor/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/sensor/xiaomi_aqara.py mode change 100755 => 100644 homeassistant/components/xiaomi_aqara.py diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py old mode 100755 new mode 100644 index 49f716b9eb7b99..e2c00abfd6c669 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -2,8 +2,8 @@ import logging from homeassistant.components.binary_sensor import BinarySensorDevice -from homeassistant.components.xiaomi_aqara import (PY_XIAOMI_GATEWAY, - XiaomiDevice) +from homeassistant.components.xiaomi_aqara import (PY_XIAOMI_GATEWAY, XiaomiDevice, + DOMAIN_CONFIG, CONF_LOCKUIDS) _LOGGER = logging.getLogger(__name__) @@ -15,18 +15,27 @@ ATTR_LAST_ACTION = 'last_action' ATTR_NO_MOTION_SINCE = 'No motion since' +VERIFIED_WRONG = 'verified_wrong' +FING_VERIFIED = 'fing_verified' +CARD_VERIFIED = 'card_verified' +PSW_VERIFIED = 'psw_verified' + DENSITY = 'density' ATTR_DENSITY = 'Density' def setup_platform(hass, config, add_devices, discovery_info=None): """Perform the setup for Xiaomi devices.""" + if PY_XIAOMI_GATEWAY not in hass.data: + return devices = [] for (_, gateway) in hass.data[PY_XIAOMI_GATEWAY].gateways.items(): for device in gateway.devices['binary_sensor']: model = device['model'] if model in ['motion', 'sensor_motion', 'sensor_motion.aq2']: devices.append(XiaomiMotionSensor(device, hass, gateway)) + elif model in ['lock.aq1']: + devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], devices, device, hass, gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': @@ -64,6 +73,7 @@ class XiaomiBinarySensor(XiaomiDevice, BinarySensorDevice): def __init__(self, device, name, xiaomi_hub, data_key, device_class): """Initialize the XiaomiSmokeSensor.""" self._data_key = data_key + self._real_sid = None if 'real_sid' not in device else device['real_sid'] self._device_class = device_class self._should_poll = False self._density = 0 @@ -86,6 +96,8 @@ def device_class(self): def update(self): """Update the sensor state.""" + if self._real_sid is not None: + return _LOGGER.debug('Updating xiaomi sensor by polling') self._get_from_hub(self._sid) @@ -187,6 +199,110 @@ def parse_data(self, data, raw_data): return True +class XiaomiUnlockSensor(XiaomiBinarySensor): + """Representation of a XiaomiUnlockSensor.""" + + def __init__(self, config, devices, device, hass, xiaomi_hub): + """Initialize the XiaomiMotionSensor.""" + self._hass = hass + self._no_motion_since = 0 + self.sub_devices = {} + users = config[CONF_LOCKUIDS] if CONF_LOCKUIDS in config else [] + users.append({ + 'uid': 'unsecu' + }) + for user in users: + self.sub_devices[str(user['uid'])] = XiaomiUnlockSubSensor({ + 'sid': str(user['uid']), + 'real_sid': device['sid'], + 'model': 'motion', + 'data': { + }, + 'raw_data': { + 'cmd': 'report' + } + }, hass, xiaomi_hub) + devices.append(self.sub_devices[str(user['uid'])]) + XiaomiBinarySensor.__init__(self, device, 'Unlock Sensor', xiaomi_hub, + 'status', 'motion') + + def push_sub_devices(self, uid=None): + for key in self.sub_devices: + self.sub_devices[key].push_data({ + 'status': MOTION if uid is not None and uid == key else NO_MOTION + }, { + 'sid': key, + 'cmd': 'report', + 'status': MOTION if uid is not None and uid == key else NO_MOTION + }) + + + def parse_data(self, data, raw_data): + """Parse data sent by gateway.""" + if raw_data['cmd'] in ['heartbeat', 'read_ack', 'read_rsp']: + _LOGGER.debug( + 'Skipping heartbeat of the motion sensor. ' + 'It can introduce an incorrect state because of a firmware ' + 'bug (https://github.com/home-assistant/home-assistant/pull/' + '11631#issuecomment-357507744).') + return + + self._should_poll = False + if VERIFIED_WRONG in data: # handle push from the hub + self.push_sub_devices('unsecu') + self._state = False + return True + + value = data.get(FING_VERIFIED) + if value is None: + value = data.get(CARD_VERIFIED) + if value is None: + value = data.get(PSW_VERIFIED) + if value is None: + self.push_sub_devices() + self._state = False + return False + + self.push_sub_devices(str(value)) + + self._should_poll = True + if self.entity_id is not None: + self._hass.bus.fire('motion', { + 'entity_id': self.entity_id + }) + self._state = True + return True + + +class XiaomiUnlockSubSensor(XiaomiBinarySensor): + """Representation of a XiaomiMotionSensor.""" + + def __init__(self, device, hass, xiaomi_hub): + """Initialize the XiaomiMotionSensor.""" + self._hass = hass + self._no_motion_since = 0 + if 'proto' not in device or int(device['proto'][0:1]) == 1: + data_key = 'status' + else: + data_key = 'motion_status' + XiaomiBinarySensor.__init__(self, device, 'Motion Sensor', xiaomi_hub, + data_key, 'motion') + + def parse_data(self, data, raw_data): + """Parse data sent by gateway.""" + if raw_data['cmd'] == 'heartbeat': + _LOGGER.debug( + 'Skipping heartbeat of the motion sensor. ' + 'It can introduce an incorrect state because of a firmware ' + 'bug (https://github.com/home-assistant/home-assistant/pull/' + '11631#issuecomment-357507744).') + return + + value = data.get(self._data_key) + self._state = value is not None and value == MOTION + return True + + class XiaomiDoorSensor(XiaomiBinarySensor): """Representation of a XiaomiDoorSensor.""" @@ -381,3 +497,4 @@ def parse_data(self, data, raw_data): self._last_action = 'rotate' return True + diff --git a/homeassistant/components/sensor/xiaomi_aqara.py b/homeassistant/components/sensor/xiaomi_aqara.py old mode 100755 new mode 100644 index 497a3915154d26..63a187b2d9a8c5 --- a/homeassistant/components/sensor/xiaomi_aqara.py +++ b/homeassistant/components/sensor/xiaomi_aqara.py @@ -80,7 +80,10 @@ def parse_data(self, data, raw_data): if self._data_key in ['temperature', 'humidity', 'pressure']: value /= 100 elif self._data_key in ['illumination']: - value = max(value - 300, 0) + if self._model in 'acpartner.v3': + value = max(value, 0) + else: + value = max(value - 300, 0) if self._data_key == 'temperature' and (value < -50 or value > 60): return False elif self._data_key == 'humidity' and (value <= 0 or value > 100): @@ -89,3 +92,4 @@ def parse_data(self, data, raw_data): return False self._state = round(value, 1) return True + diff --git a/homeassistant/components/xiaomi_aqara.py b/homeassistant/components/xiaomi_aqara.py old mode 100755 new mode 100644 index 96ba090ad89105..af9e51015d56ff --- a/homeassistant/components/xiaomi_aqara.py +++ b/homeassistant/components/xiaomi_aqara.py @@ -34,10 +34,12 @@ CONF_DISCOVERY_RETRY = 'discovery_retry' CONF_GATEWAYS = 'gateways' +CONF_LOCKUIDS = 'lockuids' CONF_INTERFACE = 'interface' CONF_KEY = 'key' DOMAIN = 'xiaomi_aqara' +DOMAIN_CONFIG = 'xiaomi_aqara_config' PY_XIAOMI_GATEWAY = "xiaomi_gw" @@ -95,6 +97,7 @@ def _fix_conf_defaults(config): DOMAIN: vol.Schema({ vol.Optional(CONF_GATEWAYS, default={}): vol.All(cv.ensure_list, [GATEWAY_CONFIG], [_fix_conf_defaults]), + vol.Optional(CONF_LOCKUIDS, default={}): vol.All(cv.ensure_list), vol.Optional(CONF_INTERFACE, default='any'): cv.string, vol.Optional(CONF_DISCOVERY_RETRY, default=3): cv.positive_int }) @@ -110,6 +113,7 @@ def setup(hass, config): gateways = config[DOMAIN][CONF_GATEWAYS] interface = config[DOMAIN][CONF_INTERFACE] discovery_retry = config[DOMAIN][CONF_DISCOVERY_RETRY] + hass.data[DOMAIN_CONFIG] = config[DOMAIN] @asyncio.coroutine def xiaomi_gw_discovered(service, discovery_info): @@ -209,6 +213,7 @@ def __init__(self, device, device_type, xiaomi_hub): self._state = None self._is_available = True self._sid = device['sid'] + self._model = device['model'] self._name = '{}_{}'.format(device_type, self._sid) self._type = device_type self._proto = xiaomi_hub.proto @@ -330,3 +335,4 @@ def gateway(sid): return schema.extend({ vol.Required(ATTR_GW_MAC, **kwargs): gateway }) + From e6cc64687e052bba01e9405a18d16a0cb36612d5 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 27 Apr 2018 18:44:42 +0800 Subject: [PATCH 11/16] trigger ci --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index e2c00abfd6c669..ac01fc206ecf12 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -497,4 +497,3 @@ def parse_data(self, data, raw_data): self._last_action = 'rotate' return True - From 3a1b7cb70900bb919118a60682902fb981163a23 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 27 Apr 2018 18:59:43 +0800 Subject: [PATCH 12/16] fix lint error --- .../components/binary_sensor/xiaomi_aqara.py | 18 +++++++++++------- .../components/sensor/xiaomi_aqara.py | 1 - homeassistant/components/xiaomi_aqara.py | 1 - 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index ac01fc206ecf12..1b4a3523dd392e 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -2,8 +2,8 @@ import logging from homeassistant.components.binary_sensor import BinarySensorDevice -from homeassistant.components.xiaomi_aqara import (PY_XIAOMI_GATEWAY, XiaomiDevice, - DOMAIN_CONFIG, CONF_LOCKUIDS) +from homeassistant.components.xiaomi_aqara import ( + PY_XIAOMI_GATEWAY, XiaomiDevice, DOMAIN_CONFIG, CONF_LOCKUIDS) _LOGGER = logging.getLogger(__name__) @@ -35,7 +35,8 @@ 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 ['lock.aq1']: - devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], devices, device, hass, gateway)) + devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], + devices, device, hass, gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': @@ -73,7 +74,8 @@ class XiaomiBinarySensor(XiaomiDevice, BinarySensorDevice): def __init__(self, device, name, xiaomi_hub, data_key, device_class): """Initialize the XiaomiSmokeSensor.""" self._data_key = data_key - self._real_sid = None if 'real_sid' not in device else device['real_sid'] + self._real_sid = \ + None if 'real_sid' not in device else device['real_sid'] self._device_class = device_class self._should_poll = False self._density = 0 @@ -227,16 +229,18 @@ def __init__(self, config, devices, device, hass, xiaomi_hub): 'status', 'motion') def push_sub_devices(self, uid=None): + """Parse data sent by gateway.""" for key in self.sub_devices: self.sub_devices[key].push_data({ - 'status': MOTION if uid is not None and uid == key else NO_MOTION + 'status': + MOTION if uid is not None and uid == key else NO_MOTION }, { 'sid': key, 'cmd': 'report', - 'status': MOTION if uid is not None and uid == key else NO_MOTION + 'status': + MOTION if uid is not None and uid == key else NO_MOTION }) - def parse_data(self, data, raw_data): """Parse data sent by gateway.""" if raw_data['cmd'] in ['heartbeat', 'read_ack', 'read_rsp']: diff --git a/homeassistant/components/sensor/xiaomi_aqara.py b/homeassistant/components/sensor/xiaomi_aqara.py index 63a187b2d9a8c5..10de19257a1e89 100644 --- a/homeassistant/components/sensor/xiaomi_aqara.py +++ b/homeassistant/components/sensor/xiaomi_aqara.py @@ -92,4 +92,3 @@ def parse_data(self, data, raw_data): return False self._state = round(value, 1) return True - diff --git a/homeassistant/components/xiaomi_aqara.py b/homeassistant/components/xiaomi_aqara.py index 4e8c43fb07c01f..bc4174bec7709f 100644 --- a/homeassistant/components/xiaomi_aqara.py +++ b/homeassistant/components/xiaomi_aqara.py @@ -334,4 +334,3 @@ def gateway(sid): return schema.extend({ vol.Required(ATTR_GW_MAC, **kwargs): gateway }) - From c03f1bf15c899dd599a26be472dfeb4c62dad1c9 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 27 Apr 2018 19:06:58 +0800 Subject: [PATCH 13/16] fix lint error --- 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 1b4a3523dd392e..18045d33383560 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -36,7 +36,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices.append(XiaomiMotionSensor(device, hass, gateway)) elif model in ['lock.aq1']: devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], - devices, device, hass, gateway)) + devices, device, hass, gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': From 1c1670b4db69a239d539e2c2e078edb4cf9f3099 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 27 Apr 2018 19:14:47 +0800 Subject: [PATCH 14/16] fix lint error --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 18045d33383560..600b31a989b8f1 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -36,7 +36,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices.append(XiaomiMotionSensor(device, hass, gateway)) elif model in ['lock.aq1']: devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], - devices, device, hass, gateway)) + devices, device, hass, + gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': From 94c8db67f28d9556ba41b4eb924b74fcb95175e4 Mon Sep 17 00:00:00 2001 From: ileler Date: Fri, 27 Apr 2018 19:23:58 +0800 Subject: [PATCH 15/16] fix lint error --- homeassistant/components/binary_sensor/xiaomi_aqara.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index 600b31a989b8f1..f6580abcc0a175 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -36,8 +36,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices.append(XiaomiMotionSensor(device, hass, gateway)) elif model in ['lock.aq1']: devices.append(XiaomiUnlockSensor(hass.data[DOMAIN_CONFIG], - devices, device, hass, - gateway)) + devices, device, + hass, gateway)) elif model in ['magnet', 'sensor_magnet', 'sensor_magnet.aq2']: devices.append(XiaomiDoorSensor(device, gateway)) elif model == 'sensor_wleak.aq1': From 6fadab8c95b0563e8ed02aedff18cda2b797795e Mon Sep 17 00:00:00 2001 From: ileler Date: Tue, 8 May 2018 08:31:54 +0800 Subject: [PATCH 16/16] support aqara door lock --- .../components/binary_sensor/xiaomi_aqara.py | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/binary_sensor/xiaomi_aqara.py b/homeassistant/components/binary_sensor/xiaomi_aqara.py index f6580abcc0a175..eee1c6a79bad03 100644 --- a/homeassistant/components/binary_sensor/xiaomi_aqara.py +++ b/homeassistant/components/binary_sensor/xiaomi_aqara.py @@ -206,7 +206,7 @@ class XiaomiUnlockSensor(XiaomiBinarySensor): """Representation of a XiaomiUnlockSensor.""" def __init__(self, config, devices, device, hass, xiaomi_hub): - """Initialize the XiaomiMotionSensor.""" + """Initialize the XiaomiUnlockSensor.""" self._hass = hass self._no_motion_since = 0 self.sub_devices = {} @@ -245,11 +245,6 @@ def push_sub_devices(self, uid=None): def parse_data(self, data, raw_data): """Parse data sent by gateway.""" if raw_data['cmd'] in ['heartbeat', 'read_ack', 'read_rsp']: - _LOGGER.debug( - 'Skipping heartbeat of the motion sensor. ' - 'It can introduce an incorrect state because of a firmware ' - 'bug (https://github.com/home-assistant/home-assistant/pull/' - '11631#issuecomment-357507744).') return self._should_poll = False @@ -280,27 +275,22 @@ def parse_data(self, data, raw_data): class XiaomiUnlockSubSensor(XiaomiBinarySensor): - """Representation of a XiaomiMotionSensor.""" + """Representation of a XiaomiUnlockSubSensor.""" def __init__(self, device, hass, xiaomi_hub): - """Initialize the XiaomiMotionSensor.""" + """Initialize the XiaomiUnlockSubSensor.""" self._hass = hass self._no_motion_since = 0 if 'proto' not in device or int(device['proto'][0:1]) == 1: data_key = 'status' else: data_key = 'motion_status' - XiaomiBinarySensor.__init__(self, device, 'Motion Sensor', xiaomi_hub, - data_key, 'motion') + XiaomiBinarySensor.__init__(self, device, 'UnlockSub Sensor', + xiaomi_hub, data_key, 'motion') def parse_data(self, data, raw_data): """Parse data sent by gateway.""" if raw_data['cmd'] == 'heartbeat': - _LOGGER.debug( - 'Skipping heartbeat of the motion sensor. ' - 'It can introduce an incorrect state because of a firmware ' - 'bug (https://github.com/home-assistant/home-assistant/pull/' - '11631#issuecomment-357507744).') return value = data.get(self._data_key)