2015-08-11 07:28:07 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for Verisure components.
|
2015-08-17 11:05:49 +00:00
|
|
|
|
2015-10-23 20:31:37 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/verisure/
|
2015-08-11 07:28:07 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-27 20:50:19 +00:00
|
|
|
import threading
|
2015-12-25 19:16:51 +00:00
|
|
|
import time
|
2016-12-06 01:51:58 +00:00
|
|
|
import os.path
|
2015-08-12 11:00:47 +00:00
|
|
|
from datetime import timedelta
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2016-09-07 01:18:34 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2016-09-07 01:18:34 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2015-08-12 11:00:47 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-12-06 01:51:58 +00:00
|
|
|
import homeassistant.config as conf_util
|
2016-09-07 01:18:34 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-08-15 11:36:30 +00:00
|
|
|
|
2016-10-13 19:53:50 +00:00
|
|
|
REQUIREMENTS = ['vsure==0.11.1']
|
2015-08-11 07:28:07 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-12-06 01:51:58 +00:00
|
|
|
ATTR_DEVICE_SERIAL = 'device_serial'
|
2016-09-07 01:18:34 +00:00
|
|
|
CONF_ALARM = 'alarm'
|
|
|
|
CONF_CODE_DIGITS = 'code_digits'
|
|
|
|
CONF_HYDROMETERS = 'hygrometers'
|
|
|
|
CONF_LOCKS = 'locks'
|
|
|
|
CONF_MOUSE = 'mouse'
|
|
|
|
CONF_SMARTPLUGS = 'smartplugs'
|
|
|
|
CONF_THERMOMETERS = 'thermometers'
|
2016-10-21 20:41:17 +00:00
|
|
|
CONF_SMARTCAM = 'smartcam'
|
2016-09-07 01:18:34 +00:00
|
|
|
DOMAIN = 'verisure'
|
2016-12-06 01:51:58 +00:00
|
|
|
SERVICE_CAPTURE_SMARTCAM = 'capture_smartcam'
|
2016-09-07 01:18:34 +00:00
|
|
|
|
2016-02-27 20:50:19 +00:00
|
|
|
HUB = None
|
2015-08-12 11:00:47 +00:00
|
|
|
|
2016-09-07 01:18:34 +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_HYDROMETERS, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_LOCKS, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_MOUSE, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_SMARTPLUGS, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_THERMOMETERS, default=True): cv.boolean,
|
2016-10-21 20:41:17 +00:00
|
|
|
vol.Optional(CONF_SMARTCAM, default=True): cv.boolean,
|
2016-09-07 01:18:34 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-12-06 01:51:58 +00:00
|
|
|
CAPTURE_IMAGE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_DEVICE_SERIAL): cv.string
|
|
|
|
})
|
|
|
|
|
2015-08-11 07:28:07 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Setup the Verisure component."""
|
2016-02-27 20:50:19 +00:00
|
|
|
import verisure
|
|
|
|
global HUB
|
|
|
|
HUB = VerisureHub(config[DOMAIN], verisure)
|
|
|
|
if not HUB.login():
|
2015-08-15 11:36:30 +00:00
|
|
|
return False
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2016-10-21 20:41:17 +00:00
|
|
|
for component in ('sensor', 'switch', 'alarm_control_panel', 'lock',
|
|
|
|
'camera'):
|
2016-06-15 05:51:46 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
2015-08-11 07:28:07 +00:00
|
|
|
|
2016-12-06 01:51:58 +00:00
|
|
|
descriptions = conf_util.load_yaml_config_file(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
|
|
|
|
|
|
|
def capture_smartcam(service):
|
|
|
|
"""Capture a new picture from a smartcam."""
|
|
|
|
device_id = service.data.get(ATTR_DEVICE_SERIAL)
|
|
|
|
HUB.smartcam_capture(device_id)
|
|
|
|
_LOGGER.debug('Capturing new image from %s', ATTR_DEVICE_SERIAL)
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, SERVICE_CAPTURE_SMARTCAM,
|
|
|
|
capture_smartcam,
|
|
|
|
descriptions[DOMAIN][SERVICE_CAPTURE_SMARTCAM],
|
|
|
|
schema=CAPTURE_IMAGE_SCHEMA)
|
|
|
|
|
2015-08-11 07:28:07 +00:00
|
|
|
return True
|
2015-08-12 11:00:47 +00:00
|
|
|
|
|
|
|
|
2016-02-27 20:50:19 +00:00
|
|
|
class VerisureHub(object):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""A Verisure hub wrapper class."""
|
|
|
|
|
2016-02-27 20:50:19 +00:00
|
|
|
def __init__(self, domain_config, verisure):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the Verisure hub."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.alarm_status = {}
|
|
|
|
self.lock_status = {}
|
|
|
|
self.climate_status = {}
|
|
|
|
self.mouse_status = {}
|
|
|
|
self.smartplug_status = {}
|
2016-10-21 20:41:17 +00:00
|
|
|
self.smartcam_status = {}
|
|
|
|
self.smartcam_dict = {}
|
2016-02-27 20:50:19 +00:00
|
|
|
|
|
|
|
self.config = domain_config
|
|
|
|
self._verisure = verisure
|
|
|
|
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
|
2016-03-06 21:09:15 +00:00
|
|
|
# When MyPages is brought up from maintenance it sometimes give us a
|
|
|
|
# "wrong password" message. We will continue to retry after maintenance
|
|
|
|
# regardless of that error.
|
|
|
|
self._disable_wrong_password_error = False
|
2016-05-04 01:53:11 +00:00
|
|
|
self._password_retries = 1
|
2016-02-27 20:50:19 +00:00
|
|
|
self._reconnect_timeout = time.time()
|
|
|
|
|
|
|
|
self.my_pages = verisure.MyPages(
|
|
|
|
domain_config[CONF_USERNAME],
|
|
|
|
domain_config[CONF_PASSWORD])
|
|
|
|
|
|
|
|
def login(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Login to Verisure MyPages."""
|
2016-02-27 20:50:19 +00:00
|
|
|
try:
|
|
|
|
self.my_pages.login()
|
|
|
|
except self._verisure.Error as ex:
|
|
|
|
_LOGGER.error('Could not log in to verisure mypages, %s', ex)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=1))
|
|
|
|
def update_alarms(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of the alarm."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.update_component(
|
|
|
|
self.my_pages.alarm.get,
|
|
|
|
self.alarm_status)
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=1))
|
|
|
|
def update_locks(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of the locks."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.update_component(
|
|
|
|
self.my_pages.lock.get,
|
|
|
|
self.lock_status)
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=60))
|
|
|
|
def update_climate(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of the climate units."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.update_component(
|
|
|
|
self.my_pages.climate.get,
|
|
|
|
self.climate_status)
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=60))
|
|
|
|
def update_mousedetection(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of the mouse detectors."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.update_component(
|
|
|
|
self.my_pages.mousedetection.get,
|
|
|
|
self.mouse_status)
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=1))
|
|
|
|
def update_smartplugs(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of the smartplugs."""
|
2016-02-27 20:50:19 +00:00
|
|
|
self.update_component(
|
|
|
|
self.my_pages.smartplug.get,
|
|
|
|
self.smartplug_status)
|
|
|
|
|
2016-10-21 20:41:17 +00:00
|
|
|
@Throttle(timedelta(seconds=30))
|
|
|
|
def update_smartcam(self):
|
|
|
|
"""Update the status of the smartcam."""
|
|
|
|
self.update_component(
|
|
|
|
self.my_pages.smartcam.get,
|
|
|
|
self.smartcam_status)
|
|
|
|
|
|
|
|
@Throttle(timedelta(seconds=30))
|
|
|
|
def update_smartcam_imagelist(self):
|
|
|
|
"""Update the imagelist for the camera."""
|
|
|
|
_LOGGER.debug('Running update imagelist')
|
|
|
|
self.smartcam_dict = self.my_pages.smartcam.get_imagelist()
|
|
|
|
_LOGGER.debug('New dict: %s', self.smartcam_dict)
|
|
|
|
|
2016-12-06 01:51:58 +00:00
|
|
|
@Throttle(timedelta(seconds=30))
|
|
|
|
def smartcam_capture(self, device_id):
|
|
|
|
"""Capture a new image from a smartcam."""
|
|
|
|
self.my_pages.smartcam.capture(device_id)
|
|
|
|
|
2016-05-04 01:53:11 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if hub is available."""
|
|
|
|
return self._password_retries >= 0
|
|
|
|
|
2016-02-27 20:50:19 +00:00
|
|
|
def update_component(self, get_function, status):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Update the status of Verisure components."""
|
2016-02-27 20:50:19 +00:00
|
|
|
try:
|
|
|
|
for overview in get_function():
|
|
|
|
try:
|
|
|
|
status[overview.id] = overview
|
|
|
|
except AttributeError:
|
|
|
|
status[overview.deviceLabel] = overview
|
|
|
|
except self._verisure.Error as ex:
|
2016-06-14 18:21:42 +00:00
|
|
|
_LOGGER.info('Caught connection error %s, tries to reconnect', ex)
|
2016-02-27 20:50:19 +00:00
|
|
|
self.reconnect()
|
|
|
|
|
|
|
|
def reconnect(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Reconnect to Verisure MyPages."""
|
2016-05-04 01:53:11 +00:00
|
|
|
if (self._reconnect_timeout > time.time() or
|
|
|
|
not self._lock.acquire(blocking=False) or
|
|
|
|
self._password_retries < 0):
|
2016-02-27 20:50:19 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.my_pages.login()
|
2016-03-06 21:09:15 +00:00
|
|
|
self._disable_wrong_password_error = False
|
2016-05-04 01:53:11 +00:00
|
|
|
self._password_retries = 1
|
2016-02-27 20:50:19 +00:00
|
|
|
except self._verisure.LoginError as ex:
|
|
|
|
_LOGGER.error("Wrong user name or password for Verisure MyPages")
|
2016-03-06 21:09:15 +00:00
|
|
|
if self._disable_wrong_password_error:
|
2016-05-04 01:53:11 +00:00
|
|
|
self._reconnect_timeout = time.time() + 60*60
|
2016-02-27 20:50:19 +00:00
|
|
|
else:
|
2016-05-04 01:53:11 +00:00
|
|
|
self._password_retries = self._password_retries - 1
|
2016-02-27 20:50:19 +00:00
|
|
|
except self._verisure.MaintenanceError:
|
2016-03-06 21:09:15 +00:00
|
|
|
self._disable_wrong_password_error = True
|
2016-05-04 01:53:11 +00:00
|
|
|
self._reconnect_timeout = time.time() + 60*60
|
2016-02-27 20:50:19 +00:00
|
|
|
_LOGGER.error("Verisure MyPages down for maintenance")
|
|
|
|
except self._verisure.Error as ex:
|
|
|
|
_LOGGER.error("Could not login to Verisure MyPages, %s", ex)
|
2016-05-04 01:53:11 +00:00
|
|
|
self._reconnect_timeout = time.time() + 60
|
2016-02-27 20:50:19 +00:00
|
|
|
finally:
|
|
|
|
self._lock.release()
|