Convert august to async so a token refresh lock can be used (#31848)

* Convert august to async so a token refresh lock can be used

* Update comment since we now have a lock

* Do not mock the lock

* Address review items
This commit is contained in:
J. Nick Koston 2020-02-15 23:08:52 -06:00 committed by GitHub
parent fb8cbc2e93
commit f6d9e6b6c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 93 deletions

View file

@ -15,35 +15,39 @@ _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=10)
def _retrieve_door_state(data, lock):
async def _async_retrieve_door_state(data, lock):
"""Get the latest state of the DoorSense sensor."""
return data.get_door_state(lock.device_id)
return await data.async_get_door_state(lock.device_id)
def _retrieve_online_state(data, doorbell):
async def _async_retrieve_online_state(data, doorbell):
"""Get the latest state of the sensor."""
detail = data.get_doorbell_detail(doorbell.device_id)
detail = await data.async_get_doorbell_detail(doorbell.device_id)
if detail is None:
return None
return detail.is_online
def _retrieve_motion_state(data, doorbell):
async def _async_retrieve_motion_state(data, doorbell):
return _activity_time_based_state(
return await _async_activity_time_based_state(
data, doorbell, [ActivityType.DOORBELL_MOTION, ActivityType.DOORBELL_DING]
)
def _retrieve_ding_state(data, doorbell):
async def _async_retrieve_ding_state(data, doorbell):
return _activity_time_based_state(data, doorbell, [ActivityType.DOORBELL_DING])
return await _async_activity_time_based_state(
data, doorbell, [ActivityType.DOORBELL_DING]
)
def _activity_time_based_state(data, doorbell, activity_types):
async def _async_activity_time_based_state(data, doorbell, activity_types):
"""Get the latest state of the sensor."""
latest = data.get_latest_device_activity(doorbell.device_id, *activity_types)
latest = await data.async_get_latest_device_activity(
doorbell.device_id, *activity_types
)
if latest is not None:
start = latest.activity_start_time
@ -52,25 +56,25 @@ def _activity_time_based_state(data, doorbell, activity_types):
return None
# Sensor types: Name, device_class, state_provider
SENSOR_TYPES_DOOR = {"door_open": ["Open", "door", _retrieve_door_state]}
# Sensor types: Name, device_class, async_state_provider
SENSOR_TYPES_DOOR = {"door_open": ["Open", "door", _async_retrieve_door_state]}
SENSOR_TYPES_DOORBELL = {
"doorbell_ding": ["Ding", "occupancy", _retrieve_ding_state],
"doorbell_motion": ["Motion", "motion", _retrieve_motion_state],
"doorbell_online": ["Online", "connectivity", _retrieve_online_state],
"doorbell_ding": ["Ding", "occupancy", _async_retrieve_ding_state],
"doorbell_motion": ["Motion", "motion", _async_retrieve_motion_state],
"doorbell_online": ["Online", "connectivity", _async_retrieve_online_state],
}
def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the August binary sensors."""
data = hass.data[DATA_AUGUST]
devices = []
for door in data.locks:
for sensor_type in SENSOR_TYPES_DOOR:
state_provider = SENSOR_TYPES_DOOR[sensor_type][2]
if state_provider(data, door) is LockDoorStatus.UNKNOWN:
async_state_provider = SENSOR_TYPES_DOOR[sensor_type][2]
if await async_state_provider(data, door) is LockDoorStatus.UNKNOWN:
_LOGGER.debug(
"Not adding sensor class %s for lock %s ",
SENSOR_TYPES_DOOR[sensor_type][1],
@ -94,7 +98,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
)
devices.append(AugustDoorbellBinarySensor(data, sensor_type, doorbell))
add_entities(devices, True)
async_add_entities(devices, True)
class AugustDoorBinarySensor(BinarySensorDevice):
@ -130,15 +134,15 @@ class AugustDoorBinarySensor(BinarySensorDevice):
self._door.device_name, SENSOR_TYPES_DOOR[self._sensor_type][0]
)
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)
async def async_update(self):
"""Get the latest state of the sensor and update activity."""
async_state_provider = SENSOR_TYPES_DOOR[self._sensor_type][2]
self._state = await async_state_provider(self._data, self._door)
self._available = self._state is not None
self._state = self._state == LockDoorStatus.OPEN
door_activity = self._data.get_latest_device_activity(
door_activity = await self._data.async_get_latest_device_activity(
self._door.device_id, ActivityType.DOOR_OPERATION
)
@ -226,10 +230,10 @@ class AugustDoorbellBinarySensor(BinarySensorDevice):
self._doorbell.device_name, SENSOR_TYPES_DOORBELL[self._sensor_type][0]
)
def update(self):
async def async_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)
async_state_provider = SENSOR_TYPES_DOORBELL[self._sensor_type][2]
self._state = await async_state_provider(self._data, self._doorbell)
self._available = self._doorbell.is_online
@property