core/homeassistant/components/verisure.py

182 lines
4.8 KiB
Python
Raw Normal View History

2015-08-11 07:28:07 +00:00
"""
components.verisure
~~~~~~~~~~~~~~~~~~~
Provides support for verisure components.
Configuration:
To use the Verisure component you will need to add something like the
following to your configuration.yaml file.
verisure:
username: user@example.com
password: password
alarm: 1
hygrometers: 0
smartplugs: 1
thermometers: 0
Variables:
username
*Required
Username to Verisure mypages.
password
*Required
Password to Verisure mypages.
alarm
*Optional
Set to 1 to show alarm, 0 to disable. Default 1.
hygrometers
*Optional
Set to 1 to show hygrometers, 0 to disable. Default 1.
smartplugs
*Optional
Set to 1 to show smartplugs, 0 to disable. Default 1.
thermometers
*Optional
Set to 1 to show thermometers, 0 to disable. Default 1.
2015-08-11 07:28:07 +00:00
"""
import logging
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-08-11 07:28:07 +00:00
DEPENDENCIES = []
2015-08-12 11:00:47 +00:00
REQUIREMENTS = [
'https://github.com/persandstrom/python-verisure/archive/'
'9873c4527f01b1ba1f72ae60f7f35854390d59be.zip#python-verisure==0.2.6'
]
2015-08-11 07:28:07 +00:00
_LOGGER = logging.getLogger(__name__)
2015-08-12 11:00:47 +00:00
MY_PAGES = None
STATUS = {}
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
2015-08-15 11:36:30 +00:00
# if wrong password was given don't try again
WRONG_PASSWORD_GIVEN = False
2015-08-12 11:00:47 +00:00
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=5)
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
2015-08-12 11:00:47 +00:00
STATUS[MyPages.DEVICE_ALARM] = {}
STATUS[MyPages.DEVICE_CLIMATE] = {}
STATUS[MyPages.DEVICE_SMARTPLUG] = {}
2015-08-11 07:28:07 +00:00
global SHOW_THERMOMETERS, SHOW_HYGROMETERS, SHOW_ALARM, SHOW_SMARTPLUGS
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'))
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-08-15 11:36:30 +00:00
update()
# Load components for the devices in the ISY controller that we support
for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
('switch', DISCOVER_SWITCHES))):
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
def get_alarm_status():
""" Return a list of status overviews for alarm components. """
2015-08-12 11:00:47 +00:00
return STATUS[MY_PAGES.DEVICE_ALARM]
def get_climate_status():
""" Return a list of status overviews for alarm components. """
2015-08-12 11:00:47 +00:00
return STATUS[MY_PAGES.DEVICE_CLIMATE]
def get_smartplug_status():
""" Return a list of status overviews for alarm components. """
2015-08-12 11:00:47 +00:00
return STATUS[MY_PAGES.DEVICE_SMARTPLUG]
2015-08-15 11:36:30 +00:00
def reconnect():
""" Reconnect to verisure mypages. """
2015-08-15 11:36:30 +00:00
try:
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)
def update():
""" Updates the status of verisure components. """
2015-08-15 11:36:30 +00:00
if WRONG_PASSWORD_GIVEN:
# Is there any way to inform user?
return
2015-08-12 11:00:47 +00:00
try:
for overview in MY_PAGES.get_overview(MY_PAGES.DEVICE_ALARM):
STATUS[MY_PAGES.DEVICE_ALARM][overview.id] = overview
for overview in MY_PAGES.get_overview(MY_PAGES.DEVICE_CLIMATE):
STATUS[MY_PAGES.DEVICE_CLIMATE][overview.id] = overview
for overview in MY_PAGES.get_overview(MY_PAGES.DEVICE_SMARTPLUG):
STATUS[MY_PAGES.DEVICE_SMARTPLUG][overview.id] = overview
except ConnectionError 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()