mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Update tankerkoenig to use CoordinatorEntity (#39440)
This commit is contained in:
parent
cceaa088cb
commit
d9c9adbc91
@ -3,8 +3,11 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE
|
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import (
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
UpdateFailed,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import DOMAIN, NAME
|
from .const import DOMAIN, NAME
|
||||||
|
|
||||||
@ -71,15 +74,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class FuelPriceSensor(Entity):
|
class FuelPriceSensor(CoordinatorEntity):
|
||||||
"""Contains prices for fuel in a given station."""
|
"""Contains prices for fuel in a given station."""
|
||||||
|
|
||||||
def __init__(self, fuel_type, station, coordinator, name, show_on_map):
|
def __init__(self, fuel_type, station, coordinator, name, show_on_map):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self._station = station
|
self._station = station
|
||||||
self._station_id = station["id"]
|
self._station_id = station["id"]
|
||||||
self._fuel_type = fuel_type
|
self._fuel_type = fuel_type
|
||||||
self._coordinator = coordinator
|
|
||||||
self._name = name
|
self._name = name
|
||||||
self._latitude = station["lat"]
|
self._latitude = station["lat"]
|
||||||
self._longitude = station["lng"]
|
self._longitude = station["lng"]
|
||||||
@ -105,16 +108,11 @@ class FuelPriceSensor(Entity):
|
|||||||
"""Return unit of measurement."""
|
"""Return unit of measurement."""
|
||||||
return "€"
|
return "€"
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No need to poll. Coordinator notifies of updates."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
# key Fuel_type is not available when the fuel station is closed, use "get" instead of "[]" to avoid exceptions
|
# key Fuel_type is not available when the fuel station is closed, use "get" instead of "[]" to avoid exceptions
|
||||||
return self._coordinator.data[self._station_id].get(self._fuel_type)
|
return self.coordinator.data[self._station_id].get(self._fuel_type)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
@ -124,7 +122,7 @@ class FuelPriceSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the attributes of the device."""
|
"""Return the attributes of the device."""
|
||||||
data = self._coordinator.data[self._station_id]
|
data = self.coordinator.data[self._station_id]
|
||||||
|
|
||||||
attrs = {
|
attrs = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
@ -144,20 +142,3 @@ class FuelPriceSensor(Entity):
|
|||||||
if data is not None and "status" in data:
|
if data is not None and "status" in data:
|
||||||
attrs[ATTR_IS_OPEN] = data["status"] == "open"
|
attrs[ATTR_IS_OPEN] = data["status"] == "open"
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return if entity is available."""
|
|
||||||
return self._coordinator.last_update_success
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""When entity is added to hass."""
|
|
||||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
|
||||||
"""When entity will be removed from hass."""
|
|
||||||
self._coordinator.async_remove_listener(self.async_write_ha_state)
|
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update the entity."""
|
|
||||||
await self._coordinator.async_request_refresh()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user