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.
This commit is contained in:
ehendrix23 2018-10-23 06:09:08 -06:00 committed by Paulus Schoutsen
parent 37a667c2de
commit 4a757b7994
3 changed files with 64 additions and 12 deletions

View File

@ -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(
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)
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)
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

View File

@ -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

View File

@ -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."""