2019-02-13 20:21:14 +00:00
|
|
|
"""Support for August lock."""
|
2019-03-21 05:56:46 +00:00
|
|
|
import logging
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-21 05:06:24 +00:00
|
|
|
from august.activity import ActivityType
|
2019-10-18 00:06:41 +00:00
|
|
|
from august.lock import LockStatus
|
2020-02-21 05:06:24 +00:00
|
|
|
from august.util import update_lock_detail_from_activity
|
2019-10-18 00:06:41 +00:00
|
|
|
|
2018-02-18 08:24:51 +00:00
|
|
|
from homeassistant.components.lock import LockDevice
|
|
|
|
from homeassistant.const import ATTR_BATTERY_LEVEL
|
2020-02-27 02:48:44 +00:00
|
|
|
from homeassistant.core import callback
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-28 03:44:23 +00:00
|
|
|
from .const import DATA_AUGUST, DOMAIN
|
|
|
|
from .entity import AugustEntityMixin
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-10-19 07:37:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-02-18 08:24:51 +00:00
|
|
|
"""Set up August locks."""
|
2020-02-25 18:18:15 +00:00
|
|
|
data = hass.data[DOMAIN][config_entry.entry_id][DATA_AUGUST]
|
2018-02-18 08:24:51 +00:00
|
|
|
devices = []
|
|
|
|
|
|
|
|
for lock in data.locks:
|
2018-10-19 07:37:02 +00:00
|
|
|
_LOGGER.debug("Adding lock for %s", lock.device_name)
|
2018-02-18 08:24:51 +00:00
|
|
|
devices.append(AugustLock(data, lock))
|
|
|
|
|
2020-02-16 05:08:52 +00:00
|
|
|
async_add_entities(devices, True)
|
2018-02-18 08:24:51 +00:00
|
|
|
|
|
|
|
|
2020-02-28 03:44:23 +00:00
|
|
|
class AugustLock(AugustEntityMixin, LockDevice):
|
2018-02-18 08:24:51 +00:00
|
|
|
"""Representation of an August lock."""
|
|
|
|
|
2020-02-28 03:44:23 +00:00
|
|
|
def __init__(self, data, device):
|
2018-02-18 08:24:51 +00:00
|
|
|
"""Initialize the lock."""
|
2020-02-28 03:44:23 +00:00
|
|
|
super().__init__(data, device)
|
2018-02-18 08:24:51 +00:00
|
|
|
self._data = data
|
2020-02-28 03:44:23 +00:00
|
|
|
self._device = device
|
2018-02-18 08:24:51 +00:00
|
|
|
self._lock_status = None
|
|
|
|
self._changed_by = None
|
2018-10-23 12:09:08 +00:00
|
|
|
self._available = False
|
2020-02-28 03:44:23 +00:00
|
|
|
self._update_from_data()
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-16 05:08:52 +00:00
|
|
|
async def async_lock(self, **kwargs):
|
2018-02-18 08:24:51 +00:00
|
|
|
"""Lock the device."""
|
2020-02-23 21:54:35 +00:00
|
|
|
await self._call_lock_operation(self._data.lock)
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-16 05:08:52 +00:00
|
|
|
async def async_unlock(self, **kwargs):
|
2018-02-18 08:24:51 +00:00
|
|
|
"""Unlock the device."""
|
2020-02-23 21:54:35 +00:00
|
|
|
await self._call_lock_operation(self._data.unlock)
|
|
|
|
|
|
|
|
async def _call_lock_operation(self, lock_operation):
|
|
|
|
activities = await self.hass.async_add_executor_job(
|
2020-02-28 03:44:23 +00:00
|
|
|
lock_operation, self._device_id
|
2020-02-16 05:08:52 +00:00
|
|
|
)
|
2020-02-28 03:44:23 +00:00
|
|
|
detail = self._detail
|
2020-02-23 21:54:35 +00:00
|
|
|
for lock_activity in activities:
|
2020-02-28 03:44:23 +00:00
|
|
|
update_lock_detail_from_activity(detail, lock_activity)
|
2020-02-11 16:57:26 +00:00
|
|
|
|
2020-02-23 21:54:35 +00:00
|
|
|
if self._update_lock_status_from_detail():
|
2020-02-28 03:44:23 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"async_signal_device_id_update (from lock operation): %s",
|
|
|
|
self._device_id,
|
2020-02-27 02:48:44 +00:00
|
|
|
)
|
2020-02-28 03:44:23 +00:00
|
|
|
self._data.async_signal_device_id_update(self._device_id)
|
2020-02-23 21:54:35 +00:00
|
|
|
|
|
|
|
def _update_lock_status_from_detail(self):
|
2020-02-28 03:44:23 +00:00
|
|
|
detail = self._detail
|
|
|
|
lock_status = detail.lock_status
|
|
|
|
self._available = detail.bridge_is_online
|
2020-02-25 18:18:15 +00:00
|
|
|
|
|
|
|
if self._lock_status != lock_status:
|
|
|
|
self._lock_status = lock_status
|
2020-02-23 21:54:35 +00:00
|
|
|
return True
|
|
|
|
return False
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-28 03:44:23 +00:00
|
|
|
@callback
|
|
|
|
def _update_from_data(self):
|
2020-02-16 05:08:52 +00:00
|
|
|
"""Get the latest state of the sensor and update activity."""
|
2020-02-28 03:44:23 +00:00
|
|
|
lock_detail = self._detail
|
|
|
|
lock_activity = self._data.activity_stream.get_latest_device_activity(
|
|
|
|
self._device_id, [ActivityType.LOCK_OPERATION]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2020-02-12 23:35:07 +00:00
|
|
|
if lock_activity is not None:
|
|
|
|
self._changed_by = lock_activity.operated_by
|
2020-02-28 03:44:23 +00:00
|
|
|
update_lock_detail_from_activity(lock_detail, lock_activity)
|
2020-02-11 16:57:26 +00:00
|
|
|
|
2020-02-23 21:54:35 +00:00
|
|
|
self._update_lock_status_from_detail()
|
2018-02-18 08:24:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this device."""
|
2020-02-28 03:44:23 +00:00
|
|
|
return self._device.device_name
|
2018-02-18 08:24:51 +00:00
|
|
|
|
2018-10-23 12:09:08 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return the availability of this sensor."""
|
|
|
|
return self._available
|
|
|
|
|
2018-02-18 08:24:51 +00:00
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if device is on."""
|
2020-02-25 18:18:15 +00:00
|
|
|
if self._lock_status is None or self._lock_status is LockStatus.UNKNOWN:
|
|
|
|
return None
|
2018-02-18 08:24:51 +00:00
|
|
|
return self._lock_status is LockStatus.LOCKED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def changed_by(self):
|
|
|
|
"""Last change triggered by."""
|
|
|
|
return self._changed_by
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device specific state attributes."""
|
2020-02-28 03:44:23 +00:00
|
|
|
attributes = {ATTR_BATTERY_LEVEL: self._detail.battery_level}
|
2020-02-08 19:22:48 +00:00
|
|
|
|
2020-02-28 03:44:23 +00:00
|
|
|
if self._detail.keypad is not None:
|
|
|
|
attributes["keypad_battery_level"] = self._detail.keypad.battery_level
|
2020-02-08 19:22:48 +00:00
|
|
|
|
|
|
|
return attributes
|
2019-02-11 19:48:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Get the unique id of the lock."""
|
2020-02-28 03:44:23 +00:00
|
|
|
return f"{self._device_id:s}_lock"
|