Significantly reduce code in august integration (#32030)
* Significantly reduce code in august integration * Activity updates can now be processed by py-august this allows us to eliminate the activity sync code for the door sensors and locks * Lock and door state can now be consumed from the lock detail api which allows us to remove the status call apis and reduce the number of API calls to august * Refactor the testing method for locks (part #1) * Update homeassistant/components/august/binary_sensor.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Switch to asynctest instead of unittest for mock.patch Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
a12c4da0ca
commit
d4075fb262
17 files changed
+579
-429
No files matched your search
@@ -18,7 +18,7 @@ from homeassistant.const import (
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import discovery
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util import Throttle, dt
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -45,11 +45,6 @@ DEFAULT_ENTITY_NAMESPACE = "august"
|
||||
# avoid hitting rate limits
|
||||
MIN_TIME_BETWEEN_LOCK_DETAIL_UPDATES = timedelta(seconds=1800)
|
||||
|
||||
# Limit locks status check to 900 seconds now that
|
||||
# we get the state from the lock and unlock api calls
|
||||
# and the lock and unlock activities are now captured
|
||||
MIN_TIME_BETWEEN_LOCK_STATUS_UPDATES = timedelta(seconds=900)
|
||||
|
||||
# Doorbells need to update more frequently than locks
|
||||
# since we get an image from the doorbell api
|
||||
MIN_TIME_BETWEEN_DOORBELL_STATUS_UPDATES = timedelta(seconds=20)
|
||||
@@ -218,16 +213,11 @@ class AugustData:
|
||||
self._house_ids.add(device.house_id)
|
||||
|
||||
self._doorbell_detail_by_id = {}
|
||||
self._door_last_state_update_time_utc_by_id = {}
|
||||
self._lock_last_status_update_time_utc_by_id = {}
|
||||
self._lock_status_by_id = {}
|
||||
self._lock_detail_by_id = {}
|
||||
self._door_state_by_id = {}
|
||||
self._activities_by_id = {}
|
||||
|
||||
# We check the locks right away so we can
|
||||
# remove inoperative ones
|
||||
self._update_locks_status()
|
||||
self._update_locks_detail()
|
||||
|
||||
self._filter_inoperative_locks()
|
||||
@@ -344,8 +334,13 @@ class AugustData:
|
||||
This is called when newer activity is detected on the activity feed
|
||||
in order to keep the internal data in sync
|
||||
"""
|
||||
self._door_state_by_id[lock_id] = door_state
|
||||
self._door_last_state_update_time_utc_by_id[lock_id] = update_start_time_utc
|
||||
# When syncing the door state became available via py-august, this
|
||||
# function caused to be actively used. It will be again as we will
|
||||
# update the door state from lock/unlock operations as the august api
|
||||
# does report the door state on lock/unlock, however py-august does not
|
||||
# expose this to us yet.
|
||||
self._lock_detail_by_id[lock_id].door_state = door_state
|
||||
self._lock_detail_by_id[lock_id].door_state_datetime = update_start_time_utc
|
||||
return True
|
||||
|
||||
def update_lock_status(self, lock_id, lock_status, update_start_time_utc):
|
||||
@@ -355,8 +350,8 @@ class AugustData:
|
||||
or newer activity is detected on the activity feed
|
||||
in order to keep the internal data in sync
|
||||
"""
|
||||
self._lock_status_by_id[lock_id] = lock_status
|
||||
self._lock_last_status_update_time_utc_by_id[lock_id] = update_start_time_utc
|
||||
self._lock_detail_by_id[lock_id].lock_status = lock_status
|
||||
self._lock_detail_by_id[lock_id].lock_status_datetime = update_start_time_utc
|
||||
return True
|
||||
|
||||
def lock_has_doorsense(self, lock_id):
|
||||
@@ -367,18 +362,10 @@ class AugustData:
|
||||
return False
|
||||
return self._lock_detail_by_id[lock_id].doorsense
|
||||
|
||||
async def async_get_lock_status(self, lock_id):
|
||||
"""Return status if the door is locked or unlocked.
|
||||
|
||||
This is status for the lock itself.
|
||||
"""
|
||||
await self._async_update_locks()
|
||||
return self._lock_status_by_id.get(lock_id)
|
||||
|
||||
async def async_get_lock_detail(self, lock_id):
|
||||
"""Return lock detail."""
|
||||
await self._async_update_locks()
|
||||
return self._lock_detail_by_id.get(lock_id)
|
||||
await self._async_update_locks_detail()
|
||||
return self._lock_detail_by_id[lock_id]
|
||||
|
||||
def get_lock_name(self, device_id):
|
||||
"""Return lock name as August has it stored."""
|
||||
@@ -386,85 +373,6 @@ class AugustData:
|
||||
if lock.device_id == device_id:
|
||||
return lock.device_name
|
||||
|
||||
async def async_get_door_state(self, lock_id):
|
||||
"""Return status if the door is open or closed.
|
||||
|
||||
This is the status from the door sensor.
|
||||
"""
|
||||
await self._async_update_locks_status()
|
||||
return self._door_state_by_id.get(lock_id)
|
||||
|
||||
async def _async_update_locks(self):
|
||||
await self._async_update_locks_status()
|
||||
await self._async_update_locks_detail()
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_LOCK_STATUS_UPDATES)
|
||||
async def _async_update_locks_status(self):
|
||||
await self._hass.async_add_executor_job(self._update_locks_status)
|
||||
|
||||
def _update_locks_status(self):
|
||||
status_by_id = {}
|
||||
state_by_id = {}
|
||||
lock_last_status_update_by_id = {}
|
||||
door_last_state_update_by_id = {}
|
||||
|
||||
_LOGGER.debug("Start retrieving lock and door status")
|
||||
for lock in self._locks:
|
||||
update_start_time_utc = dt.utcnow()
|
||||
_LOGGER.debug("Updating lock and door status for %s", lock.device_name)
|
||||
try:
|
||||
(
|
||||
status_by_id[lock.device_id],
|
||||
state_by_id[lock.device_id],
|
||||
) = self._api.get_lock_status(
|
||||
self._access_token, lock.device_id, door_status=True
|
||||
)
|
||||
# Since there is a a race condition between calling the
|
||||
# lock and activity apis, we set the last update time
|
||||
# BEFORE making the api call since we will compare this
|
||||
# to activity later we want activity to win over stale lock/door
|
||||
# state.
|
||||
lock_last_status_update_by_id[lock.device_id] = update_start_time_utc
|
||||
door_last_state_update_by_id[lock.device_id] = update_start_time_utc
|
||||
except RequestException as ex:
|
||||
_LOGGER.error(
|
||||
"Request error trying to retrieve lock and door status for %s. %s",
|
||||
lock.device_name,
|
||||
ex,
|
||||
)
|
||||
status_by_id[lock.device_id] = None
|
||||
state_by_id[lock.device_id] = None
|
||||
except Exception:
|
||||
status_by_id[lock.device_id] = None
|
||||
state_by_id[lock.device_id] = None
|
||||
raise
|
||||
|
||||
_LOGGER.debug("Completed retrieving lock and door status")
|
||||
self._lock_status_by_id = status_by_id
|
||||
self._door_state_by_id = state_by_id
|
||||
self._door_last_state_update_time_utc_by_id = door_last_state_update_by_id
|
||||
self._lock_last_status_update_time_utc_by_id = lock_last_status_update_by_id
|
||||
|
||||
def get_last_lock_status_update_time_utc(self, lock_id):
|
||||
"""Return the last time that a lock status update was seen from the august API."""
|
||||
# Since the activity api is called more frequently than
|
||||
# the lock api it is possible that the lock has not
|
||||
# been updated yet
|
||||
if lock_id not in self._lock_last_status_update_time_utc_by_id:
|
||||
return dt.utc_from_timestamp(0)
|
||||
|
||||
return self._lock_last_status_update_time_utc_by_id[lock_id]
|
||||
|
||||
def get_last_door_state_update_time_utc(self, lock_id):
|
||||
"""Return the last time that a door status update was seen from the august API."""
|
||||
# Since the activity api is called more frequently than
|
||||
# the lock api it is possible that the door has not
|
||||
# been updated yet
|
||||
if lock_id not in self._door_last_state_update_time_utc_by_id:
|
||||
return dt.utc_from_timestamp(0)
|
||||
|
||||
return self._door_last_state_update_time_utc_by_id[lock_id]
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_LOCK_DETAIL_UPDATES)
|
||||
async def _async_update_locks_detail(self):
|
||||
await self._hass.async_add_executor_job(self._update_locks_detail)
|
||||
|
||||
Reference in new issue
Block a user