core/homeassistant/components/xiaomi_aqara/lock.py

83 lines
2.6 KiB
Python
Raw Normal View History

"""Support for Xiaomi Aqara locks."""
from homeassistant.components.lock import LockEntity
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED
2018-05-22 08:40:11 +00:00
from homeassistant.core import callback
from homeassistant.helpers.event import async_call_later
Add Xiaomi Aqara Config Flow (#35595) * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow First tested and working version * Remove depricated discovery * Add Xiaomi Aqara Config Flow * Add Xiaomi Aqara tests * Update .coveragerc * Update requirements_test_all.txt * fix spelling mistake * fix select scheme * fix wrong conflict resolve * add IP to zeroconf discovery title * black styling * add getmac requirement Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> * add getmac * add getmac * Clean up * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * resolve data storage * move format_mac down * Remove discovery_retry from config flow * remove unused strings * fix styling * fix black styling * fix tests * remove mac connection This is needed to prevent a conflict with the Xiaomi Miio integration that I discovered during testing. * fix flake8 * remove getmac depandance * check for inavlid_interface + test * Validate gateway key * add invalid key tests * Fix spelling * Only set up sensors if no key Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-06-22 09:54:17 +00:00
from . import XiaomiDevice
from .const import DOMAIN, GATEWAYS_KEY
2018-05-22 08:40:11 +00:00
2019-07-31 19:25:30 +00:00
FINGER_KEY = "fing_verified"
PASSWORD_KEY = "psw_verified"
CARD_KEY = "card_verified"
VERIFIED_WRONG_KEY = "verified_wrong"
2018-05-22 08:40:11 +00:00
2019-07-31 19:25:30 +00:00
ATTR_VERIFIED_WRONG_TIMES = "verified_wrong_times"
2018-05-22 08:40:11 +00:00
UNLOCK_MAINTAIN_TIME = 5
Add Xiaomi Aqara Config Flow (#35595) * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow First tested and working version * Remove depricated discovery * Add Xiaomi Aqara Config Flow * Add Xiaomi Aqara tests * Update .coveragerc * Update requirements_test_all.txt * fix spelling mistake * fix select scheme * fix wrong conflict resolve * add IP to zeroconf discovery title * black styling * add getmac requirement Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> * add getmac * add getmac * Clean up * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * resolve data storage * move format_mac down * Remove discovery_retry from config flow * remove unused strings * fix styling * fix black styling * fix tests * remove mac connection This is needed to prevent a conflict with the Xiaomi Miio integration that I discovered during testing. * fix flake8 * remove getmac depandance * check for inavlid_interface + test * Validate gateway key * add invalid key tests * Fix spelling * Only set up sensors if no key Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-06-22 09:54:17 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
2018-05-22 08:40:11 +00:00
"""Perform the setup for Xiaomi devices."""
Add Xiaomi Aqara Config Flow (#35595) * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow First tested and working version * Remove depricated discovery * Add Xiaomi Aqara Config Flow * Add Xiaomi Aqara tests * Update .coveragerc * Update requirements_test_all.txt * fix spelling mistake * fix select scheme * fix wrong conflict resolve * add IP to zeroconf discovery title * black styling * add getmac requirement Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> * add getmac * add getmac * Clean up * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * resolve data storage * move format_mac down * Remove discovery_retry from config flow * remove unused strings * fix styling * fix black styling * fix tests * remove mac connection This is needed to prevent a conflict with the Xiaomi Miio integration that I discovered during testing. * fix flake8 * remove getmac depandance * check for inavlid_interface + test * Validate gateway key * add invalid key tests * Fix spelling * Only set up sensors if no key Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-06-22 09:54:17 +00:00
entities = []
gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
for device in gateway.devices["lock"]:
model = device["model"]
if model == "lock.aq1":
entities.append(XiaomiAqaraLock(device, "Lock", gateway, config_entry))
async_add_entities(entities)
2018-05-22 08:40:11 +00:00
class XiaomiAqaraLock(LockEntity, XiaomiDevice):
2018-05-22 08:40:11 +00:00
"""Representation of a XiaomiAqaraLock."""
Add Xiaomi Aqara Config Flow (#35595) * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow First tested and working version * Remove depricated discovery * Add Xiaomi Aqara Config Flow * Add Xiaomi Aqara tests * Update .coveragerc * Update requirements_test_all.txt * fix spelling mistake * fix select scheme * fix wrong conflict resolve * add IP to zeroconf discovery title * black styling * add getmac requirement Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> * add getmac * add getmac * Clean up * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * resolve data storage * move format_mac down * Remove discovery_retry from config flow * remove unused strings * fix styling * fix black styling * fix tests * remove mac connection This is needed to prevent a conflict with the Xiaomi Miio integration that I discovered during testing. * fix flake8 * remove getmac depandance * check for inavlid_interface + test * Validate gateway key * add invalid key tests * Fix spelling * Only set up sensors if no key Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-06-22 09:54:17 +00:00
def __init__(self, device, name, xiaomi_hub, config_entry):
2018-05-22 08:40:11 +00:00
"""Initialize the XiaomiAqaraLock."""
self._changed_by = 0
self._verified_wrong_times = 0
Add Xiaomi Aqara Config Flow (#35595) * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow * Xiaomi Aqara Config Flow First tested and working version * Remove depricated discovery * Add Xiaomi Aqara Config Flow * Add Xiaomi Aqara tests * Update .coveragerc * Update requirements_test_all.txt * fix spelling mistake * fix select scheme * fix wrong conflict resolve * add IP to zeroconf discovery title * black styling * add getmac requirement Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> * add getmac * add getmac * Clean up * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/xiaomi_aqara/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * resolve data storage * move format_mac down * Remove discovery_retry from config flow * remove unused strings * fix styling * fix black styling * fix tests * remove mac connection This is needed to prevent a conflict with the Xiaomi Miio integration that I discovered during testing. * fix flake8 * remove getmac depandance * check for inavlid_interface + test * Validate gateway key * add invalid key tests * Fix spelling * Only set up sensors if no key Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-06-22 09:54:17 +00:00
super().__init__(device, name, xiaomi_hub, config_entry)
2018-05-22 08:40:11 +00:00
@property
def is_locked(self) -> bool:
"""Return true if lock is locked."""
if self._state is not None:
return self._state == STATE_LOCKED
@property
def changed_by(self) -> int:
"""Last change triggered by."""
return self._changed_by
@property
def extra_state_attributes(self) -> dict:
2018-05-22 08:40:11 +00:00
"""Return the state attributes."""
2019-07-31 19:25:30 +00:00
attributes = {ATTR_VERIFIED_WRONG_TIMES: self._verified_wrong_times}
2018-05-22 08:40:11 +00:00
return attributes
@callback
def clear_unlock_state(self, _):
"""Clear unlock state automatically."""
self._state = STATE_LOCKED
self.async_write_ha_state()
2018-05-22 08:40:11 +00:00
def parse_data(self, data, raw_data):
"""Parse data sent by gateway."""
value = data.get(VERIFIED_WRONG_KEY)
if value is not None:
self._verified_wrong_times = int(value)
return True
for key in (FINGER_KEY, PASSWORD_KEY, CARD_KEY):
value = data.get(key)
if value is not None:
self._changed_by = int(value)
self._verified_wrong_times = 0
self._state = STATE_UNLOCKED
2019-07-31 19:25:30 +00:00
async_call_later(
self.hass, UNLOCK_MAINTAIN_TIME, self.clear_unlock_state
)
2018-05-22 08:40:11 +00:00
return True
return False