mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Update ovo_energy to use CoordinatorEntity (#39459)
This commit is contained in:
parent
1fbb158211
commit
ab0b0dc51c
@ -11,9 +11,11 @@ from ovoenergy.ovoenergy import OVOEnergy
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
||||||
|
|
||||||
@ -86,7 +88,7 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigType) -> bool
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class OVOEnergyEntity(Entity):
|
class OVOEnergyEntity(CoordinatorEntity):
|
||||||
"""Defines a base OVO Energy entity."""
|
"""Defines a base OVO Energy entity."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -98,7 +100,7 @@ class OVOEnergyEntity(Entity):
|
|||||||
icon: str,
|
icon: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the OVO Energy entity."""
|
"""Initialize the OVO Energy entity."""
|
||||||
self._coordinator = coordinator
|
super().__init__(coordinator)
|
||||||
self._client = client
|
self._client = client
|
||||||
self._key = key
|
self._key = key
|
||||||
self._name = name
|
self._name = name
|
||||||
@ -123,22 +125,7 @@ class OVOEnergyEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return self._coordinator.last_update_success and self._available
|
return self.coordinator.last_update_success and self._available
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No need to poll. Coordinator notifies entity of updates."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
|
||||||
"""Update OVO Energy entity."""
|
|
||||||
await self._coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
|
||||||
"""Connect to dispatcher listening for entity data notifications."""
|
|
||||||
self.async_on_remove(
|
|
||||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class OVOEnergyDeviceEntity(OVOEnergyEntity):
|
class OVOEnergyDeviceEntity(OVOEnergyEntity):
|
||||||
|
@ -100,7 +100,7 @@ class OVOEnergyLastElectricityReading(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.electricity:
|
if usage is None or not usage.electricity:
|
||||||
return None
|
return None
|
||||||
return usage.electricity[-1].consumption
|
return usage.electricity[-1].consumption
|
||||||
@ -108,7 +108,7 @@ class OVOEnergyLastElectricityReading(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self) -> object:
|
def device_state_attributes(self) -> object:
|
||||||
"""Return the attributes of the sensor."""
|
"""Return the attributes of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.electricity:
|
if usage is None or not usage.electricity:
|
||||||
return None
|
return None
|
||||||
return {
|
return {
|
||||||
@ -135,7 +135,7 @@ class OVOEnergyLastGasReading(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.gas:
|
if usage is None or not usage.gas:
|
||||||
return None
|
return None
|
||||||
return usage.gas[-1].consumption
|
return usage.gas[-1].consumption
|
||||||
@ -143,7 +143,7 @@ class OVOEnergyLastGasReading(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self) -> object:
|
def device_state_attributes(self) -> object:
|
||||||
"""Return the attributes of the sensor."""
|
"""Return the attributes of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.gas:
|
if usage is None or not usage.gas:
|
||||||
return None
|
return None
|
||||||
return {
|
return {
|
||||||
@ -171,7 +171,7 @@ class OVOEnergyLastElectricityCost(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.electricity:
|
if usage is None or not usage.electricity:
|
||||||
return None
|
return None
|
||||||
return usage.electricity[-1].cost.amount
|
return usage.electricity[-1].cost.amount
|
||||||
@ -179,7 +179,7 @@ class OVOEnergyLastElectricityCost(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self) -> object:
|
def device_state_attributes(self) -> object:
|
||||||
"""Return the attributes of the sensor."""
|
"""Return the attributes of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.electricity:
|
if usage is None or not usage.electricity:
|
||||||
return None
|
return None
|
||||||
return {
|
return {
|
||||||
@ -207,7 +207,7 @@ class OVOEnergyLastGasCost(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.gas:
|
if usage is None or not usage.gas:
|
||||||
return None
|
return None
|
||||||
return usage.gas[-1].cost.amount
|
return usage.gas[-1].cost.amount
|
||||||
@ -215,7 +215,7 @@ class OVOEnergyLastGasCost(OVOEnergySensor):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self) -> object:
|
def device_state_attributes(self) -> object:
|
||||||
"""Return the attributes of the sensor."""
|
"""Return the attributes of the sensor."""
|
||||||
usage: OVODailyUsage = self._coordinator.data
|
usage: OVODailyUsage = self.coordinator.data
|
||||||
if usage is None or not usage.gas:
|
if usage is None or not usage.gas:
|
||||||
return None
|
return None
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user