core/homeassistant/components/efergy/sensor.py

226 lines
7.1 KiB
Python
Raw Normal View History

"""Support for Efergy sensors."""
from __future__ import annotations
import logging
2021-10-11 08:07:31 +00:00
from re import sub
from pyefergy import Efergy, exceptions
import voluptuous as vol
2021-10-11 08:07:31 +00:00
from homeassistant.components.efergy import EfergyEntity
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
SensorEntityDescription,
)
2021-10-11 08:07:31 +00:00
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
2021-02-08 08:45:05 +00:00
from homeassistant.const import (
CONF_CURRENCY,
CONF_MONITORED_VARIABLES,
CONF_TYPE,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_MONETARY,
DEVICE_CLASS_POWER,
2021-02-08 08:45:05 +00:00
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
)
from homeassistant.core import HomeAssistant
2021-10-11 08:07:31 +00:00
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2021-10-11 08:07:31 +00:00
from .const import CONF_APPTOKEN, CONF_CURRENT_VALUES, DATA_KEY_API, DOMAIN
_LOGGER = logging.getLogger(__name__)
2021-10-11 08:07:31 +00:00
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="instant_readings",
name="Power Usage",
device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT,
),
2021-10-11 08:07:31 +00:00
SensorEntityDescription(
key="energy_day",
name="Daily Consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="energy_week",
name="Weekly Consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
2021-10-11 08:07:31 +00:00
SensorEntityDescription(
key="energy_month",
name="Monthly Consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
),
SensorEntityDescription(
key="energy_year",
name="Yearly Consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="budget",
name="Energy Budget",
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="cost_day",
name="Daily Energy Cost",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="cost_week",
name="Weekly Energy Cost",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="cost_month",
name="Monthly Energy Cost",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
2021-10-11 08:07:31 +00:00
SensorEntityDescription(
key="cost_year",
name="Yearly Energy Cost",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
2021-10-11 08:07:31 +00:00
entity_registry_enabled_default=False,
),
2021-10-11 08:07:31 +00:00
SensorEntityDescription(
key=CONF_CURRENT_VALUES,
2021-10-11 08:07:31 +00:00
name="Power Usage",
device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT,
),
2021-10-11 08:07:31 +00:00
)
TYPES_SCHEMA = vol.In(
["current_values", "instant_readings", "amount", "budget", "cost"]
)
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,
2021-10-11 08:07:31 +00:00
vol.Optional("period", default="year"): cv.string,
2019-07-31 19:25:30 +00:00
}
)
2016-08-23 03:51:17 +00:00
2021-10-11 08:07:31 +00:00
# Deprecated in Home Assistant 2021.11
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_APPTOKEN): cv.string,
2021-10-11 08:07:31 +00:00
vol.Optional("utc_offset", default="0"): cv.string,
2019-07-31 19:25:30 +00:00
vol.Required(CONF_MONITORED_VARIABLES): [SENSORS_SCHEMA],
}
)
2016-08-23 03:51:17 +00:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
2021-10-22 16:20:39 +00:00
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2021-10-11 08:07:31 +00:00
"""Set up the Efergy sensor from yaml."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
2021-10-11 08:07:31 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: entity_platform.AddEntitiesCallback,
) -> None:
"""Set up Efergy sensors."""
api: Efergy = hass.data[DOMAIN][entry.entry_id][DATA_KEY_API]
sensors = []
for description in SENSOR_TYPES:
if description.key != CONF_CURRENT_VALUES:
sensors.append(
EfergySensor(
api,
description,
entry.entry_id,
period=sub("^energy_|^cost_", "", description.key),
currency=hass.config.currency,
)
)
else:
description.entity_registry_enabled_default = len(api.info["sids"]) > 1
for sid in api.info["sids"]:
sensors.append(
2019-07-31 19:25:30 +00:00
EfergySensor(
api,
2021-10-11 08:07:31 +00:00
description,
entry.entry_id,
sid=sid,
2019-07-31 19:25:30 +00:00
)
)
2021-10-11 08:07:31 +00:00
async_add_entities(sensors, True)
2021-10-11 08:07:31 +00:00
class EfergySensor(EfergyEntity, SensorEntity):
2016-03-08 15:46:34 +00:00
"""Implementation of an Efergy sensor."""
def __init__(
self,
api: Efergy,
description: SensorEntityDescription,
2021-10-11 08:07:31 +00:00
server_unique_id: str,
2021-10-22 16:20:39 +00:00
period: str | None = None,
currency: str | None = None,
2021-10-11 08:07:31 +00:00
sid: str = "",
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2021-10-11 08:07:31 +00:00
super().__init__(api, server_unique_id)
self.entity_description = description
2021-10-11 08:07:31 +00:00
if description.key == CONF_CURRENT_VALUES:
self._attr_name = f"{description.name}_{sid}"
self._attr_unique_id = f"{server_unique_id}/{description.key}_{sid}"
if "cost" in description.key:
self._attr_native_unit_of_measurement = currency
self.sid = sid
self.period = period
async def async_update(self) -> None:
2016-03-08 15:46:34 +00:00
"""Get the Efergy monitor data from the web service."""
try:
self._attr_native_value = await self.api.async_get_reading(
self.entity_description.key, period=self.period, sid=self.sid
)
2021-10-11 08:07:31 +00:00
except (exceptions.DataError, exceptions.ConnectError) as ex:
if self._attr_available:
self._attr_available = False
2021-10-11 08:07:31 +00:00
_LOGGER.error("Error getting data: %s", ex)
return
if not self._attr_available:
self._attr_available = True
2021-10-11 08:07:31 +00:00
_LOGGER.info("Connection has resumed")