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

78 lines
2.2 KiB
Python
Raw Normal View History

"""
2015-12-17 19:44:18 +01: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/
"""
import logging
2016-02-16 18:12:52 +01:00
from urllib.error import URLError
2016-02-18 21:27:50 -08:00
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, STATE_UNKNOWN
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['eliqonline==1.0.11']
2015-12-16 16:35:35 +01:00
DEFAULT_NAME = "ELIQ Energy Usage"
2015-12-15 22:05:55 +01:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 16:46:34 +01:00
"""Setup the Eliq 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)
2015-12-15 22:05:55 +01:00
channel_id = config.get("channel_id")
2015-12-16 16:36:56 +01:00
if access_token is None:
_LOGGER.error(
2015-12-16 18:11:44 +01:00
"Configuration Error: "
"Please make sure you have configured your access token "
2015-12-15 22:05:55 +01:00
"that can be aquired from https://my.eliq.se/user/settings/api")
2015-12-16 16:36:56 +01:00
return False
api = eliqonline.API(access_token)
add_devices([EliqSensor(api, channel_id, name)])
2015-12-15 22:05:55 +01:00
class EliqSensor(Entity):
2016-03-08 16:46:34 +01:00
"""Implementation of an Eliq 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._unit_of_measurement = "W"
self._state = STATE_UNKNOWN
2015-12-15 22:05:55 +01:00
self.api = api
self.channel_id = channel_id
self.update()
@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."""
2015-12-25 18:50:35 +01:00
return "mdi:speedometer"
@property
def unit_of_measurement(self):
2016-03-08 16:46:34 +01:00
"""Return the unit of measurement of this entity, if any."""
return self._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:
response = self.api.get_data_now(channelid=self.channel_id)
self._state = int(response.power)
2016-02-16 18:12:52 +01:00
except (TypeError, URLError):
2016-02-23 06:21:49 +01:00
_LOGGER.error("Could not connect to the eliqonline servers")