2020-08-05 12:38:29 +00:00
|
|
|
"""Support for OVO Energy sensors."""
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from ovoenergy import OVODailyUsage
|
|
|
|
from ovoenergy.ovoenergy import OVOEnergy
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-08-05 12:38:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-22 18:23:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-08-05 12:38:29 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
|
|
|
|
from . import OVOEnergyDeviceEntity
|
|
|
|
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=300)
|
|
|
|
PARALLEL_UPDATES = 4
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-22 18:23:19 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
|
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:
|
|
|
|
entities.append(OVOEnergyLastElectricityReading(coordinator, client))
|
|
|
|
entities.append(
|
|
|
|
OVOEnergyLastElectricityCost(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
coordinator.data.electricity[
|
|
|
|
len(coordinator.data.electricity) - 1
|
|
|
|
].cost.currency_unit,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if coordinator.data.gas:
|
|
|
|
entities.append(OVOEnergyLastGasReading(coordinator, client))
|
|
|
|
entities.append(
|
|
|
|
OVOEnergyLastGasCost(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
coordinator.data.gas[
|
|
|
|
len(coordinator.data.gas) - 1
|
|
|
|
].cost.currency_unit,
|
|
|
|
)
|
|
|
|
)
|
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):
|
2020-08-05 12:38:29 +00:00
|
|
|
"""Defines a OVO Energy sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
client: OVOEnergy,
|
|
|
|
key: str,
|
|
|
|
name: str,
|
|
|
|
icon: str,
|
|
|
|
unit_of_measurement: str = "",
|
|
|
|
) -> None:
|
|
|
|
"""Initialize OVO Energy sensor."""
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
|
|
|
|
|
|
|
super().__init__(coordinator, client, key, name, icon)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
|
|
|
|
class OVOEnergyLastElectricityReading(OVOEnergySensor):
|
|
|
|
"""Defines a OVO Energy last reading sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator: DataUpdateCoordinator, client: OVOEnergy):
|
|
|
|
"""Initialize OVO Energy sensor."""
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
f"{client.account_id}_last_electricity_reading",
|
|
|
|
"OVO Last Electricity Reading",
|
|
|
|
"mdi:flash",
|
|
|
|
"kWh",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.electricity:
|
|
|
|
return None
|
|
|
|
return usage.electricity[-1].consumption
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self) -> object:
|
2020-08-05 12:38:29 +00:00
|
|
|
"""Return the attributes of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.electricity:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
"start_time": usage.electricity[-1].interval.start,
|
|
|
|
"end_time": usage.electricity[-1].interval.end,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OVOEnergyLastGasReading(OVOEnergySensor):
|
|
|
|
"""Defines a OVO Energy last reading sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator: DataUpdateCoordinator, client: OVOEnergy):
|
|
|
|
"""Initialize OVO Energy sensor."""
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
f"{DOMAIN}_{client.account_id}_last_gas_reading",
|
|
|
|
"OVO Last Gas Reading",
|
|
|
|
"mdi:gas-cylinder",
|
|
|
|
"kWh",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.gas:
|
|
|
|
return None
|
|
|
|
return usage.gas[-1].consumption
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self) -> object:
|
2020-08-05 12:38:29 +00:00
|
|
|
"""Return the attributes of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.gas:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
"start_time": usage.gas[-1].interval.start,
|
|
|
|
"end_time": usage.gas[-1].interval.end,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OVOEnergyLastElectricityCost(OVOEnergySensor):
|
|
|
|
"""Defines a OVO Energy last cost sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: DataUpdateCoordinator, client: OVOEnergy, currency: str
|
|
|
|
):
|
|
|
|
"""Initialize OVO Energy sensor."""
|
|
|
|
super().__init__(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
f"{DOMAIN}_{client.account_id}_last_electricity_cost",
|
|
|
|
"OVO Last Electricity Cost",
|
|
|
|
"mdi:cash-multiple",
|
|
|
|
currency,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.electricity:
|
|
|
|
return None
|
|
|
|
return usage.electricity[-1].cost.amount
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self) -> object:
|
2020-08-05 12:38:29 +00:00
|
|
|
"""Return the attributes of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.electricity:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
"start_time": usage.electricity[-1].interval.start,
|
|
|
|
"end_time": usage.electricity[-1].interval.end,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OVOEnergyLastGasCost(OVOEnergySensor):
|
|
|
|
"""Defines a OVO Energy last cost sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: DataUpdateCoordinator, client: OVOEnergy, currency: str
|
|
|
|
):
|
|
|
|
"""Initialize OVO Energy sensor."""
|
|
|
|
super().__init__(
|
|
|
|
coordinator,
|
|
|
|
client,
|
|
|
|
f"{DOMAIN}_{client.account_id}_last_gas_cost",
|
|
|
|
"OVO Last Gas Cost",
|
|
|
|
"mdi:cash-multiple",
|
|
|
|
currency,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the state of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.gas:
|
|
|
|
return None
|
|
|
|
return usage.gas[-1].cost.amount
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self) -> object:
|
2020-08-05 12:38:29 +00:00
|
|
|
"""Return the attributes of the sensor."""
|
2020-08-30 18:20:45 +00:00
|
|
|
usage: OVODailyUsage = self.coordinator.data
|
2020-08-05 12:38:29 +00:00
|
|
|
if usage is None or not usage.gas:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
"start_time": usage.gas[-1].interval.start,
|
|
|
|
"end_time": usage.gas[-1].interval.end,
|
|
|
|
}
|