hass-core/homeassistant/components/verisure.py

168 lines
5 KiB
Python
Raw Normal View History

2015-08-11 09:28:07 +02:00
"""
components.verisure
~~~~~~~~~~~~~~~~~~~
Provides support for verisure components.
2015-10-23 22:31:37 +02:00
For more details about this component, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/verisure/
2015-08-11 09:28:07 +02:00
"""
import logging
2015-12-25 20:16:51 +01:00
import time
2015-08-12 13:00:47 +02:00
from datetime import timedelta
2015-08-11 09:28:07 +02:00
2015-08-15 13:36:30 +02:00
from homeassistant import bootstrap
2016-02-18 21:27:50 -08:00
from homeassistant.const import (
ATTR_DISCOVERED, ATTR_SERVICE, CONF_PASSWORD, CONF_USERNAME,
EVENT_PLATFORM_DISCOVERED)
2015-08-11 09:28:07 +02:00
from homeassistant.helpers import validate_config
2016-02-18 21:27:50 -08:00
from homeassistant.loader import get_component
2015-08-12 13:00:47 +02:00
from homeassistant.util import Throttle
2015-08-15 13:36:30 +02:00
2015-08-11 09:28:07 +02:00
DOMAIN = "verisure"
2015-08-15 13:36:30 +02:00
DISCOVER_SENSORS = 'verisure.sensors'
DISCOVER_SWITCHES = 'verisure.switches'
2015-09-13 21:07:16 +02:00
DISCOVER_ALARMS = 'verisure.alarm_control_panel'
DISCOVER_LOCKS = 'verisure.lock'
2015-08-15 13:36:30 +02:00
2015-09-13 20:21:02 +02:00
DEPENDENCIES = ['alarm_control_panel']
2016-02-17 19:28:26 +01:00
REQUIREMENTS = ['vsure==0.5.1']
2015-08-11 09:28:07 +02:00
_LOGGER = logging.getLogger(__name__)
2015-08-12 13:00:47 +02:00
MY_PAGES = None
2015-12-25 20:16:51 +01:00
ALARM_STATUS = {}
SMARTPLUG_STATUS = {}
CLIMATE_STATUS = {}
LOCK_STATUS = {}
MOUSEDETECTION_STATUS = {}
2015-08-12 13:00:47 +02:00
2015-08-15 13:36:30 +02:00
VERISURE_LOGIN_ERROR = None
VERISURE_ERROR = None
SHOW_THERMOMETERS = True
SHOW_HYGROMETERS = True
SHOW_ALARM = True
SHOW_SMARTPLUGS = True
SHOW_LOCKS = True
SHOW_MOUSEDETECTION = True
2016-01-16 15:12:54 +01:00
CODE_DIGITS = 4
2015-08-15 13:36:30 +02:00
# if wrong password was given don't try again
WRONG_PASSWORD_GIVEN = False
2015-12-25 20:16:51 +01:00
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=1)
2015-08-12 13:00:47 +02:00
2015-08-11 09:28:07 +02:00
def setup(hass, config):
""" Setup the Verisure component. """
if not validate_config(config,
{DOMAIN: [CONF_USERNAME, CONF_PASSWORD]},
_LOGGER):
return False
2015-08-15 13:36:30 +02:00
from verisure import MyPages, LoginError, Error
2015-08-11 09:28:07 +02:00
2016-01-16 15:12:54 +01:00
global SHOW_THERMOMETERS, SHOW_HYGROMETERS,\
SHOW_ALARM, SHOW_SMARTPLUGS, SHOW_LOCKS, SHOW_MOUSEDETECTION,\
CODE_DIGITS
SHOW_THERMOMETERS = int(config[DOMAIN].get('thermometers', '1'))
SHOW_HYGROMETERS = int(config[DOMAIN].get('hygrometers', '1'))
SHOW_ALARM = int(config[DOMAIN].get('alarm', '1'))
SHOW_SMARTPLUGS = int(config[DOMAIN].get('smartplugs', '1'))
SHOW_LOCKS = int(config[DOMAIN].get('locks', '1'))
SHOW_MOUSEDETECTION = int(config[DOMAIN].get('mouse', '1'))
2016-01-16 15:12:54 +01:00
CODE_DIGITS = int(config[DOMAIN].get('code_digits', '4'))
2015-08-12 13:00:47 +02:00
global MY_PAGES
MY_PAGES = MyPages(
config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD])
2015-08-15 13:36:30 +02:00
global VERISURE_LOGIN_ERROR, VERISURE_ERROR
VERISURE_LOGIN_ERROR = LoginError
VERISURE_ERROR = Error
2015-08-11 09:28:07 +02:00
2015-08-15 13:36:30 +02:00
try:
MY_PAGES.login()
except (ConnectionError, Error) as ex:
2015-08-16 06:51:09 +02:00
_LOGGER.error('Could not log in to verisure mypages, %s', ex)
2015-08-15 13:36:30 +02:00
return False
2015-08-11 09:28:07 +02:00
2015-12-25 20:16:51 +01:00
update_alarm()
update_climate()
update_smartplug()
update_lock()
update_mousedetection()
2015-08-15 13:36:30 +02:00
# Load components for the devices in the ISY controller that we support
for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
2015-09-13 07:42:38 +02:00
('switch', DISCOVER_SWITCHES),
('alarm_control_panel', DISCOVER_ALARMS),
('lock', DISCOVER_LOCKS))):
2015-08-15 13:36:30 +02:00
component = get_component(comp_name)
_LOGGER.info(config[DOMAIN])
2015-08-15 13:36:30 +02:00
bootstrap.setup_component(hass, component.DOMAIN, config)
2015-08-11 09:28:07 +02:00
2015-08-15 13:36:30 +02:00
hass.bus.fire(EVENT_PLATFORM_DISCOVERED,
{ATTR_SERVICE: discovery,
ATTR_DISCOVERED: {}})
2015-08-11 09:28:07 +02:00
return True
2015-08-12 13:00:47 +02:00
2015-08-15 13:36:30 +02:00
def reconnect():
""" Reconnect to verisure mypages. """
2015-08-15 13:36:30 +02:00
try:
2015-12-25 20:16:51 +01:00
time.sleep(1)
2015-08-15 13:36:30 +02:00
MY_PAGES.login()
except VERISURE_LOGIN_ERROR as ex:
2015-08-16 06:51:09 +02:00
_LOGGER.error("Could not login to Verisure mypages, %s", ex)
2015-08-15 13:36:30 +02:00
global WRONG_PASSWORD_GIVEN
WRONG_PASSWORD_GIVEN = True
except (ConnectionError, VERISURE_ERROR) as ex:
2015-08-16 06:51:09 +02:00
_LOGGER.error("Could not login to Verisure mypages, %s", ex)
2015-08-15 13:36:30 +02:00
2015-08-12 13:00:47 +02:00
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
2015-12-25 20:16:51 +01:00
def update_alarm():
2015-12-25 21:04:16 +01:00
""" Updates the status of alarms. """
2015-12-25 20:16:51 +01:00
update_component(MY_PAGES.alarm.get, ALARM_STATUS)
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update_climate():
2015-12-25 21:04:16 +01:00
""" Updates the status of climate sensors. """
2015-12-25 20:16:51 +01:00
update_component(MY_PAGES.climate.get, CLIMATE_STATUS)
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update_smartplug():
2015-12-25 21:04:16 +01:00
""" Updates the status of smartplugs. """
2015-12-25 20:16:51 +01:00
update_component(MY_PAGES.smartplug.get, SMARTPLUG_STATUS)
def update_lock():
""" Updates the status of alarms. """
update_component(MY_PAGES.lock.get, LOCK_STATUS)
def update_mousedetection():
""" Updates the status of mouse detectors. """
update_component(MY_PAGES.mousedetection.get, MOUSEDETECTION_STATUS)
2015-12-25 20:16:51 +01:00
def update_component(get_function, status):
""" Updates the status of verisure components. """
2015-08-15 13:36:30 +02:00
if WRONG_PASSWORD_GIVEN:
2015-09-13 07:42:38 +02:00
_LOGGER.error('Wrong password')
2015-08-15 13:36:30 +02:00
return
2015-08-12 13:00:47 +02:00
try:
2015-12-25 20:16:51 +01:00
for overview in get_function():
try:
status[overview.id] = overview
2016-02-13 09:05:18 +01:00
except AttributeError:
status[overview.deviceLabel] = overview
2015-12-25 20:16:51 +01:00
except (ConnectionError, VERISURE_ERROR) as ex:
2015-08-12 13:32:15 +02:00
_LOGGER.error('Caught connection error %s, tries to reconnect', ex)
2015-08-15 13:36:30 +02:00
reconnect()