core/homeassistant/components/eliqonline/sensor.py

103 lines
3.0 KiB
Python
Raw Normal View History

"""
2016-04-28 15:33:33 +00:00
Monitors home energy use for the ELIQ Online service.
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/
"""
from datetime import timedelta
import logging
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, POWER_WATT)
from homeassistant.helpers.entity import Entity
2016-09-04 02:36:21 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2016-09-04 02:36:21 +00:00
REQUIREMENTS = ['eliqonline==1.2.2']
_LOGGER = logging.getLogger(__name__)
2016-09-04 02:36:21 +00:00
CONF_CHANNEL_ID = 'channel_id'
DEFAULT_NAME = 'ELIQ Online'
2017-02-25 11:51:48 +00:00
ICON = 'mdi:gauge'
2016-09-04 02:36:21 +00:00
SCAN_INTERVAL = timedelta(seconds=60)
UNIT_OF_MEASUREMENT = POWER_WATT
2016-09-04 02:36:21 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Required(CONF_CHANNEL_ID): cv.positive_int,
2016-09-04 02:36:21 +00:00
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
2015-12-15 21:05:55 +00:00
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the ELIQ Online sensor."""
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)
session = async_get_clientsession(hass)
api = eliqonline.API(session=session,
access_token=access_token)
2016-04-28 15:33:33 +00:00
try:
_LOGGER.debug("Probing for access to ELIQ Online API")
await 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 15:33:33 +00:00
return False
async_add_entities([EliqSensor(api, channel_id, name)], True)
2015-12-15 21:05:55 +00:00
class EliqSensor(Entity):
2016-04-28 15:33:33 +00:00
"""Implementation of an ELIQ Online sensor."""
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
self._state = None
2016-04-28 15:33:33 +00:00
self._api = api
self._channel_id = channel_id
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
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
@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
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the device."""
return self._state
async def async_update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data."""
try:
response = await self._api.get_data_now(channelid=self._channel_id)
self._state = int(response["power"])
2016-04-28 15:33:33 +00:00
_LOGGER.debug("Updated power from server %d W", self._state)
except KeyError:
_LOGGER.warning("Invalid response from ELIQ Online API")
except OSError as error:
_LOGGER.warning("Could not connect to the ELIQ Online API: %s",
error)