2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Sesame, by CANDY HOUSE."""
|
2018-06-25 17:05:07 +00:00
|
|
|
from typing import Callable
|
2017-06-05 05:06:18 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.components.lock import LockDevice, PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2017-06-08 22:21:56 +00:00
|
|
|
ATTR_BATTERY_LEVEL, CONF_EMAIL, CONF_PASSWORD,
|
|
|
|
STATE_LOCKED, STATE_UNLOCKED)
|
2017-06-05 05:06:18 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2017-06-08 22:21:56 +00:00
|
|
|
ATTR_DEVICE_ID = 'device_id'
|
2017-06-05 05:06:18 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_EMAIL): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-12-22 09:28:51 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass, config: ConfigType,
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities: Callable[[list], None], discovery_info=None):
|
2017-06-05 05:06:18 +00:00
|
|
|
"""Set up the Sesame platform."""
|
|
|
|
import pysesame
|
|
|
|
|
|
|
|
email = config.get(CONF_EMAIL)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([SesameDevice(sesame) for sesame in
|
|
|
|
pysesame.get_sesames(email, password)],
|
|
|
|
update_before_add=True)
|
2017-06-05 05:06:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SesameDevice(LockDevice):
|
|
|
|
"""Representation of a Sesame device."""
|
|
|
|
|
|
|
|
def __init__(self, sesame: object) -> None:
|
|
|
|
"""Initialize the Sesame device."""
|
|
|
|
self._sesame = sesame
|
|
|
|
|
2017-12-22 09:28:51 +00:00
|
|
|
# Cached properties from pysesame object.
|
|
|
|
self._device_id = None
|
|
|
|
self._nickname = None
|
|
|
|
self._is_unlocked = False
|
|
|
|
self._api_enabled = False
|
|
|
|
self._battery = -1
|
|
|
|
|
2017-06-05 05:06:18 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the device."""
|
2017-12-22 09:28:51 +00:00
|
|
|
return self._nickname
|
2017-06-05 05:06:18 +00:00
|
|
|
|
2017-06-08 22:21:56 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
2017-12-22 09:28:51 +00:00
|
|
|
return self._api_enabled
|
2017-06-08 22:21:56 +00:00
|
|
|
|
2017-06-05 05:06:18 +00:00
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool:
|
|
|
|
"""Return True if the device is currently locked, else False."""
|
2017-12-22 09:28:51 +00:00
|
|
|
return not self._is_unlocked
|
2017-06-05 05:06:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Get the state of the device."""
|
2017-12-22 09:28:51 +00:00
|
|
|
if self._is_unlocked:
|
2017-06-05 05:06:18 +00:00
|
|
|
return STATE_UNLOCKED
|
|
|
|
return STATE_LOCKED
|
|
|
|
|
|
|
|
def lock(self, **kwargs) -> None:
|
|
|
|
"""Lock the device."""
|
|
|
|
self._sesame.lock()
|
|
|
|
|
|
|
|
def unlock(self, **kwargs) -> None:
|
|
|
|
"""Unlock the device."""
|
|
|
|
self._sesame.unlock()
|
|
|
|
|
|
|
|
def update(self) -> None:
|
|
|
|
"""Update the internal state of the device."""
|
|
|
|
self._sesame.update_state()
|
2017-12-22 09:28:51 +00:00
|
|
|
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
|
2017-06-08 22:21:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self) -> dict:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attributes = {}
|
2017-12-22 09:28:51 +00:00
|
|
|
attributes[ATTR_DEVICE_ID] = self._device_id
|
|
|
|
attributes[ATTR_BATTERY_LEVEL] = self._battery
|
2017-06-08 22:21:56 +00:00
|
|
|
return attributes
|