From 4a757b79945584e38983ba7822062ddbd36c82e1 Mon Sep 17 00:00:00 2001 From: ehendrix23 Date: Tue, 23 Oct 2018 06:09:08 -0600 Subject: [PATCH] Set available property (#17706) Will set the available property to False if unable to communicate with August lock or doorbell. HTTP request errors (i.e. timeout, connection error, HTTP error) will not result in traceback. Instead an error will be logged. --- homeassistant/components/august.py | 51 ++++++++++++++++--- .../components/binary_sensor/august.py | 17 +++++-- homeassistant/components/lock/august.py | 8 +++ 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/august.py b/homeassistant/components/august.py index 850d972c373..ce8e3d8de11 100644 --- a/homeassistant/components/august.py +++ b/homeassistant/components/august.py @@ -225,8 +225,17 @@ class AugustData: for doorbell in self._doorbells: _LOGGER.debug("Updating status for %s", doorbell.device_name) - detail_by_id[doorbell.device_id] = self._api.get_doorbell_detail( - self._access_token, doorbell.device_id) + try: + detail_by_id[doorbell.device_id] =\ + self._api.get_doorbell_detail( + self._access_token, doorbell.device_id) + except RequestException as ex: + _LOGGER.error("Request error trying to retrieve doorbell" + " status for %s. %s", doorbell.device_name, ex) + detail_by_id[doorbell.device_id] = None + except Exception: + detail_by_id[doorbell.device_id] = None + raise _LOGGER.debug("Completed retrieving doorbell details") self._doorbell_detail_by_id = detail_by_id @@ -260,8 +269,17 @@ class AugustData: for lock in self._locks: _LOGGER.debug("Updating status for %s", lock.device_name) - state_by_id[lock.device_id] = self._api.get_lock_door_status( - self._access_token, lock.device_id) + + try: + state_by_id[lock.device_id] = self._api.get_lock_door_status( + self._access_token, lock.device_id) + except RequestException as ex: + _LOGGER.error("Request error trying to retrieve door" + " status for %s. %s", lock.device_name, ex) + state_by_id[lock.device_id] = None + except Exception: + state_by_id[lock.device_id] = None + raise _LOGGER.debug("Completed retrieving door status") self._door_state_by_id = state_by_id @@ -275,10 +293,27 @@ class AugustData: for lock in self._locks: _LOGGER.debug("Updating status for %s", lock.device_name) - status_by_id[lock.device_id] = self._api.get_lock_status( - self._access_token, lock.device_id) - detail_by_id[lock.device_id] = self._api.get_lock_detail( - self._access_token, lock.device_id) + try: + status_by_id[lock.device_id] = self._api.get_lock_status( + self._access_token, lock.device_id) + except RequestException as ex: + _LOGGER.error("Request error trying to retrieve door" + " status for %s. %s", lock.device_name, ex) + status_by_id[lock.device_id] = None + except Exception: + status_by_id[lock.device_id] = None + raise + + try: + detail_by_id[lock.device_id] = self._api.get_lock_detail( + self._access_token, lock.device_id) + except RequestException as ex: + _LOGGER.error("Request error trying to retrieve door" + " details for %s. %s", lock.device_name, ex) + detail_by_id[lock.device_id] = None + except Exception: + detail_by_id[lock.device_id] = None + raise _LOGGER.debug("Completed retrieving locks status") self._lock_status_by_id = status_by_id diff --git a/homeassistant/components/binary_sensor/august.py b/homeassistant/components/binary_sensor/august.py index 55b31a6da5f..4116a791b01 100644 --- a/homeassistant/components/binary_sensor/august.py +++ b/homeassistant/components/binary_sensor/august.py @@ -19,14 +19,15 @@ SCAN_INTERVAL = timedelta(seconds=5) def _retrieve_door_state(data, lock): """Get the latest state of the DoorSense sensor.""" - from august.lock import LockDoorStatus - doorstate = data.get_door_state(lock.device_id) - return doorstate == LockDoorStatus.OPEN + return data.get_door_state(lock.device_id) def _retrieve_online_state(data, doorbell): """Get the latest state of the sensor.""" detail = data.get_doorbell_detail(doorbell.device_id) + if detail is None: + return None + return detail.is_online @@ -138,9 +139,10 @@ class AugustDoorBinarySensor(BinarySensorDevice): """Get the latest state of the sensor.""" state_provider = SENSOR_TYPES_DOOR[self._sensor_type][2] self._state = state_provider(self._data, self._door) + self._available = self._state is not None from august.lock import LockDoorStatus - self._available = self._state != LockDoorStatus.UNKNOWN + self._state = self._state == LockDoorStatus.OPEN class AugustDoorbellBinarySensor(BinarySensorDevice): @@ -152,6 +154,12 @@ class AugustDoorbellBinarySensor(BinarySensorDevice): self._sensor_type = sensor_type self._doorbell = doorbell self._state = None + self._available = False + + @property + def available(self): + """Return the availability of this sensor.""" + return self._available @property def is_on(self): @@ -173,3 +181,4 @@ class AugustDoorbellBinarySensor(BinarySensorDevice): """Get the latest state of the sensor.""" state_provider = SENSOR_TYPES_DOORBELL[self._sensor_type][2] self._state = state_provider(self._data, self._doorbell) + self._available = self._state is not None diff --git a/homeassistant/components/lock/august.py b/homeassistant/components/lock/august.py index e8949255ee9..ce6792ceb39 100644 --- a/homeassistant/components/lock/august.py +++ b/homeassistant/components/lock/august.py @@ -40,6 +40,7 @@ class AugustLock(LockDevice): self._lock_status = None self._lock_detail = None self._changed_by = None + self._available = False def lock(self, **kwargs): """Lock the device.""" @@ -52,6 +53,8 @@ class AugustLock(LockDevice): def update(self): """Get the latest state of the sensor.""" self._lock_status = self._data.get_lock_status(self._lock.device_id) + self._available = self._lock_status is not None + self._lock_detail = self._data.get_lock_detail(self._lock.device_id) from august.activity import ActivityType @@ -67,6 +70,11 @@ class AugustLock(LockDevice): """Return the name of this device.""" return self._lock.device_name + @property + def available(self): + """Return the availability of this sensor.""" + return self._available + @property def is_locked(self): """Return true if device is on."""