From 92fc7eab36dfc7823c7ea03fbff363d8f8c20c96 Mon Sep 17 00:00:00 2001 From: Per Sandstrom Date: Tue, 11 Aug 2015 09:28:07 +0200 Subject: [PATCH] added component and sensor --- .gitmodules | 3 + homeassistant/components/sensor/verisure.py | 65 +++++++++++++++++++++ homeassistant/components/verisure.py | 57 ++++++++++++++++++ requirements.txt | 2 + 4 files changed, 127 insertions(+) create mode 100644 homeassistant/components/sensor/verisure.py create mode 100644 homeassistant/components/verisure.py diff --git a/.gitmodules b/.gitmodules index a627e522d8f..2446411c57c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "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 +[submodule "homeassistant/external/verisure"] + path = homeassistant/external/verisure + url = https://github.com/persandstrom/python-verisure.git diff --git a/homeassistant/components/sensor/verisure.py b/homeassistant/components/sensor/verisure.py new file mode 100644 index 00000000000..6f4f8054772 --- /dev/null +++ b/homeassistant/components/sensor/verisure.py @@ -0,0 +1,65 @@ +""" +homeassistant.components.sensor.verisure +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Interfaces with Verisure sensors. +""" +import logging + +import homeassistant.components.verisure as verisure + +from homeassistant.helpers.entity import Entity +from homeassistant.const import STATE_OPEN, STATE_CLOSED + +_LOGGER = logging.getLogger(__name__) + +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up the Verisure platform. """ + + if not verisure.MY_PAGES: + _LOGGER.error('A connection has not been made to Verisure mypages.') + return False + + sensors = [ + VerisureClimateDevice(status) for status + in verisure.MY_PAGES.get_climate_status()] + + sensors.extend([ + VerisureAlarmDevice(status) for status + in verisure.MY_PAGES.get_alarm_status()]) + + + add_devices(sensors) + + +class VerisureClimateDevice(Entity): + """ represents a Verisure climate sensor within home assistant. """ + + def __init__(self, climate_status): + self._status = climate_status + + @property + def name(self): + """ Returns the name of the device. """ + return self._status.location + + @property + def state(self): + """ Returns the state of the device. """ + return self._status.temperature + + +class VerisureAlarmDevice(Entity): + """ represents a Verisure alarm remote control within home assistant. """ + + def __init__(self, alarm_status): + self._status = alarm_status + + @property + def name(self): + """ Returns the name of the device. """ + return 'Alarm {}'.format(self._status.id) + + @property + def state(self): + """ Returns the state of the device. """ + return self._status.status diff --git a/homeassistant/components/verisure.py b/homeassistant/components/verisure.py new file mode 100644 index 00000000000..77a34ad979d --- /dev/null +++ b/homeassistant/components/verisure.py @@ -0,0 +1,57 @@ +""" +components.verisure +~~~~~~~~~~~~~~~~~~ +""" +import logging + +from homeassistant import bootstrap +from homeassistant.helpers import validate_config +from homeassistant.loader import get_component +from homeassistant.const import ( + EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, + CONF_USERNAME, CONF_PASSWORD, + EVENT_PLATFORM_DISCOVERED, + ATTR_SERVICE, ATTR_DISCOVERED, ATTR_FRIENDLY_NAME) + +DOMAIN = "verisure" +DEPENDENCIES = [] +REQUIREMENTS = ['https://github.com/persandstrom/python-verisure/archive/master.zip'] + +MY_PAGES = None +_LOGGER = logging.getLogger(__name__) + +DISCOVER_SENSORS = "wink.sensors" + +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 + global MY_PAGES + MY_PAGES = MyPages(config[DOMAIN][CONF_USERNAME], config[DOMAIN][CONF_PASSWORD]) + MY_PAGES.login() + + 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): + """ Stop the Arduino service. """ + MY_PAGES.logout() + + def start_verisure(event): + """ Start the Arduino service. """ + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_verisure) + + hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_verisure) + + return True diff --git a/requirements.txt b/requirements.txt index 445a2fe3657..24027be2d3b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -110,3 +110,5 @@ paho-mqtt>=1.1 # PyModbus (modbus) https://github.com/bashwork/pymodbus/archive/python3.zip#pymodbus>=1.2.0 +# Verisure +https://github.com/persandstrom/python-verisure/archive/master.zip