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
|
2020-08-05 12:38:29 +00:00
|
|
|
from datetime import 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 (
|
|
|
|
STATE_CLASS_TOTAL_INCREASING,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2020-08-05 12:38:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-08-24 10:53:32 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_ENERGY,
|
|
|
|
DEVICE_CLASS_MONETARY,
|
|
|
|
DEVICE_CLASS_TIMESTAMP,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
)
|
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."""
|
|
|
|
|
|
|
|
value: Callable[[OVODailyUsage], StateType] = round
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES_ELECTRICITY: tuple[OVOEnergySensorEntityDescription, ...] = (
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key="last_electricity_reading",
|
|
|
|
name="OVO Last Electricity Reading",
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
value=lambda usage: usage.electricity[-1].consumption,
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key=KEY_LAST_ELECTRICITY_COST,
|
|
|
|
name="OVO Last Electricity Cost",
|
|
|
|
device_class=DEVICE_CLASS_MONETARY,
|
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
2021-09-30 10:31:06 +00:00
|
|
|
value=lambda usage: usage.electricity[-1].cost.amount,
|
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,
|
|
|
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
|
|
|
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,
|
|
|
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
|
|
|
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",
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
|
|
|
icon="mdi:gas-cylinder",
|
|
|
|
value=lambda usage: usage.gas[-1].consumption,
|
|
|
|
),
|
|
|
|
OVOEnergySensorEntityDescription(
|
|
|
|
key=KEY_LAST_GAS_COST,
|
|
|
|
name="OVO Last Gas Cost",
|
|
|
|
device_class=DEVICE_CLASS_MONETARY,
|
|
|
|
state_class=STATE_CLASS_TOTAL_INCREASING,
|
|
|
|
icon="mdi:cash-multiple",
|
2021-09-30 10:31:06 +00:00
|
|
|
value=lambda usage: usage.gas[-1].cost.amount,
|
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,
|
|
|
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
|
|
|
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,
|
|
|
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
|
|
|
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."""
|
|
|
|
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
DATA_COORDINATOR
|
|
|
|
]
|
|
|
|
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:
|
|
|
|
if description.key == KEY_LAST_ELECTRICITY_COST:
|
|
|
|
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:
|
|
|
|
if description.key == KEY_LAST_GAS_COST:
|
|
|
|
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
|
|
|
|
2021-08-24 10:53:32 +00:00
|
|
|
coordinator: DataUpdateCoordinator
|
|
|
|
entity_description: OVOEnergySensorEntityDescription
|
2021-08-08 04:20:55 +00:00
|
|
|
|
2020-08-05 12:38:29 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
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."""
|
2020-08-05 12:38:29 +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
|
2021-08-24 10:53:32 +00:00
|
|
|
def native_value(self) -> StateType:
|
|
|
|
"""Return the state."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2021-08-24 10:53:32 +00:00
|
|
|
return self.entity_description.value(usage)
|