core/homeassistant/components/verisure.py

148 lines
4.2 KiB
Python
Raw Normal View History

2015-08-11 07:28:07 +00:00
"""
components.verisure
~~~~~~~~~~~~~~~~~~~
Provides support for verisure components.
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
2015-12-25 19:16:51 +00:00
import time
2015-08-12 11:00:47 +00:00
from datetime import timedelta
2015-08-11 07:28:07 +00:00
2015-08-15 11:36:30 +00:00
from homeassistant import bootstrap
from homeassistant.loader import get_component
2015-08-11 07:28:07 +00:00
from homeassistant.helpers import validate_config
2015-08-12 11:00:47 +00:00
from homeassistant.util import Throttle
2015-08-11 07:28:07 +00:00
from homeassistant.const import (
2015-08-15 11:36:30 +00:00
EVENT_PLATFORM_DISCOVERED,
ATTR_SERVICE, ATTR_DISCOVERED,
2015-08-12 11:00:47 +00:00
CONF_USERNAME, CONF_PASSWORD)
2015-08-11 07:28:07 +00:00
2015-08-15 11:36:30 +00:00
2015-08-11 07:28:07 +00:00
DOMAIN = "verisure"
2015-08-15 11:36:30 +00:00
DISCOVER_SENSORS = 'verisure.sensors'
DISCOVER_SWITCHES = 'verisure.switches'
2015-09-13 19:07:16 +00:00
DISCOVER_ALARMS = 'verisure.alarm_control_panel'
2015-08-15 11:36:30 +00:00
2015-09-13 18:21:02 +00:00
DEPENDENCIES = ['alarm_control_panel']
2016-01-25 20:39:41 +00:00
REQUIREMENTS = ['vsure==0.4.8']
2015-08-11 07:28:07 +00:00
_LOGGER = logging.getLogger(__name__)
2015-08-12 11:00:47 +00:00
MY_PAGES = None
2015-12-25 19:16:51 +00:00
ALARM_STATUS = {}
SMARTPLUG_STATUS = {}
CLIMATE_STATUS = {}
2015-08-12 11:00:47 +00:00
2015-08-15 11:36:30 +00:00
VERISURE_LOGIN_ERROR = None
VERISURE_ERROR = None
SHOW_THERMOMETERS = True
SHOW_HYGROMETERS = True
SHOW_ALARM = True
SHOW_SMARTPLUGS = True
2016-01-16 14:12:54 +00:00
CODE_DIGITS = 4
2015-08-15 11:36:30 +00:00
# if wrong password was given don't try again
WRONG_PASSWORD_GIVEN = False
2015-12-25 19:16:51 +00:00
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=1)
2015-08-12 11:00:47 +00:00
2015-08-11 07:28:07 +00: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 11:36:30 +00:00
from verisure import MyPages, LoginError, Error
2015-08-11 07:28:07 +00:00
2016-01-16 14:12:54 +00:00
global SHOW_THERMOMETERS, SHOW_HYGROMETERS,\
SHOW_ALARM, SHOW_SMARTPLUGS, 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'))
2016-01-16 14:12:54 +00:00
CODE_DIGITS = int(config[DOMAIN].get('code_digits', '4'))
2015-08-12 11:00:47 +00:00
global MY_PAGES
MY_PAGES = MyPages(
config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD])
2015-08-15 11:36:30 +00:00
global VERISURE_LOGIN_ERROR, VERISURE_ERROR
VERISURE_LOGIN_ERROR = LoginError
VERISURE_ERROR = Error
2015-08-11 07:28:07 +00:00
2015-08-15 11:36:30 +00:00
try:
MY_PAGES.login()
except (ConnectionError, Error) as ex:
2015-08-16 04:51:09 +00:00
_LOGGER.error('Could not log in to verisure mypages, %s', ex)
2015-08-15 11:36:30 +00:00
return False
2015-08-11 07:28:07 +00:00
2015-12-25 19:16:51 +00:00
update_alarm()
update_climate()
update_smartplug()
2015-08-15 11:36:30 +00:00
# Load components for the devices in the ISY controller that we support
for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
2015-09-13 05:42:38 +00:00
('switch', DISCOVER_SWITCHES),
2015-09-14 15:33:43 +00:00
('alarm_control_panel', DISCOVER_ALARMS))):
2015-08-15 11:36:30 +00:00
component = get_component(comp_name)
_LOGGER.info(config[DOMAIN])
2015-08-15 11:36:30 +00:00
bootstrap.setup_component(hass, component.DOMAIN, config)
2015-08-11 07:28:07 +00:00
2015-08-15 11:36:30 +00:00
hass.bus.fire(EVENT_PLATFORM_DISCOVERED,
{ATTR_SERVICE: discovery,
ATTR_DISCOVERED: {}})
2015-08-11 07:28:07 +00:00
return True
2015-08-12 11:00:47 +00:00
2015-08-15 11:36:30 +00:00
def reconnect():
""" Reconnect to verisure mypages. """
2015-08-15 11:36:30 +00:00
try:
2015-12-25 19:16:51 +00:00
time.sleep(1)
2015-08-15 11:36:30 +00:00
MY_PAGES.login()
except VERISURE_LOGIN_ERROR as ex:
2015-08-16 04:51:09 +00:00
_LOGGER.error("Could not login to Verisure mypages, %s", ex)
2015-08-15 11:36:30 +00:00
global WRONG_PASSWORD_GIVEN
WRONG_PASSWORD_GIVEN = True
except (ConnectionError, VERISURE_ERROR) as ex:
2015-08-16 04:51:09 +00:00
_LOGGER.error("Could not login to Verisure mypages, %s", ex)
2015-08-15 11:36:30 +00:00
2015-08-12 11:00:47 +00:00
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
2015-12-25 19:16:51 +00:00
def update_alarm():
2015-12-25 20:04:16 +00:00
""" Updates the status of alarms. """
2015-12-25 19:16:51 +00:00
update_component(MY_PAGES.alarm.get, ALARM_STATUS)
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update_climate():
2015-12-25 20:04:16 +00:00
""" Updates the status of climate sensors. """
2015-12-25 19:16:51 +00:00
update_component(MY_PAGES.climate.get, CLIMATE_STATUS)
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update_smartplug():
2015-12-25 20:04:16 +00:00
""" Updates the status of smartplugs. """
2015-12-25 19:16:51 +00:00
update_component(MY_PAGES.smartplug.get, SMARTPLUG_STATUS)
def update_component(get_function, status):
""" Updates the status of verisure components. """
2015-08-15 11:36:30 +00:00
if WRONG_PASSWORD_GIVEN:
2015-09-13 05:42:38 +00:00
_LOGGER.error('Wrong password')
2015-08-15 11:36:30 +00:00
return
2015-08-12 11:00:47 +00:00
try:
2015-12-25 19:16:51 +00:00
for overview in get_function():
status[overview.id] = overview
except (ConnectionError, VERISURE_ERROR) as ex:
2015-08-12 11:32:15 +00:00
_LOGGER.error('Caught connection error %s, tries to reconnect', ex)
2015-08-15 11:36:30 +00:00
reconnect()