From dade33187fddc6669e0f1d261b9a48aaf855ff13 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 15 Dec 2015 21:48:42 +0100 Subject: [PATCH 1/9] added support for eliq online energy sensor --- homeassistant/components/sensor/eliqonline.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 homeassistant/components/sensor/eliqonline.py diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py new file mode 100644 index 00000000000..1820a24e2d9 --- /dev/null +++ b/homeassistant/components/sensor/eliqonline.py @@ -0,0 +1,85 @@ +""" +homeassistant.components.sensor.eliqonline +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +monitors home energy use for the eliq online service + +api documentation: + https://my.eliq.se/knowledge/sv-SE/49-eliq-online/299-eliq-online-api + +access to api access token: + https://my.eliq.se/user/settings/api + +current energy use: + https://my.eliq.se/api/datanow?accesstoken= + +history: + https://my.eliq.se/api/data?startdate=2015-12-14&intervaltype=6min&accesstoken= + +""" + +import logging +from datetime import timedelta +from homeassistant.helpers.entity import Entity +from homeassistant.util import Throttle +from homeassistant.const import (STATE_UNKNOWN, CONF_ACCESS_TOKEN, CONF_NAME) + +_LOGGER = logging.getLogger(__name__) + +REQUIREMENTS = ['eliqonline==1.0.11'] + +MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) + +def setup_platform(hass, config, add_devices, discovery_info=None): + import eliqonline + + access_token = config.get(CONF_ACCESS_TOKEN) + name = config.get(CONF_NAME) + channel_id = config.get("channel_id") + + if not access_token: + _LOGGER.error( + "Configuration Error" + "Please make sure you have configured your access token which can be aquired from https://my.eliq.se/user/settings/api") + return None + + api = eliqonline.API(access_token) + add_devices([EliqSensor(api, channel_id, name)]) + + +class EliqSensor(Entity): + """ Implements a Eliq sensor. """ + + def __init__(self, api, channel_id, name): + self._name = "Energy Usage" + if name: + self._name = name + " " + self._name + self._unit_of_measurement = "W" + self._state = STATE_UNKNOWN + + self.api = api + self.channel_id = channel_id + self.update() + + + @property + def name(self): + """ Returns the name. """ + return self._name + + + @property + def unit_of_measurement(self): + """ Unit of measurement of this entity, if any. """ + return self._unit_of_measurement + + + @property + def state(self): + """ Returns the state of the device. """ + return self._state + + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """ Gets the latest data """ + self._state = int(self.api.get_data_now(channelid=self.channel_id).power) From a764817ee9bbbcdff842584db6d3c190dd66245c Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 15 Dec 2015 22:05:55 +0100 Subject: [PATCH 2/9] lint ok --- homeassistant/components/sensor/eliqonline.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index 1820a24e2d9..10c63f413f0 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -29,22 +29,26 @@ REQUIREMENTS = ['eliqonline==1.0.11'] MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) + def setup_platform(hass, config, add_devices, discovery_info=None): + """ Set up the sensors """ + import eliqonline access_token = config.get(CONF_ACCESS_TOKEN) - name = config.get(CONF_NAME) - channel_id = config.get("channel_id") + name = config.get(CONF_NAME) + channel_id = config.get("channel_id") if not access_token: _LOGGER.error( "Configuration Error" - "Please make sure you have configured your access token which can be aquired from https://my.eliq.se/user/settings/api") + "Please make sure you have configured your access token " + + "that can be aquired from https://my.eliq.se/user/settings/api") return None api = eliqonline.API(access_token) add_devices([EliqSensor(api, channel_id, name)]) - + class EliqSensor(Entity): """ Implements a Eliq sensor. """ @@ -55,31 +59,28 @@ class EliqSensor(Entity): self._name = name + " " + self._name self._unit_of_measurement = "W" self._state = STATE_UNKNOWN - + self.api = api self.channel_id = channel_id self.update() - @property def name(self): """ Returns the name. """ return self._name - @property def unit_of_measurement(self): """ Unit of measurement of this entity, if any. """ return self._unit_of_measurement - @property def state(self): """ Returns the state of the device. """ return self._state - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """ Gets the latest data """ - self._state = int(self.api.get_data_now(channelid=self.channel_id).power) + response = self.api.get_data_now(channelid=self.channel_id) + self._state = int(response.power) From 956a6418cc78c45b2be2abe3b13b4528ae49d7eb Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 16:35:35 +0100 Subject: [PATCH 3/9] let the user supply the sensor name --- homeassistant/components/sensor/eliqonline.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index 10c63f413f0..dd6a1c4fac3 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -26,6 +26,7 @@ from homeassistant.const import (STATE_UNKNOWN, CONF_ACCESS_TOKEN, CONF_NAME) _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['eliqonline==1.0.11'] +DEFAULT_NAME = "ELIQ Energy Usage" MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) @@ -36,7 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): import eliqonline access_token = config.get(CONF_ACCESS_TOKEN) - name = config.get(CONF_NAME) + name = config.get(CONF_NAME, DEFAULT_NAME) channel_id = config.get("channel_id") if not access_token: @@ -54,9 +55,7 @@ class EliqSensor(Entity): """ Implements a Eliq sensor. """ def __init__(self, api, channel_id, name): - self._name = "Energy Usage" - if name: - self._name = name + " " + self._name + self._name = name self._unit_of_measurement = "W" self._state = STATE_UNKNOWN From 3b8c5d6833c249f4f7d6d4a6b0bfdd989423a76e Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 16:36:04 +0100 Subject: [PATCH 4/9] disable throttling for now --- homeassistant/components/sensor/eliqonline.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index dd6a1c4fac3..fcc52915e28 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -20,7 +20,6 @@ history: import logging from datetime import timedelta from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle from homeassistant.const import (STATE_UNKNOWN, CONF_ACCESS_TOKEN, CONF_NAME) _LOGGER = logging.getLogger(__name__) @@ -28,8 +27,6 @@ _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['eliqonline==1.0.11'] DEFAULT_NAME = "ELIQ Energy Usage" -MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) - def setup_platform(hass, config, add_devices, discovery_info=None): """ Set up the sensors """ @@ -78,7 +75,6 @@ class EliqSensor(Entity): """ Returns the state of the device. """ return self._state - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """ Gets the latest data """ response = self.api.get_data_now(channelid=self.channel_id) From e76defe03574bd7278ecdf4c7c91083a8cda5fd0 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 16:36:56 +0100 Subject: [PATCH 5/9] proper access_token check --- homeassistant/components/sensor/eliqonline.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index fcc52915e28..ebec1410a59 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -37,12 +37,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): name = config.get(CONF_NAME, DEFAULT_NAME) channel_id = config.get("channel_id") - if not access_token: + if access_token is None: _LOGGER.error( "Configuration Error" "Please make sure you have configured your access token " + "that can be aquired from https://my.eliq.se/user/settings/api") - return None + return False api = eliqonline.API(access_token) add_devices([EliqSensor(api, channel_id, name)]) From 14056951c70d14f778df1ec8cbe2d32278330c8d Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 16:41:34 +0100 Subject: [PATCH 6/9] lint --- homeassistant/components/sensor/eliqonline.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index ebec1410a59..df9262f9503 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -18,7 +18,6 @@ history: """ import logging -from datetime import timedelta from homeassistant.helpers.entity import Entity from homeassistant.const import (STATE_UNKNOWN, CONF_ACCESS_TOKEN, CONF_NAME) From 0075752c066139e08a09a61459e6bf62111ed775 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 18:11:44 +0100 Subject: [PATCH 7/9] better error message --- homeassistant/components/sensor/eliqonline.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/eliqonline.py b/homeassistant/components/sensor/eliqonline.py index df9262f9503..ce638aaac26 100644 --- a/homeassistant/components/sensor/eliqonline.py +++ b/homeassistant/components/sensor/eliqonline.py @@ -38,8 +38,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if access_token is None: _LOGGER.error( - "Configuration Error" - "Please make sure you have configured your access token " + + "Configuration Error: " + "Please make sure you have configured your access token " "that can be aquired from https://my.eliq.se/user/settings/api") return False From 70bc6f4506dd043162d95f7cfdee00f36112e56d Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 20:54:11 +0100 Subject: [PATCH 8/9] added eliqonline --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 94f4352ef89..32cc82cd7f1 100644 --- a/.coveragerc +++ b/.coveragerc @@ -88,6 +88,7 @@ omit = homeassistant/components/sensor/dht.py homeassistant/components/sensor/dweet.py homeassistant/components/sensor/efergy.py + homeassistant/components/sensor/eliqonline.py homeassistant/components/sensor/forecast.py homeassistant/components/sensor/glances.py homeassistant/components/sensor/mysensors.py From 69d8b995d4d39f0578b92ebdbc63d31ed0d1dffa Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 16 Dec 2015 20:54:25 +0100 Subject: [PATCH 9/9] added eliqonline --- requirements_all.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/requirements_all.txt b/requirements_all.txt index 48a127b0f30..c1955371152 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -122,6 +122,9 @@ py-cpuinfo==0.1.6 # homeassistant.components.sensor.dweet dweepy==0.2.0 +# homeassistant.components.sensor.eliqonline +eliqonline==1.0.11 + # homeassistant.components.sensor.forecast python-forecastio==1.3.3