2020-08-05 12:38:29 +00:00
|
|
|
"""Support for OVO Energy sensors."""
|
2021-08-08 04:20:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 12:06:51 +00:00
|
|
|
from collections.abc import Callable
|
2021-08-24 10:53:32 +00:00
|
|
|
from dataclasses import dataclass
|
2022-01-19 12:29:24 +00:00
|
|
|
from datetime import datetime, timedelta
|
2021-09-29 12:06:51 +00:00
|
|
|
from typing import Final
|
2020-08-05 12:38:29 +00:00
|
|
|
|
|
|
|
from ovoenergy import OVODailyUsage
|
|
|
|
from ovoenergy.ovoenergy import OVOEnergy
|
|
|
|
|
2021-08-24 10:53:32 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 14:30:59 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-24 10:53:32 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-16 14:30:59 +00:00
|
|
|
SensorStateClass,
|
2021-08-24 10:53:32 +00:00
|
|
|
)
|
2020-08-05 12:38:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-19 09:58:37 +00:00
|
|
|
from homeassistant.const import UnitOfEnergy
|
2021-04-22 18:23:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-08-24 10:53:32 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-05 12:38:29 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2021-08-24 10:53:32 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2020-08-05 12:38:29 +00:00
|
|
|
|
|
|
|
from . import OVOEnergyDeviceEntity
|
|
|
|
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=300)
|
|
|
|
PARALLEL_UPDATES = 4
|
|
|
|
|
2021-08-24 10:53:32 +00:00
|
|
|
KEY_LAST_ELECTRICITY_COST: Final = "last_electricity_cost"
|
|
|
|
KEY_LAST_GAS_COST: Final = "last_gas_cost"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class OVOEnergySensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Class describing System Bridge sensor entities."""
|
|
|
|
|
2022-01-19 12:29:24 +00:00
|
|
|
value: Callable[[OVODailyUsage], StateType | datetime] = round
|
2021-08-24 10:53:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES_ELECTRICITY: tuple[OVOEnergySensorEntityDescription, ...] = (
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_electricity_reading",
|
|
|
|
name="OVO Last Electricity Reading",
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2022-12-19 09:58:37 +00:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
2021-08-24 10:53:32 +00:00
|
|
|
value=lambda usage: usage.electricity[-1].consumption,
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key=KEY_LAST_ELECTRICITY_COST,
|
|
|
|
name="OVO Last Electricity Cost",
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.MONETARY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2022-02-13 14:37:18 +00:00
|
|
|
value=lambda usage: usage.electricity[-1].cost.amount
|
|
|
|
if usage.electricity[-1].cost is not None
|
|
|
|
else None,
|
2021-08-24 10:53:32 +00:00
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_electricity_start_time",
|
|
|
|
name="OVO Last Electricity Start Time",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-08-24 10:53:32 +00:00
|
|
|
value=lambda usage: dt_util.as_utc(usage.electricity[-1].interval.start),
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_electricity_end_time",
|
|
|
|
name="OVO Last Electricity End Time",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-08-24 10:53:32 +00:00
|
|
|
value=lambda usage: dt_util.as_utc(usage.electricity[-1].interval.end),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_TYPES_GAS: tuple[OVOEnergySensorEntityDescription, ...] = (
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_gas_reading",
|
|
|
|
name="OVO Last Gas Reading",
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2022-12-19 09:58:37 +00:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
2021-08-24 10:53:32 +00:00
|
|
|
icon="mdi:gas-cylinder",
|
|
|
|
value=lambda usage: usage.gas[-1].consumption,
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key=KEY_LAST_GAS_COST,
|
|
|
|
name="OVO Last Gas Cost",
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.MONETARY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-08-24 10:53:32 +00:00
|
|
|
icon="mdi:cash-multiple",
|
2022-02-13 14:37:18 +00:00
|
|
|
value=lambda usage: usage.gas[-1].cost.amount
|
|
|
|
if usage.gas[-1].cost is not None
|
|
|
|
else None,
|
2021-08-24 10:53:32 +00:00
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_gas_start_time",
|
|
|
|
name="OVO Last Gas Start Time",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-08-24 10:53:32 +00:00
|
|
|
value=lambda usage: dt_util.as_utc(usage.gas[-1].interval.start),
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_gas_end_time",
|
|
|
|
name="OVO Last Gas End Time",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-16 14:30:59 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-08-24 10:53:32 +00:00
|
|
|
value=lambda usage: dt_util.as_utc(usage.gas[-1].interval.end),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-08-05 12:38:29 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-08-24 10:53:32 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-08-05 12:38:29 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up OVO Energy sensor based on a config entry."""
|
2022-12-27 20:17:35 +00:00
|
|
|
coordinator: DataUpdateCoordinator[OVODailyUsage] = hass.data[DOMAIN][
|
|
|
|
entry.entry_id
|
|
|
|
][DATA_COORDINATOR]
|
2020-08-05 12:38:29 +00:00
|
|
|
client: OVOEnergy = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
|
|
|
|
|
2020-08-13 09:47:32 +00:00
|
|
|
entities = []
|
|
|
|
|
2020-08-14 12:06:31 +00:00
|
|
|
if coordinator.data:
|
|
|
|
if coordinator.data.electricity:
|
2021-08-24 10:53:32 +00:00
|
|
|
for description in SENSOR_TYPES_ELECTRICITY:
|
2022-02-05 10:46:52 +00:00
|
|
|
if (
|
|
|
|
description.key == KEY_LAST_ELECTRICITY_COST
|
|
|
|
and coordinator.data.electricity[-1] is not None
|
|
|
|
and coordinator.data.electricity[-1].cost is not None
|
|
|
|
):
|
2021-08-24 10:53:32 +00:00
|
|
|
description.native_unit_of_measurement = (
|
|
|
|
coordinator.data.electricity[-1].cost.currency_unit
|
|
|
|
)
|
|
|
|
entities.append(OVOEnergySensor(coordinator, description, client))
|
2020-08-14 12:06:31 +00:00
|
|
|
if coordinator.data.gas:
|
2021-08-24 10:53:32 +00:00
|
|
|
for description in SENSOR_TYPES_GAS:
|
2022-02-05 10:46:52 +00:00
|
|
|
if (
|
|
|
|
description.key == KEY_LAST_GAS_COST
|
|
|
|
and coordinator.data.gas[-1] is not None
|
|
|
|
and coordinator.data.gas[-1].cost is not None
|
|
|
|
):
|
2021-08-24 10:53:32 +00:00
|
|
|
description.native_unit_of_measurement = coordinator.data.gas[
|
|
|
|
-1
|
|
|
|
].cost.currency_unit
|
|
|
|
entities.append(OVOEnergySensor(coordinator, description, client))
|
2020-08-05 12:38:29 +00:00
|
|
|
|
2020-11-21 10:21:23 +00:00
|
|
|
async_add_entities(entities, True)
|
2020-08-05 12:38:29 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class OVOEnergySensor(OVOEnergyDeviceEntity, SensorEntity):
|
2021-08-24 10:53:32 +00:00
|
|
|
"""Define a OVO Energy sensor."""
|
2020-08-05 12:38:29 +00:00
|
|
|
|
2022-12-27 20:17:35 +00:00
|
|
|
coordinator: DataUpdateCoordinator[DataUpdateCoordinator[OVODailyUsage]]
|
2021-08-24 10:53:32 +00:00
|
|
|
entity_description: OVOEnergySensorEntityDescription
|
2021-08-08 04:20:55 +00:00
|
|
|
|
2020-08-05 12:38:29 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-12-27 20:17:35 +00:00
|
|
|
coordinator: DataUpdateCoordinator[OVODailyUsage],
|
2021-08-24 10:53:32 +00:00
|
|
|
description: OVOEnergySensorEntityDescription,
|
2020-08-05 12:38:29 +00:00
|
|
|
client: OVOEnergy,
|
|
|
|
) -> None:
|
2021-08-24 10:53:32 +00:00
|
|
|
"""Initialize."""
|
2022-12-27 20:17:35 +00:00
|
|
|
super().__init__(coordinator, client)
|
2021-08-29 03:31:07 +00:00
|
|
|
self._attr_unique_id = f"{DOMAIN}_{client.account_id}_{description.key}"
|
2021-08-24 10:53:32 +00:00
|
|
|
self.entity_description = description
|
2020-08-05 12:38:29 +00:00
|
|
|
|
|
|
|
@property
|
2022-01-19 12:29:24 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2021-08-24 10:53:32 +00:00
|
|
|
"""Return the state."""
|
2022-12-27 20:17:35 +00:00
|
|
|
usage = self.coordinator.data
|
2021-08-24 10:53:32 +00:00
|
|
|
return self.entity_description.value(usage)
|