core/homeassistant/components/verisure/__init__.py

230 lines
7.4 KiB
Python
Raw Normal View History

"""Support for Verisure devices."""
from datetime import timedelta
2015-08-11 07:28:07 +00:00
import logging
2016-02-27 20:50:19 +00:00
import threading
from jsonpath import jsonpath
import verisure
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from homeassistant.const import (
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle
2015-08-15 11:36:30 +00:00
2015-08-11 07:28:07 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_DEVICE_SERIAL = "device_serial"
2019-07-31 19:25:30 +00:00
CONF_ALARM = "alarm"
CONF_CODE_DIGITS = "code_digits"
CONF_DOOR_WINDOW = "door_window"
CONF_GIID = "giid"
CONF_HYDROMETERS = "hygrometers"
CONF_LOCKS = "locks"
CONF_DEFAULT_LOCK_CODE = "default_lock_code"
CONF_MOUSE = "mouse"
CONF_SMARTPLUGS = "smartplugs"
CONF_THERMOMETERS = "thermometers"
CONF_SMARTCAM = "smartcam"
2019-07-31 19:25:30 +00:00
DOMAIN = "verisure"
MIN_SCAN_INTERVAL = timedelta(minutes=1)
DEFAULT_SCAN_INTERVAL = timedelta(minutes=1)
2019-07-31 19:25:30 +00:00
SERVICE_CAPTURE_SMARTCAM = "capture_smartcam"
2019-11-10 07:25:10 +00:00
SERVICE_DISABLE_AUTOLOCK = "disable_autolock"
SERVICE_ENABLE_AUTOLOCK = "enable_autolock"
2016-02-27 20:50:19 +00:00
HUB = None
2015-08-12 11:00:47 +00:00
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_ALARM, default=True): cv.boolean,
vol.Optional(CONF_CODE_DIGITS, default=4): cv.positive_int,
vol.Optional(CONF_DOOR_WINDOW, default=True): cv.boolean,
vol.Optional(CONF_GIID): cv.string,
vol.Optional(CONF_HYDROMETERS, default=True): cv.boolean,
vol.Optional(CONF_LOCKS, default=True): cv.boolean,
vol.Optional(CONF_DEFAULT_LOCK_CODE): cv.string,
vol.Optional(CONF_MOUSE, default=True): cv.boolean,
vol.Optional(CONF_SMARTPLUGS, default=True): cv.boolean,
vol.Optional(CONF_THERMOMETERS, default=True): cv.boolean,
vol.Optional(CONF_SMARTCAM, default=True): cv.boolean,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): (
vol.All(cv.time_period, vol.Clamp(min=MIN_SCAN_INTERVAL))
),
}
)
},
extra=vol.ALLOW_EXTRA,
)
2019-11-10 07:25:10 +00:00
DEVICE_SERIAL_SCHEMA = vol.Schema({vol.Required(ATTR_DEVICE_SERIAL): cv.string})
2015-08-11 07:28:07 +00:00
def setup(hass, config):
"""Set up the Verisure component."""
global HUB # pylint: disable=global-statement
HUB = VerisureHub(config[DOMAIN])
2019-07-31 19:25:30 +00:00
HUB.update_overview = Throttle(config[DOMAIN][CONF_SCAN_INTERVAL])(
HUB.update_overview
)
2016-02-27 20:50:19 +00:00
if not HUB.login():
2015-08-15 11:36:30 +00:00
return False
2019-07-31 19:25:30 +00:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: HUB.logout())
HUB.update_overview()
2015-08-11 07:28:07 +00:00
2019-07-31 19:25:30 +00:00
for component in (
"sensor",
"switch",
"alarm_control_panel",
"lock",
"camera",
"binary_sensor",
):
2016-06-15 05:51:46 +00:00
discovery.load_platform(hass, component, DOMAIN, {}, config)
2015-08-11 07:28:07 +00:00
2019-11-10 07:25:10 +00:00
async def capture_smartcam(service):
"""Capture a new picture from a smartcam."""
2019-11-10 07:25:10 +00:00
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(HUB.smartcam_capture, device_id)
_LOGGER.debug("Capturing new image from %s", ATTR_DEVICE_SERIAL)
except verisure.Error as ex:
_LOGGER.error("Could not capture image, %s", ex)
2019-07-31 19:25:30 +00:00
hass.services.register(
2019-11-10 07:25:10 +00:00
DOMAIN, SERVICE_CAPTURE_SMARTCAM, capture_smartcam, schema=DEVICE_SERIAL_SCHEMA
2019-07-31 19:25:30 +00:00
)
2019-11-10 07:25:10 +00:00
async def disable_autolock(service):
"""Disable autolock on a doorlock."""
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(HUB.disable_autolock, device_id)
_LOGGER.debug("Disabling autolock on%s", ATTR_DEVICE_SERIAL)
except verisure.Error as ex:
_LOGGER.error("Could not disable autolock, %s", ex)
hass.services.register(
DOMAIN, SERVICE_DISABLE_AUTOLOCK, disable_autolock, schema=DEVICE_SERIAL_SCHEMA
)
async def enable_autolock(service):
"""Enable autolock on a doorlock."""
device_id = service.data[ATTR_DEVICE_SERIAL]
try:
await hass.async_add_executor_job(HUB.enable_autolock, device_id)
_LOGGER.debug("Enabling autolock on %s", ATTR_DEVICE_SERIAL)
except verisure.Error as ex:
_LOGGER.error("Could not enable autolock, %s", ex)
hass.services.register(
DOMAIN, SERVICE_ENABLE_AUTOLOCK, enable_autolock, schema=DEVICE_SERIAL_SCHEMA
)
2015-08-11 07:28:07 +00:00
return True
2015-08-12 11:00:47 +00:00
class VerisureHub:
2016-03-08 16:55:57 +00:00
"""A Verisure hub wrapper class."""
def __init__(self, domain_config):
2016-03-08 16:55:57 +00:00
"""Initialize the Verisure hub."""
self.overview = {}
self.imageseries = {}
2016-02-27 20:50:19 +00:00
self.config = domain_config
self._lock = threading.Lock()
self.session = verisure.Session(
2019-07-31 19:25:30 +00:00
domain_config[CONF_USERNAME], domain_config[CONF_PASSWORD]
)
2016-02-27 20:50:19 +00:00
self.giid = domain_config.get(CONF_GIID)
2016-02-27 20:50:19 +00:00
def login(self):
"""Login to Verisure."""
2016-02-27 20:50:19 +00:00
try:
self.session.login()
except verisure.Error as ex:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Could not log in to verisure, %s", ex)
2016-02-27 20:50:19 +00:00
return False
if self.giid:
return self.set_giid()
2016-02-27 20:50:19 +00:00
return True
def logout(self):
"""Logout from Verisure."""
try:
self.session.logout()
except verisure.Error as ex:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Could not log out from verisure, %s", ex)
return False
return True
2016-02-27 20:50:19 +00:00
def set_giid(self):
"""Set installation GIID."""
try:
self.session.set_giid(self.giid)
except verisure.Error as ex:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Could not set installation GIID, %s", ex)
return False
return True
def update_overview(self):
"""Update the overview."""
try:
self.overview = self.session.get_overview()
except verisure.ResponseError as ex:
2019-07-31 19:25:30 +00:00
_LOGGER.error("Could not read overview, %s", ex)
if ex.status_code == 503: # Service unavailable
2019-07-31 19:25:30 +00:00
_LOGGER.info("Trying to log in again")
self.login()
else:
raise
2016-02-27 20:50:19 +00:00
@Throttle(timedelta(seconds=60))
def update_smartcam_imageseries(self):
"""Update the image series."""
self.imageseries = self.session.get_camera_imageseries()
@Throttle(timedelta(seconds=30))
def smartcam_capture(self, device_id):
"""Capture a new image from a smartcam."""
self.session.capture_image(device_id)
2019-11-10 07:25:10 +00:00
def disable_autolock(self, device_id):
"""Disable autolock."""
self.session.set_lock_config(device_id, auto_lock_enabled=False)
def enable_autolock(self, device_id):
"""Enable autolock."""
self.session.set_lock_config(device_id, auto_lock_enabled=True)
def get(self, jpath, *args):
"""Get values from the overview that matches the jsonpath."""
res = jsonpath(self.overview, jpath % args)
return res if res else []
def get_first(self, jpath, *args):
"""Get first value from the overview that matches the jsonpath."""
res = self.get(jpath, *args)
return res[0] if res else None
def get_image_info(self, jpath, *args):
"""Get values from the imageseries that matches the jsonpath."""
res = jsonpath(self.imageseries, jpath % args)
return res if res else []