2019-04-03 15:40:03 +00:00
|
|
|
"""Monitors home energy use for the ELIQ Online service."""
|
2019-10-20 21:39:24 +00:00
|
|
|
import asyncio
|
2017-01-05 23:16:12 +00:00
|
|
|
from datetime import timedelta
|
2015-12-15 20:48:42 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2019-10-20 21:39:24 +00:00
|
|
|
import eliqonline
|
2016-09-04 02:36:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-06-23 17:44:55 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
2021-12-10 08:04:07 +00:00
|
|
|
SensorStateClass,
|
2021-06-23 17:44:55 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME, POWER_WATT
|
2018-12-14 12:25:28 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2019-10-20 21:39:24 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CHANNEL_ID = "channel_id"
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "ELIQ Online"
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:gauge"
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2019-03-02 10:29:59 +00:00
|
|
|
UNIT_OF_MEASUREMENT = POWER_WATT
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
|
|
|
vol.Required(CONF_CHANNEL_ID): cv.positive_int,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 02:36:21 +00:00
|
|
|
|
2015-12-15 21:05:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the ELIQ Online sensor."""
|
2015-12-15 20:48:42 +00:00
|
|
|
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)
|
2018-12-14 12:25:28 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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")
|
2018-12-14 12:25:28 +00:00
|
|
|
await api.get_data_now(channelid=channel_id)
|
2017-06-10 08:12:30 +00:00
|
|
|
except OSError as error:
|
2017-08-06 17:21:55 +00:00
|
|
|
_LOGGER.error("Could not access the ELIQ Online API: %s", error)
|
2016-04-28 15:33:33 +00:00
|
|
|
return False
|
|
|
|
|
2018-12-14 12:25:28 +00:00
|
|
|
async_add_entities([EliqSensor(api, channel_id, name)], True)
|
2015-12-15 21:05:55 +00:00
|
|
|
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class EliqSensor(SensorEntity):
|
2016-04-28 15:33:33 +00:00
|
|
|
"""Implementation of an ELIQ Online sensor."""
|
2015-12-15 20:48:42 +00:00
|
|
|
|
2021-12-10 08:04:07 +00:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-06-23 17:44:55 +00:00
|
|
|
|
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
|
2018-12-14 12:25:28 +00:00
|
|
|
self._state = None
|
2016-04-28 15:33:33 +00:00
|
|
|
self._api = api
|
|
|
|
self._channel_id = channel_id
|
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
|
2021-08-11 16:57:12 +00:00
|
|
|
def native_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
|
2021-08-11 16:57:12 +00:00
|
|
|
def native_value(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
|
|
|
|
|
2018-12-14 12:25:28 +00:00
|
|
|
async def async_update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data."""
|
2016-01-04 20:12:10 +00:00
|
|
|
try:
|
2018-12-14 12:25:28 +00:00
|
|
|
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)
|
2018-12-14 12:25:28 +00:00
|
|
|
except KeyError:
|
|
|
|
_LOGGER.warning("Invalid response from ELIQ Online API")
|
2019-04-17 13:00:57 +00:00
|
|
|
except (OSError, asyncio.TimeoutError) as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Could not connect to the ELIQ Online API: %s", error)
|