2015-12-15 20:48:42 +00:00
|
|
|
"""
|
2016-04-28 15:33:33 +00:00
|
|
|
Monitors home energy use for the ELIQ Online service.
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2015-12-17 18:44:18 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.eliqonline/
|
2015-12-15 20:48:42 +00:00
|
|
|
"""
|
2017-01-05 23:16:12 +00:00
|
|
|
from datetime import timedelta
|
2015-12-15 20:48:42 +00:00
|
|
|
import logging
|
2016-02-16 17:12:52 +00:00
|
|
|
from urllib.error import URLError
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-09-04 02:36:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (CONF_ACCESS_TOKEN, CONF_NAME, STATE_UNKNOWN)
|
2015-12-15 20:48:42 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-04 02:36:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2016-12-17 20:14:04 +00:00
|
|
|
REQUIREMENTS = ['eliqonline==1.0.13']
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-04 02:36:21 +00:00
|
|
|
CONF_CHANNEL_ID = 'channel_id'
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'ELIQ Online'
|
|
|
|
|
|
|
|
ICON = 'mdi:speedometer'
|
|
|
|
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2016-09-04 02:36:21 +00:00
|
|
|
UNIT_OF_MEASUREMENT = 'W'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_CHANNEL_ID): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2015-12-15 21:05:55 +00:00
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-04-28 15:33:33 +00:00
|
|
|
"""Setup the ELIQ Online sensor."""
|
2015-12-15 20:48:42 +00:00
|
|
|
import eliqonline
|
|
|
|
|
|
|
|
access_token = config.get(CONF_ACCESS_TOKEN)
|
2015-12-16 15:35:35 +00:00
|
|
|
name = config.get(CONF_NAME, DEFAULT_NAME)
|
2016-04-28 15:33:33 +00:00
|
|
|
channel_id = config.get(CONF_CHANNEL_ID)
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
api = eliqonline.API(access_token)
|
2016-04-28 15:33:33 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
_LOGGER.debug("Probing for access to ELIQ Online API")
|
|
|
|
api.get_data_now(channelid=channel_id)
|
|
|
|
except URLError:
|
|
|
|
_LOGGER.error("Could not access the ELIQ Online API. "
|
|
|
|
"Is the configuration valid?")
|
|
|
|
return False
|
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
add_devices([EliqSensor(api, channel_id, name)])
|
2015-12-15 21:05:55 +00:00
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
class EliqSensor(Entity):
|
2016-04-28 15:33:33 +00:00
|
|
|
"""Implementation of an ELIQ Online sensor."""
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
def __init__(self, api, channel_id, name):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-12-16 15:35:35 +00:00
|
|
|
self._name = name
|
2015-12-15 20:48:42 +00:00
|
|
|
self._state = STATE_UNKNOWN
|
2016-04-28 15:33:33 +00:00
|
|
|
self._api = api
|
|
|
|
self._channel_id = channel_id
|
2016-01-04 20:12:10 +00:00
|
|
|
self.update()
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-12-15 20:48:42 +00:00
|
|
|
return self._name
|
|
|
|
|
2015-12-25 17:50:35 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return icon."""
|
2016-04-28 15:33:33 +00:00
|
|
|
return ICON
|
2015-12-25 17:50:35 +00:00
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-04-28 15:33:33 +00:00
|
|
|
return UNIT_OF_MEASUREMENT
|
2015-12-15 20:48:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the device."""
|
2015-12-15 20:48:42 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data."""
|
2016-01-04 20:12:10 +00:00
|
|
|
try:
|
2016-04-28 15:33:33 +00:00
|
|
|
response = self._api.get_data_now(channelid=self._channel_id)
|
2016-01-04 20:12:10 +00:00
|
|
|
self._state = int(response.power)
|
2016-04-28 15:33:33 +00:00
|
|
|
_LOGGER.debug("Updated power from server %d W", self._state)
|
|
|
|
except URLError:
|
|
|
|
_LOGGER.error("Could not connect to the ELIQ Online API")
|