hass-core/homeassistant/components/verisure.py

90 lines
2.5 KiB
Python
Raw Normal View History

2015-08-11 09:28:07 +02:00
"""
components.verisure
~~~~~~~~~~~~~~~~~~
"""
import logging
2015-08-12 13:00:47 +02:00
from datetime import timedelta
2015-08-11 09:28:07 +02:00
from homeassistant.helpers import validate_config
2015-08-12 13:00:47 +02:00
from homeassistant.util import Throttle
2015-08-11 09:28:07 +02:00
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
2015-08-12 13:00:47 +02:00
CONF_USERNAME, CONF_PASSWORD)
2015-08-11 09:28:07 +02:00
DOMAIN = "verisure"
DEPENDENCIES = []
2015-08-12 13:00:47 +02:00
REQUIREMENTS = [
'https://github.com/persandstrom/python-verisure/archive/master.zip'
]
2015-08-11 09:28:07 +02:00
_LOGGER = logging.getLogger(__name__)
2015-08-12 13:00:47 +02:00
MY_PAGES = None
STATUS = {}
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=5)
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
from verisure import MyPages
2015-08-12 13:00:47 +02:00
STATUS[MyPages.DEVICE_ALARM] = {}
STATUS[MyPages.DEVICE_CLIMATE] = {}
STATUS[MyPages.DEVICE_SMARTPLUG] = {}
2015-08-11 09:28:07 +02:00
2015-08-12 13:00:47 +02:00
global MY_PAGES
MY_PAGES = MyPages(
config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD])
MY_PAGES.login()
update()
2015-08-11 09:28:07 +02:00
def stop_verisure(event):
2015-08-12 13:00:47 +02:00
""" Stop the Verisure service. """
2015-08-11 09:28:07 +02:00
MY_PAGES.logout()
def start_verisure(event):
2015-08-12 13:00:47 +02:00
""" Start the Verisure service. """
2015-08-11 09:28:07 +02:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_verisure)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_verisure)
return True
2015-08-12 13:00:47 +02:00
def get_alarm_status():
''' return a list of status overviews for alarm components '''
return STATUS[MY_PAGES.DEVICE_ALARM]
def get_climate_status():
''' return a list of status overviews for alarm components '''
return STATUS[MY_PAGES.DEVICE_CLIMATE]
def get_smartplug_status():
''' return a list of status overviews for alarm components '''
return STATUS[MY_PAGES.DEVICE_SMARTPLUG]
@Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update():
''' Updates the status of verisure components '''
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 13:28:22 +02:00
_LOGGER.error('Caught connection error %, tries to reconnect', ex)
2015-08-12 13:00:47 +02:00
MY_PAGES.login()