Update ovo_energy to use CoordinatorEntity (#39459)

This commit is contained in:
J. Nick Koston 2020-08-30 13:20:45 -05:00 committed by GitHub
parent 1fbb158211
commit ab0b0dc51c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 28 deletions

View File

@ -11,9 +11,11 @@ from ovoenergy.ovoenergy import OVOEnergy
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import Entity
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
@ -86,7 +88,7 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigType) -> bool
return True
class OVOEnergyEntity(Entity):
class OVOEnergyEntity(CoordinatorEntity):
"""Defines a base OVO Energy entity."""
def __init__(
@ -98,7 +100,7 @@ class OVOEnergyEntity(Entity):
icon: str,
) -> None:
"""Initialize the OVO Energy entity."""
self._coordinator = coordinator
super().__init__(coordinator)
self._client = client
self._key = key
self._name = name
@ -123,22 +125,7 @@ class OVOEnergyEntity(Entity):
@property
def available(self) -> bool:
"""Return True if entity is 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)
)
return self.coordinator.last_update_success and self._available
class OVOEnergyDeviceEntity(OVOEnergyEntity):

View File

@ -100,7 +100,7 @@ class OVOEnergyLastElectricityReading(OVOEnergySensor):
@property
def state(self) -> str:
"""Return the state of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.electricity:
return None
return usage.electricity[-1].consumption
@ -108,7 +108,7 @@ class OVOEnergyLastElectricityReading(OVOEnergySensor):
@property
def device_state_attributes(self) -> object:
"""Return the attributes of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.electricity:
return None
return {
@ -135,7 +135,7 @@ class OVOEnergyLastGasReading(OVOEnergySensor):
@property
def state(self) -> str:
"""Return the state of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.gas:
return None
return usage.gas[-1].consumption
@ -143,7 +143,7 @@ class OVOEnergyLastGasReading(OVOEnergySensor):
@property
def device_state_attributes(self) -> object:
"""Return the attributes of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.gas:
return None
return {
@ -171,7 +171,7 @@ class OVOEnergyLastElectricityCost(OVOEnergySensor):
@property
def state(self) -> str:
"""Return the state of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.electricity:
return None
return usage.electricity[-1].cost.amount
@ -179,7 +179,7 @@ class OVOEnergyLastElectricityCost(OVOEnergySensor):
@property
def device_state_attributes(self) -> object:
"""Return the attributes of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.electricity:
return None
return {
@ -207,7 +207,7 @@ class OVOEnergyLastGasCost(OVOEnergySensor):
@property
def state(self) -> str:
"""Return the state of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.gas:
return None
return usage.gas[-1].cost.amount
@ -215,7 +215,7 @@ class OVOEnergyLastGasCost(OVOEnergySensor):
@property
def device_state_attributes(self) -> object:
"""Return the attributes of the sensor."""
usage: OVODailyUsage = self._coordinator.data
usage: OVODailyUsage = self.coordinator.data
if usage is None or not usage.gas:
return None
return {