Refactor volvooncall to (mostly) use DataUpdateCoordinator (#75885)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
02ad4843b8
commit
b5a6ee3c56
6 changed files with 209 additions and 136 deletions
|
@ -2,11 +2,11 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import DATA_KEY, VolvoEntity
|
||||
from . import DATA_KEY, VolvoEntity, VolvoUpdateCoordinator
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
|
@ -24,12 +24,24 @@ async def async_setup_platform(
|
|||
class VolvoSensor(VolvoEntity, SensorEntity):
|
||||
"""Representation of a Volvo sensor."""
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state."""
|
||||
return self.instrument.state
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: VolvoUpdateCoordinator,
|
||||
vin: str,
|
||||
component: str,
|
||||
attribute: str,
|
||||
slug_attr: str,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(vin, component, attribute, slug_attr, coordinator)
|
||||
self._update_value_and_unit()
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return self.instrument.unit
|
||||
def _update_value_and_unit(self) -> None:
|
||||
self._attr_native_value = self.instrument.state
|
||||
self._attr_native_unit_of_measurement = self.instrument.unit
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._update_value_and_unit()
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue