-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Support for Aqara door lock #14125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Support for Aqara door lock #14125
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
17ef258
support new device model;support new LAN protocol 2.0
7ace038
format code style
54dad52
format code style
77cc7df
format code style
29b2d97
format code style
8a11675
format code style
5dc40bf
support motion 2.0
e1f4036
remove file executable permissions
d8c103f
remove unused variables
e59e19d
Merge branch 'dev' of gh:ileler/home-assistant into dev
ad14d40
support aqara-lock
706fc69
Merge branch 'local' into dev
e6cc646
trigger ci
3a1b7cb
fix lint error
c03f1bf
fix lint error
1c1670b
fix lint error
94c8db6
fix lint error
ccffaf6
Merge branch 'dev' into dev
6fadab8
support aqara door lock
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,29 @@ | |
| 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 +75,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._device_class = device_class | ||
| self._should_poll = False | ||
| self._density = 0 | ||
|
|
@@ -86,6 +99,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 +202,112 @@ 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): | ||
| """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 | ||
| }, { | ||
| '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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this and why it's called motion? |
||
| """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' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 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.""" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really want to skip the heartbeat? This was/is a workaround for motion sensors.