hass-core/homeassistant/components/sensor/eliqonline.py

97 lines
2.6 KiB
Python
Raw Normal View History

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