Added a switch

This commit is contained in:
Per Sandstrom 2015-08-12 13:00:47 +02:00
parent 92fc7eab36
commit c7ca6e4784
3 changed files with 156 additions and 36 deletions

View File

@ -8,10 +8,13 @@ import logging
import homeassistant.components.verisure as verisure import homeassistant.components.verisure as verisure
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.const import STATE_OPEN, STATE_CLOSED from homeassistant.const import TEMP_CELCIUS
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['verisure']
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Verisure platform. """ """ Sets up the Verisure platform. """
@ -19,14 +22,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error('A connection has not been made to Verisure mypages.') _LOGGER.error('A connection has not been made to Verisure mypages.')
return False return False
sensors = [ sensors = []
VerisureClimateDevice(status) for status
in verisure.MY_PAGES.get_climate_status()]
sensors.extend([ sensors.extend([
VerisureAlarmDevice(status) for status VerisureClimateDevice(value)
in verisure.MY_PAGES.get_alarm_status()]) for value in verisure.get_climate_status().values()
])
sensors.extend([
VerisureAlarmDevice(value)
for value in verisure.get_alarm_status().values()
])
add_devices(sensors) add_devices(sensors)
@ -35,31 +41,45 @@ class VerisureClimateDevice(Entity):
""" represents a Verisure climate sensor within home assistant. """ """ represents a Verisure climate sensor within home assistant. """
def __init__(self, climate_status): def __init__(self, climate_status):
self._status = climate_status self._id = climate_status.id
self._device = verisure.MY_PAGES.DEVICE_CLIMATE
@property @property
def name(self): def name(self):
""" Returns the name of the device. """ """ Returns the name of the device. """
return self._status.location return verisure.STATUS[self._device][self._id].location
@property @property
def state(self): def state(self):
""" Returns the state of the device. """ """ Returns the state of the device. """
return self._status.temperature # remove ° character
return verisure.STATUS[self._device][self._id].temperature[:-1]
@property
def unit_of_measurement(self):
""" Unit of measurement of this entity """
return TEMP_CELCIUS # can verisure report in fahrenheit?
def update(self):
verisure.update()
class VerisureAlarmDevice(Entity): class VerisureAlarmDevice(Entity):
""" represents a Verisure alarm remote control within home assistant. """ """ represents a Verisure alarm status within home assistant. """
def __init__(self, alarm_status): def __init__(self, alarm_status):
self._status = alarm_status self._id = alarm_status.id
self._device = verisure.MY_PAGES.DEVICE_ALARM
@property @property
def name(self): def name(self):
""" Returns the name of the device. """ """ Returns the name of the device. """
return 'Alarm {}'.format(self._status.id) return 'Alarm {}'.format(self._id)
@property @property
def state(self): def state(self):
""" Returns the state of the device. """ """ Returns the state of the device. """
return self._status.status return verisure.STATUS[self._device][self._id].status
def update(self):
verisure.update()

View File

@ -0,0 +1,67 @@
"""
homeassistant.components.switch.verisure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Verisure Smartplugs
Configuration:
switch:
platform: verisure
Variables:
"""
import logging
import homeassistant.components.verisure as verisure
from homeassistant.components.switch import SwitchDevice
DEPENDENCIES = ['verisure']
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Arduino platform. """
if not verisure.MY_PAGES:
_LOGGER.error('A connection has not been made to Verisure mypages.')
return False
switches = []
switches.extend([
VerisureSmartplug(value)
for value in verisure.get_smartplug_status().values()
])
add_devices(switches)
class VerisureSmartplug(SwitchDevice):
""" Represents a Verisure smartplug. """
def __init__(self, smartplug_status):
self._id = smartplug_status.id
@property
def name(self):
""" Get the name (location) of the smartplug. """
return verisure.get_smartplug_status()[self._id].location
@property
def is_on(self):
""" Returns True if on """
plug_status = verisure.get_smartplug_status()[self._id].status
return plug_status == verisure.MY_PAGES.SMARTPLUG_ON
def turn_on(self):
""" Set smartplug status on """
verisure.MY_PAGES.set_smartplug_status(
self._id,
verisure.MY_PAGES.SMARTPLUG_ON)
def turn_off(self):
""" Set smartplug status off. """
verisure.MY_PAGES.set_smartplug_status(
self._id,
verisure.MY_PAGES.SMARTPLUG_OFF)

View File

@ -3,24 +3,27 @@ components.verisure
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
""" """
import logging import logging
from datetime import timedelta
from homeassistant import bootstrap
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.loader import get_component from homeassistant.util import Throttle
from homeassistant.const import ( from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
CONF_USERNAME, CONF_PASSWORD, CONF_USERNAME, CONF_PASSWORD)
EVENT_PLATFORM_DISCOVERED,
ATTR_SERVICE, ATTR_DISCOVERED, ATTR_FRIENDLY_NAME)
DOMAIN = "verisure" DOMAIN = "verisure"
DEPENDENCIES = [] DEPENDENCIES = []
REQUIREMENTS = ['https://github.com/persandstrom/python-verisure/archive/master.zip'] REQUIREMENTS = [
'https://github.com/persandstrom/python-verisure/archive/master.zip'
]
MY_PAGES = None
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DISCOVER_SENSORS = "wink.sensors" MY_PAGES = None
STATUS = {}
MIN_TIME_BETWEEN_REQUESTS = timedelta(seconds=5)
def setup(hass, config): def setup(hass, config):
""" Setup the Verisure component. """ """ Setup the Verisure component. """
@ -31,27 +34,57 @@ def setup(hass, config):
return False return False
from verisure import MyPages from verisure import MyPages
STATUS[MyPages.DEVICE_ALARM] = {}
STATUS[MyPages.DEVICE_CLIMATE] = {}
STATUS[MyPages.DEVICE_SMARTPLUG] = {}
global MY_PAGES global MY_PAGES
MY_PAGES = MyPages(config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_PASSWORD]) MY_PAGES = MyPages(
config[DOMAIN][CONF_USERNAME],
config[DOMAIN][CONF_PASSWORD])
MY_PAGES.login() MY_PAGES.login()
update()
component = get_component('sensor')
bootstrap.setup_component(hass, component.DOMAIN, config)
# Fire discovery event
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
ATTR_SERVICE: DISCOVER_SENSORS,
ATTR_DISCOVERED: {}
})
def stop_verisure(event): def stop_verisure(event):
""" Stop the Arduino service. """ """ Stop the Verisure service. """
MY_PAGES.logout() MY_PAGES.logout()
def start_verisure(event): def start_verisure(event):
""" Start the Arduino service. """ """ Start the Verisure service. """
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_verisure) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_verisure)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_verisure) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_verisure)
return True return True
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:
_LOGGER.error('Caught connection error {}, tries to reconnect'.format(
ex))
MY_PAGES.login()