2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Efergy sensors."""
|
2021-09-21 21:53:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-09 09:23:40 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from pyefergy import Efergy, exceptions
|
2018-07-09 12:39:28 +00:00
|
|
|
import voluptuous as vol
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2021-09-21 21:53:35 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2021-02-08 08:45:05 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_CURRENCY,
|
|
|
|
CONF_MONITORED_VARIABLES,
|
|
|
|
CONF_TYPE,
|
2021-09-21 21:53:35 +00:00
|
|
|
DEVICE_CLASS_ENERGY,
|
|
|
|
DEVICE_CLASS_MONETARY,
|
|
|
|
DEVICE_CLASS_POWER,
|
2021-02-08 08:45:05 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
)
|
2021-09-21 21:53:35 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-09 09:23:40 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2021-09-24 10:54:15 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2018-07-09 12:39:28 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-09-21 21:53:35 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_APPTOKEN = "app_token"
|
|
|
|
CONF_UTC_OFFSET = "utc_offset"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PERIOD = "period"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_INSTANT = "instant_readings"
|
|
|
|
CONF_AMOUNT = "amount"
|
|
|
|
CONF_BUDGET = "budget"
|
|
|
|
CONF_COST = "cost"
|
|
|
|
CONF_CURRENT_VALUES = "current_values"
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_PERIOD = "year"
|
|
|
|
DEFAULT_UTC_OFFSET = "0"
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2021-10-09 09:23:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-09-21 21:53:35 +00:00
|
|
|
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|
|
|
CONF_INSTANT: SensorEntityDescription(
|
|
|
|
key=CONF_INSTANT,
|
|
|
|
name="Energy Usage",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
),
|
|
|
|
CONF_AMOUNT: SensorEntityDescription(
|
|
|
|
key=CONF_AMOUNT,
|
|
|
|
name="Energy Consumed",
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
),
|
|
|
|
CONF_BUDGET: SensorEntityDescription(
|
|
|
|
key=CONF_BUDGET,
|
|
|
|
name="Energy Budget",
|
|
|
|
),
|
|
|
|
CONF_COST: SensorEntityDescription(
|
|
|
|
key=CONF_COST,
|
|
|
|
name="Energy Cost",
|
|
|
|
device_class=DEVICE_CLASS_MONETARY,
|
|
|
|
),
|
|
|
|
CONF_CURRENT_VALUES: SensorEntityDescription(
|
|
|
|
key=CONF_CURRENT_VALUES,
|
|
|
|
name="Per-Device Usage",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
native_unit_of_measurement=POWER_WATT,
|
|
|
|
),
|
2015-07-09 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 06:00:17 +00:00
|
|
|
TYPES_SCHEMA = vol.In(SENSOR_TYPES)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSORS_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-02-08 08:45:05 +00:00
|
|
|
vol.Required(CONF_TYPE): TYPES_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_CURRENCY, default=""): cv.string,
|
|
|
|
vol.Optional(CONF_PERIOD, default=DEFAULT_PERIOD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_APPTOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_UTC_OFFSET, default=DEFAULT_UTC_OFFSET): cv.string,
|
|
|
|
vol.Required(CONF_MONITORED_VARIABLES): [SENSORS_SCHEMA],
|
|
|
|
}
|
|
|
|
)
|
2016-08-23 03:51:17 +00:00
|
|
|
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2021-09-24 10:54:15 +00:00
|
|
|
async def async_setup_platform(
|
2021-09-21 21:53:35 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType = None,
|
|
|
|
) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Efergy sensor."""
|
2021-09-24 10:54:15 +00:00
|
|
|
api = Efergy(
|
|
|
|
config[CONF_APPTOKEN],
|
|
|
|
async_get_clientsession(hass),
|
|
|
|
utc_offset=config[CONF_UTC_OFFSET],
|
|
|
|
)
|
2018-07-09 12:39:28 +00:00
|
|
|
|
2015-07-09 16:05:19 +00:00
|
|
|
dev = []
|
2021-10-09 09:23:40 +00:00
|
|
|
try:
|
|
|
|
sensors = await api.get_sids()
|
|
|
|
except (exceptions.DataError, exceptions.ConnectTimeout) as ex:
|
|
|
|
raise PlatformNotReady("Error getting data from Efergy:") from ex
|
2016-08-23 03:51:17 +00:00
|
|
|
for variable in config[CONF_MONITORED_VARIABLES]:
|
2021-02-08 08:45:05 +00:00
|
|
|
if variable[CONF_TYPE] == CONF_CURRENT_VALUES:
|
2021-09-24 10:54:15 +00:00
|
|
|
for sensor in sensors:
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(
|
|
|
|
EfergySensor(
|
2021-09-24 10:54:15 +00:00
|
|
|
api,
|
2019-07-31 19:25:30 +00:00
|
|
|
variable[CONF_PERIOD],
|
|
|
|
variable[CONF_CURRENCY],
|
2021-09-21 21:53:35 +00:00
|
|
|
SENSOR_TYPES[variable[CONF_TYPE]],
|
2021-09-24 10:54:15 +00:00
|
|
|
sid=sensor["sid"],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
dev.append(
|
|
|
|
EfergySensor(
|
2021-09-24 10:54:15 +00:00
|
|
|
api,
|
2019-07-31 19:25:30 +00:00
|
|
|
variable[CONF_PERIOD],
|
|
|
|
variable[CONF_CURRENCY],
|
2021-09-21 21:53:35 +00:00
|
|
|
SENSOR_TYPES[variable[CONF_TYPE]],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2015-07-09 16:05:19 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class EfergySensor(SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of an Efergy sensor."""
|
2015-07-09 16:05:19 +00:00
|
|
|
|
2021-09-21 21:53:35 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-09-24 10:54:15 +00:00
|
|
|
api: Efergy,
|
2021-09-21 21:53:35 +00:00
|
|
|
period: str,
|
|
|
|
currency: str,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
sid: str = None,
|
|
|
|
) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-09-21 21:53:35 +00:00
|
|
|
self.entity_description = description
|
2017-03-17 06:22:10 +00:00
|
|
|
self.sid = sid
|
2021-09-24 10:54:15 +00:00
|
|
|
self.api = api
|
|
|
|
self.period = period
|
2017-03-17 06:22:10 +00:00
|
|
|
if sid:
|
2021-09-21 21:53:35 +00:00
|
|
|
self._attr_name = f"efergy_{sid}"
|
|
|
|
if description.key == CONF_COST:
|
|
|
|
self._attr_native_unit_of_measurement = f"{currency}/{period}"
|
|
|
|
|
2021-09-24 10:54:15 +00:00
|
|
|
async def async_update(self) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the Efergy monitor data from the web service."""
|
2021-10-09 09:23:40 +00:00
|
|
|
try:
|
|
|
|
self._attr_native_value = await self.api.async_get_reading(
|
|
|
|
self.entity_description.key, period=self.period, sid=self.sid
|
|
|
|
)
|
|
|
|
except (exceptions.DataError, exceptions.ConnectTimeout) as ex:
|
|
|
|
if self._attr_available:
|
|
|
|
self._attr_available = False
|
|
|
|
_LOGGER.error("Error getting data from Efergy: %s", ex)
|
|
|
|
return
|
|
|
|
if not self._attr_available:
|
|
|
|
self._attr_available = True
|
|
|
|
_LOGGER.info("Connection to Efergy has resumed")
|