Simplify native value property for WattTime (#56664)

This commit is contained in:
Aaron Bach 2021-09-27 03:58:51 -06:00 committed by GitHub
parent 4f5d6b8ba1
commit 56b94d6809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,8 +17,9 @@ from homeassistant.const import (
MASS_POUNDS, MASS_POUNDS,
PERCENTAGE, PERCENTAGE,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
@ -115,20 +116,7 @@ class RealtimeEmissionsSensor(CoordinatorEntity, SensorEntity):
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}" self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}"
self.entity_description = description self.entity_description = description
@callback @property
def _handle_coordinator_update(self) -> None: def native_value(self) -> StateType:
"""Respond to a DataUpdateCoordinator update.""" """Return the value reported by the sensor."""
self.update_from_latest_data() return self.coordinator.data[self.entity_description.data_key]
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
self.update_from_latest_data()
@callback
def update_from_latest_data(self) -> None:
"""Update the state."""
self._attr_native_value = self.coordinator.data[
self.entity_description.data_key
]