Fix async IO in Sesame lock component. (#11054)

* Call update on Sesame devices to cache initial state

* Switch to using async_add_devices

* Fix line length

* Fix Lint errors

* Fix more Lint errors

* Cache pysesame properties

* Updates from CR feedback
This commit is contained in:
Ben Randall 2017-12-22 01:28:51 -08:00 committed by Pascal Vizeli
parent eeb309aea1
commit 295caeb065

View File

@ -25,46 +25,53 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config: ConfigType, def setup_platform(
add_devices: Callable[[list], None], discovery_info=None): hass, config: ConfigType,
add_devices: Callable[[list], None], discovery_info=None):
"""Set up the Sesame platform.""" """Set up the Sesame platform."""
import pysesame import pysesame
email = config.get(CONF_EMAIL) email = config.get(CONF_EMAIL)
password = config.get(CONF_PASSWORD) password = config.get(CONF_PASSWORD)
add_devices([SesameDevice(sesame) for add_devices([SesameDevice(sesame) for sesame in
sesame in pysesame.get_sesames(email, password)]) pysesame.get_sesames(email, password)],
update_before_add=True)
class SesameDevice(LockDevice): class SesameDevice(LockDevice):
"""Representation of a Sesame device.""" """Representation of a Sesame device."""
_sesame = None
def __init__(self, sesame: object) -> None: def __init__(self, sesame: object) -> None:
"""Initialize the Sesame device.""" """Initialize the Sesame device."""
self._sesame = sesame self._sesame = sesame
# Cached properties from pysesame object.
self._device_id = None
self._nickname = None
self._is_unlocked = False
self._api_enabled = False
self._battery = -1
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the device.""" """Return the name of the device."""
return self._sesame.nickname return self._nickname
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
return self._sesame.api_enabled return self._api_enabled
@property @property
def is_locked(self) -> bool: def is_locked(self) -> bool:
"""Return True if the device is currently locked, else False.""" """Return True if the device is currently locked, else False."""
return not self._sesame.is_unlocked return not self._is_unlocked
@property @property
def state(self) -> str: def state(self) -> str:
"""Get the state of the device.""" """Get the state of the device."""
if self._sesame.is_unlocked: if self._is_unlocked:
return STATE_UNLOCKED return STATE_UNLOCKED
return STATE_LOCKED return STATE_LOCKED
@ -79,11 +86,16 @@ class SesameDevice(LockDevice):
def update(self) -> None: def update(self) -> None:
"""Update the internal state of the device.""" """Update the internal state of the device."""
self._sesame.update_state() self._sesame.update_state()
self._nickname = self._sesame.nickname
self._api_enabled = self._sesame.api_enabled
self._is_unlocked = self._sesame.is_unlocked
self._device_id = self._sesame.device_id
self._battery = self._sesame.battery
@property @property
def device_state_attributes(self) -> dict: def device_state_attributes(self) -> dict:
"""Return the state attributes.""" """Return the state attributes."""
attributes = {} attributes = {}
attributes[ATTR_DEVICE_ID] = self._sesame.device_id attributes[ATTR_DEVICE_ID] = self._device_id
attributes[ATTR_BATTERY_LEVEL] = self._sesame.battery attributes[ATTR_BATTERY_LEVEL] = self._battery
return attributes return attributes