code reveiw

This commit is contained in:
Per Sandstrom 2015-08-15 13:36:30 +02:00
parent f20be1e7f8
commit ad327b64ed
5 changed files with 64 additions and 21 deletions

3
.gitmodules vendored
View File

@ -10,6 +10,3 @@
[submodule "homeassistant/components/frontend/www_static/home-assistant-polymer"] [submodule "homeassistant/components/frontend/www_static/home-assistant-polymer"]
path = homeassistant/components/frontend/www_static/home-assistant-polymer path = homeassistant/components/frontend/www_static/home-assistant-polymer
url = https://github.com/balloob/home-assistant-polymer.git url = https://github.com/balloob/home-assistant-polymer.git
[submodule "homeassistant/external/verisure"]
path = homeassistant/external/verisure
url = https://github.com/persandstrom/python-verisure.git

View File

@ -6,7 +6,7 @@ Component to interface with various sensors that can be monitored.
import logging import logging
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.components import wink, zwave, isy994 from homeassistant.components import wink, zwave, isy994, verisure
DOMAIN = 'sensor' DOMAIN = 'sensor'
DEPENDENCIES = [] DEPENDENCIES = []
@ -18,7 +18,8 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}'
DISCOVERY_PLATFORMS = { DISCOVERY_PLATFORMS = {
wink.DISCOVER_SENSORS: 'wink', wink.DISCOVER_SENSORS: 'wink',
zwave.DISCOVER_SENSORS: 'zwave', zwave.DISCOVER_SENSORS: 'zwave',
isy994.DISCOVER_SENSORS: 'isy994' isy994.DISCOVER_SENSORS: 'isy994',
verisure.DISCOVER_SENSORS: 'verisure'
} }

View File

@ -11,7 +11,7 @@ from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import ( from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID) STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
from homeassistant.components import group, discovery, wink, isy994 from homeassistant.components import group, discovery, wink, isy994, verisure
DOMAIN = 'switch' DOMAIN = 'switch'
DEPENDENCIES = [] DEPENDENCIES = []
@ -32,6 +32,7 @@ DISCOVERY_PLATFORMS = {
discovery.SERVICE_WEMO: 'wemo', discovery.SERVICE_WEMO: 'wemo',
wink.DISCOVER_SWITCHES: 'wink', wink.DISCOVER_SWITCHES: 'wink',
isy994.DISCOVER_SWITCHES: 'isy994', isy994.DISCOVER_SWITCHES: 'isy994',
verisure.DISCOVER_SWITCHES: 'verisure'
} }
PROP_TO_ATTR = { PROP_TO_ATTR = {

View File

@ -42,6 +42,8 @@ class VerisureSmartplug(SwitchDevice):
""" Represents a Verisure smartplug. """ """ Represents a Verisure smartplug. """
def __init__(self, smartplug_status): def __init__(self, smartplug_status):
self._id = smartplug_status.id self._id = smartplug_status.id
self.STATUS_ON = verisure.MY_PAGES.SMARTPLUG_ON
self.STATUS_OFF = verisure.MY_PAGES.SMARTPLUG_OFF
@property @property
def name(self): def name(self):
@ -52,16 +54,19 @@ class VerisureSmartplug(SwitchDevice):
def is_on(self): def is_on(self):
""" Returns True if on """ """ Returns True if on """
plug_status = verisure.get_smartplug_status()[self._id].status plug_status = verisure.get_smartplug_status()[self._id].status
return plug_status == verisure.MY_PAGES.SMARTPLUG_ON return plug_status == self.STATUS_ON
def turn_on(self): def turn_on(self):
""" Set smartplug status on """ """ Set smartplug status on """
verisure.MY_PAGES.set_smartplug_status( verisure.MY_PAGES.set_smartplug_status(
self._id, self._id,
verisure.MY_PAGES.SMARTPLUG_ON) self.STATUS_ON)
def turn_off(self): def turn_off(self):
""" Set smartplug status off. """ """ Set smartplug status off. """
verisure.MY_PAGES.set_smartplug_status( verisure.MY_PAGES.set_smartplug_status(
self._id, self._id,
verisure.MY_PAGES.SMARTPLUG_OFF) self.STATUS_OFF)
def update(self):
verisure.update()

View File

@ -5,13 +5,21 @@ components.verisure
import logging import logging
from datetime import timedelta from datetime import timedelta
from homeassistant import bootstrap
from homeassistant.loader import get_component
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.util import Throttle from homeassistant.util import Throttle
from homeassistant.const import ( from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_PLATFORM_DISCOVERED,
ATTR_SERVICE, ATTR_DISCOVERED,
CONF_USERNAME, CONF_PASSWORD) CONF_USERNAME, CONF_PASSWORD)
DOMAIN = "verisure" DOMAIN = "verisure"
DISCOVER_SENSORS = 'verisure.sensors'
DISCOVER_SWITCHES = 'verisure.switches'
DEPENDENCIES = [] DEPENDENCIES = []
REQUIREMENTS = [ REQUIREMENTS = [
'https://github.com/persandstrom/python-verisure/archive/master.zip' 'https://github.com/persandstrom/python-verisure/archive/master.zip'
@ -22,6 +30,12 @@ _LOGGER = logging.getLogger(__name__)
MY_PAGES = None MY_PAGES = None
STATUS = {} STATUS = {}
VERISURE_LOGIN_ERROR = None
VERISURE_ERROR = None
# if wrong password was given don't try again
WRONG_PASSWORD_GIVEN = False
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=5) MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=5)
@ -33,7 +47,7 @@ def setup(hass, config):
_LOGGER): _LOGGER):
return False return False
from verisure import MyPages from verisure import MyPages, LoginError, Error
STATUS[MyPages.DEVICE_ALARM] = {} STATUS[MyPages.DEVICE_ALARM] = {}
STATUS[MyPages.DEVICE_CLIMATE] = {} STATUS[MyPages.DEVICE_CLIMATE] = {}
@ -43,18 +57,27 @@ def setup(hass, config):
MY_PAGES = MyPages( MY_PAGES = MyPages(
config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD]) config[DOMAIN][CONF_PASSWORD])
MY_PAGES.login() global VERISURE_LOGIN_ERROR, VERISURE_ERROR
VERISURE_LOGIN_ERROR = LoginError
VERISURE_ERROR = Error
try:
MY_PAGES.login()
except (ConnectionError, Error) as ex:
_LOGGER.error('Could not log in to verisure mypages, %s', ex.message)
return False
update() update()
def stop_verisure(event): # Load components for the devices in the ISY controller that we support
""" Stop the Verisure service. """ for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
MY_PAGES.logout() ('switch', DISCOVER_SWITCHES))):
component = get_component(comp_name)
bootstrap.setup_component(hass, component.DOMAIN, config)
def start_verisure(event): hass.bus.fire(EVENT_PLATFORM_DISCOVERED,
""" Start the Verisure service. """ {ATTR_SERVICE: discovery,
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_verisure) ATTR_DISCOVERED: {}})
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_verisure)
return True return True
@ -74,9 +97,25 @@ def get_smartplug_status():
return STATUS[MY_PAGES.DEVICE_SMARTPLUG] return STATUS[MY_PAGES.DEVICE_SMARTPLUG]
def reconnect():
''' reconnect to verisure mypages '''
try:
MY_PAGES.login()
except VERISURE_LOGIN_ERROR as ex:
_LOGGER.error("Could not login to Verisure mypages, %s", ex.message)
global WRONG_PASSWORD_GIVEN
WRONG_PASSWORD_GIVEN = True
except (ConnectionError, VERISURE_ERROR) as ex:
_LOGGER.error("Could not login to Verisure mypages, %s", ex.message)
@Throttle(MIN_TIME_BETWEEN_REQUESTS) @Throttle(MIN_TIME_BETWEEN_REQUESTS)
def update(): def update():
''' Updates the status of verisure components ''' ''' Updates the status of verisure components '''
if WRONG_PASSWORD_GIVEN:
# Is there any way to inform user?
return
try: try:
for overview in MY_PAGES.get_overview(MY_PAGES.DEVICE_ALARM): for overview in MY_PAGES.get_overview(MY_PAGES.DEVICE_ALARM):
STATUS[MY_PAGES.DEVICE_ALARM][overview.id] = overview STATUS[MY_PAGES.DEVICE_ALARM][overview.id] = overview
@ -86,4 +125,4 @@ def update():
STATUS[MY_PAGES.DEVICE_SMARTPLUG][overview.id] = overview STATUS[MY_PAGES.DEVICE_SMARTPLUG][overview.id] = overview
except ConnectionError as ex: except ConnectionError as ex:
_LOGGER.error('Caught connection error %s, tries to reconnect', ex) _LOGGER.error('Caught connection error %s, tries to reconnect', ex)
MY_PAGES.login() reconnect()