OVO Energy Long-term Statistics (#54157)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Aidan Timson 2021-08-08 05:20:55 +01:00 committed by GitHub
parent 53c64e5148
commit 11f15f66af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Support for OVO Energy sensors.""" """Support for OVO Energy sensors."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from ovoenergy import OVODailyUsage from ovoenergy import OVODailyUsage
@ -6,8 +8,10 @@ from ovoenergy.ovoenergy import OVOEnergy
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_CLASS_ENERGY, DEVICE_CLASS_MONETARY
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util.dt import utc_from_timestamp
from . import OVOEnergyDeviceEntity from . import OVOEnergyDeviceEntity
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
@ -57,6 +61,9 @@ async def async_setup_entry(
class OVOEnergySensor(OVOEnergyDeviceEntity, SensorEntity): class OVOEnergySensor(OVOEnergyDeviceEntity, SensorEntity):
"""Defines a OVO Energy sensor.""" """Defines a OVO Energy sensor."""
_attr_last_reset = utc_from_timestamp(0)
_attr_state_class = "measurement"
def __init__( def __init__(
self, self,
coordinator: DataUpdateCoordinator, coordinator: DataUpdateCoordinator,
@ -64,15 +71,17 @@ class OVOEnergySensor(OVOEnergyDeviceEntity, SensorEntity):
key: str, key: str,
name: str, name: str,
icon: str, icon: str,
unit_of_measurement: str = "", device_class: str | None,
unit_of_measurement: str | None,
) -> None: ) -> None:
"""Initialize OVO Energy sensor.""" """Initialize OVO Energy sensor."""
self._attr_device_class = device_class
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement
super().__init__(coordinator, client, key, name, icon) super().__init__(coordinator, client, key, name, icon)
@property @property
def unit_of_measurement(self) -> str: def unit_of_measurement(self) -> str | None:
"""Return the unit this state is expressed in.""" """Return the unit this state is expressed in."""
return self._unit_of_measurement return self._unit_of_measurement
@ -89,6 +98,7 @@ class OVOEnergyLastElectricityReading(OVOEnergySensor):
f"{client.account_id}_last_electricity_reading", f"{client.account_id}_last_electricity_reading",
"OVO Last Electricity Reading", "OVO Last Electricity Reading",
"mdi:flash", "mdi:flash",
DEVICE_CLASS_ENERGY,
"kWh", "kWh",
) )
@ -124,6 +134,7 @@ class OVOEnergyLastGasReading(OVOEnergySensor):
f"{DOMAIN}_{client.account_id}_last_gas_reading", f"{DOMAIN}_{client.account_id}_last_gas_reading",
"OVO Last Gas Reading", "OVO Last Gas Reading",
"mdi:gas-cylinder", "mdi:gas-cylinder",
DEVICE_CLASS_ENERGY,
"kWh", "kWh",
) )
@ -160,6 +171,7 @@ class OVOEnergyLastElectricityCost(OVOEnergySensor):
f"{DOMAIN}_{client.account_id}_last_electricity_cost", f"{DOMAIN}_{client.account_id}_last_electricity_cost",
"OVO Last Electricity Cost", "OVO Last Electricity Cost",
"mdi:cash-multiple", "mdi:cash-multiple",
DEVICE_CLASS_MONETARY,
currency, currency,
) )
@ -196,6 +208,7 @@ class OVOEnergyLastGasCost(OVOEnergySensor):
f"{DOMAIN}_{client.account_id}_last_gas_cost", f"{DOMAIN}_{client.account_id}_last_gas_cost",
"OVO Last Gas Cost", "OVO Last Gas Cost",
"mdi:cash-multiple", "mdi:cash-multiple",
DEVICE_CLASS_MONETARY,
currency, currency,
) )