2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Vera locks."""
|
2016-06-30 00:28:20 +00:00
|
|
|
import logging
|
2020-04-03 07:49:50 +00:00
|
|
|
from typing import Callable, List
|
2016-06-30 00:28:20 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.components.lock import (
|
|
|
|
DOMAIN as PLATFORM_DOMAIN,
|
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
LockDevice,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from . import VeraDevice
|
|
|
|
from .const import DOMAIN
|
2016-06-30 00:28:20 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-10-02 07:50:45 +00:00
|
|
|
ATTR_LAST_USER_NAME = "changed_by_name"
|
|
|
|
ATTR_LOW_BATTERY = "low_battery"
|
|
|
|
|
2016-06-30 00:28:20 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
|
|
|
controller_data = hass.data[DOMAIN]
|
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2020-04-03 07:49:50 +00:00
|
|
|
VeraLock(device, controller_data.controller)
|
|
|
|
for device in controller_data.devices.get(PLATFORM_DOMAIN)
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-06-30 00:28:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VeraLock(VeraDevice, LockDevice):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Representation of a Vera lock."""
|
2016-06-30 00:28:20 +00:00
|
|
|
|
|
|
|
def __init__(self, vera_device, controller):
|
|
|
|
"""Initialize the Vera device."""
|
|
|
|
self._state = None
|
|
|
|
VeraDevice.__init__(self, vera_device, controller)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2016-06-30 00:28:20 +00:00
|
|
|
|
|
|
|
def lock(self, **kwargs):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Lock the device."""
|
2016-06-30 00:28:20 +00:00
|
|
|
self.vera_device.lock()
|
|
|
|
self._state = STATE_LOCKED
|
|
|
|
|
|
|
|
def unlock(self, **kwargs):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Unlock the device."""
|
2016-06-30 00:28:20 +00:00
|
|
|
self.vera_device.unlock()
|
|
|
|
self._state = STATE_UNLOCKED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state == STATE_LOCKED
|
|
|
|
|
2019-10-02 07:50:45 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Who unlocked the lock and did a low battery alert fire.
|
|
|
|
|
|
|
|
Reports on the previous poll cycle.
|
|
|
|
changed_by_name is a string like 'Bob'.
|
|
|
|
low_battery is 1 if an alert fired, 0 otherwise.
|
|
|
|
"""
|
|
|
|
data = super().device_state_attributes
|
|
|
|
|
|
|
|
last_user = self.vera_device.get_last_user_alert()
|
|
|
|
if last_user is not None:
|
|
|
|
data[ATTR_LAST_USER_NAME] = last_user[1]
|
|
|
|
|
|
|
|
data[ATTR_LOW_BATTERY] = self.vera_device.get_low_battery_alert()
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def changed_by(self):
|
|
|
|
"""Who unlocked the lock.
|
|
|
|
|
|
|
|
Reports on the previous poll cycle.
|
|
|
|
changed_by is an integer user ID.
|
|
|
|
"""
|
|
|
|
last_user = self.vera_device.get_last_user_alert()
|
|
|
|
if last_user is not None:
|
|
|
|
return last_user[0]
|
|
|
|
return None
|
|
|
|
|
2016-06-30 00:28:20 +00:00
|
|
|
def update(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Update state by the Vera device callback."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = (
|
|
|
|
STATE_LOCKED if self.vera_device.is_locked(True) else STATE_UNLOCKED
|
|
|
|
)
|