Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Tankerkoenig sensor integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from aiotankerkoenig import GasType, Station
8 
9 from homeassistant.components.sensor import SensorEntity, SensorStateClass
10 from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CURRENCY_EURO
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import (
15  ATTR_BRAND,
16  ATTR_CITY,
17  ATTR_FUEL_TYPE,
18  ATTR_HOUSE_NUMBER,
19  ATTR_POSTCODE,
20  ATTR_STATION_NAME,
21  ATTR_STREET,
22  ATTRIBUTION,
23 )
24 from .coordinator import TankerkoenigConfigEntry, TankerkoenigDataUpdateCoordinator
25 from .entity import TankerkoenigCoordinatorEntity
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
31  hass: HomeAssistant,
32  entry: TankerkoenigConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up the tankerkoenig sensors."""
36  coordinator = entry.runtime_data
37 
38  entities = []
39  for station in coordinator.stations.values():
40  for fuel in (GasType.E10, GasType.E5, GasType.DIESEL):
41  if getattr(station, fuel) is None:
42  _LOGGER.debug(
43  "Station %s %s (%s) does not offer %s fuel, skipping",
44  station.brand,
45  station.name,
46  station.id,
47  fuel,
48  )
49  continue
50 
51  entities.append(
53  fuel,
54  station,
55  coordinator,
56  )
57  )
58 
59  async_add_entities(entities)
60 
61 
63  """Contains prices for fuel in a given station."""
64 
65  _attr_attribution = ATTRIBUTION
66  _attr_state_class = SensorStateClass.MEASUREMENT
67  _attr_native_unit_of_measurement = CURRENCY_EURO
68  _unrecorded_attributes = frozenset(
69  {
70  ATTR_BRAND,
71  ATTR_CITY,
72  ATTR_HOUSE_NUMBER,
73  ATTR_POSTCODE,
74  ATTR_STATION_NAME,
75  ATTR_STREET,
76  ATTRIBUTION,
77  ATTR_LATITUDE,
78  ATTR_LONGITUDE,
79  }
80  )
81 
82  def __init__(
83  self,
84  fuel_type: GasType,
85  station: Station,
86  coordinator: TankerkoenigDataUpdateCoordinator,
87  ) -> None:
88  """Initialize the sensor."""
89  super().__init__(coordinator, station)
90  self._station_id_station_id = station.id
91  self._fuel_type_fuel_type = fuel_type
92  self._attr_translation_key_attr_translation_key = fuel_type
93  self._attr_unique_id_attr_unique_id = f"{station.id}_{fuel_type}"
94  attrs: dict[str, int | str | float | None] = {
95  ATTR_BRAND: station.brand,
96  ATTR_FUEL_TYPE: fuel_type,
97  ATTR_STATION_NAME: station.name,
98  ATTR_STREET: station.street,
99  ATTR_HOUSE_NUMBER: station.house_number,
100  ATTR_POSTCODE: station.post_code,
101  ATTR_CITY: station.place,
102  }
103 
104  if coordinator.show_on_map:
105  attrs[ATTR_LATITUDE] = station.lat
106  attrs[ATTR_LONGITUDE] = station.lng
107  self._attr_extra_state_attributes_attr_extra_state_attributes = attrs
108 
109  @property
110  def native_value(self) -> float:
111  """Return the current price for the fuel type."""
112  info = self.coordinator.data[self._station_id_station_id]
113  return getattr(info, self._fuel_type_fuel_type)
None __init__(self, GasType fuel_type, Station station, TankerkoenigDataUpdateCoordinator coordinator)
Definition: sensor.py:87
None async_setup_entry(HomeAssistant hass, TankerkoenigConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:34