Skip to content

Commit

Permalink
Set available property (#17706)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ehendrix23 authored and balloob committed Oct 23, 2018
1 parent 37a667c commit 4a757b7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
51 changes: 43 additions & 8 deletions homeassistant/components/august.py
Expand Up @@ -225,8 +225,17 @@ def _update_doorbells(self):
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
Expand Down Expand Up @@ -260,8 +269,17 @@ def _update_doors(self):
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
Expand All @@ -275,10 +293,27 @@ def _update_locks(self):
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
Expand Down
17 changes: 13 additions & 4 deletions homeassistant/components/binary_sensor/august.py
Expand Up @@ -19,14 +19,15 @@

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


Expand Down Expand Up @@ -138,9 +139,10 @@ def update(self):
"""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):
Expand All @@ -152,6 +154,12 @@ def __init__(self, data, sensor_type, doorbell):
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):
Expand All @@ -173,3 +181,4 @@ def update(self):
"""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
8 changes: 8 additions & 0 deletions homeassistant/components/lock/august.py
Expand Up @@ -40,6 +40,7 @@ def __init__(self, data, lock):
self._lock_status = None
self._lock_detail = None
self._changed_by = None
self._available = False

def lock(self, **kwargs):
"""Lock the device."""
Expand All @@ -52,6 +53,8 @@ def unlock(self, **kwargs):
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
Expand All @@ -67,6 +70,11 @@ def name(self):
"""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."""
Expand Down

0 comments on commit 4a757b7

Please sign in to comment.