Restore august lock changed_by state on restart (#32340)

* Various code review items from previous PRs

* Add a test for fetching the doorbell camera image

* Switch to using UNIT_PERCENTAGE for battery charge unit

* Add tests for changed_by
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-02-29 12:12:50 +01:00
1 parent e9a7b66df6
commit be14b94705
8 files changed
+112 -50

No files matched your search

@@ -8,6 +8,7 @@ from august.util import update_lock_detail_from_activity
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OCCUPANCY,
BinarySensorDevice,
@@ -116,24 +117,22 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorDevice):
self._data = data
self._sensor_type = sensor_type
self._device = device
self._state = None
self._available = False
self._update_from_data()
@property
def available(self):
"""Return the availability of this sensor."""
return self._available
return self._detail.bridge_is_online
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
return self._detail.door_state == LockDoorStatus.OPEN
@property
def device_class(self):
"""Return the class of this device."""
return "door"
return DEVICE_CLASS_DOOR
@property
def name(self):
@@ -146,15 +145,9 @@ class AugustDoorBinarySensor(AugustEntityMixin, BinarySensorDevice):
door_activity = self._data.activity_stream.get_latest_device_activity(
self._device_id, [ActivityType.DOOR_OPERATION]
)
detail = self._detail
if door_activity is not None:
update_lock_detail_from_activity(detail, door_activity)
lock_door_state = detail.door_state
self._available = detail.bridge_is_online
self._state = lock_door_state == LockDoorStatus.OPEN
update_lock_detail_from_activity(self._detail, door_activity)
@property
def unique_id(self) -> str:
+2 -1
View File
@@ -3,13 +3,14 @@
import logging
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from . import DEFAULT_NAME, DOMAIN
_LOGGER = logging.getLogger(__name__)
class AugustEntityMixin:
class AugustEntityMixin(Entity):
"""Base implementation for August device."""
def __init__(self, data, device):
+19 -11
View File
@@ -5,9 +5,10 @@ from august.activity import ActivityType
from august.lock import LockStatus
from august.util import update_lock_detail_from_activity
from homeassistant.components.lock import LockDevice
from homeassistant.components.lock import ATTR_CHANGED_BY, LockDevice
from homeassistant.const import ATTR_BATTERY_LEVEL
from homeassistant.core import callback
from homeassistant.helpers.restore_state import RestoreEntity
from .const import DATA_AUGUST, DOMAIN
from .entity import AugustEntityMixin
@@ -27,7 +28,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(devices, True)
class AugustLock(AugustEntityMixin, LockDevice):
class AugustLock(AugustEntityMixin, RestoreEntity, LockDevice):
"""Representation of an August lock."""
def __init__(self, data, device):
@@ -52,9 +53,8 @@ class AugustLock(AugustEntityMixin, LockDevice):
activities = await self.hass.async_add_executor_job(
lock_operation, self._device_id
)
detail = self._detail
for lock_activity in activities:
update_lock_detail_from_activity(detail, lock_activity)
update_lock_detail_from_activity(self._detail, lock_activity)
if self._update_lock_status_from_detail():
_LOGGER.debug(
@@ -64,26 +64,23 @@ class AugustLock(AugustEntityMixin, LockDevice):
self._data.async_signal_device_id_update(self._device_id)
def _update_lock_status_from_detail(self):
detail = self._detail
lock_status = detail.lock_status
self._available = detail.bridge_is_online
self._available = self._detail.bridge_is_online
if self._lock_status != lock_status:
self._lock_status = lock_status
if self._lock_status != self._detail.lock_status:
self._lock_status = self._detail.lock_status
return True
return False
@callback
def _update_from_data(self):
"""Get the latest state of the sensor and update activity."""
lock_detail = self._detail
lock_activity = self._data.activity_stream.get_latest_device_activity(
self._device_id, [ActivityType.LOCK_OPERATION]
)
if lock_activity is not None:
self._changed_by = lock_activity.operated_by
update_lock_detail_from_activity(lock_detail, lock_activity)
update_lock_detail_from_activity(self._detail, lock_activity)
self._update_lock_status_from_detail()
@@ -119,6 +116,17 @@ class AugustLock(AugustEntityMixin, LockDevice):
return attributes
async def async_added_to_hass(self):
"""Restore ATTR_CHANGED_BY on startup since it is likely no longer in the activity log."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if not last_state:
return
if ATTR_CHANGED_BY in last_state.attributes:
self._changed_by = last_state.attributes[ATTR_CHANGED_BY]
@property
def unique_id(self) -> str:
"""Get the unique id of the lock."""
+2 -1
View File
@@ -2,6 +2,7 @@
import logging
from homeassistant.components.sensor import DEVICE_CLASS_BATTERY
from homeassistant.const import UNIT_PERCENTAGE
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
@@ -111,7 +112,7 @@ class AugustBatterySensor(AugustEntityMixin, Entity):
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%" # UNIT_PERCENTAGE will be available after PR#32094
return UNIT_PERCENTAGE
@property
def device_class(self):