1 """The Tankerkoenig update coordinator."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from aiotankerkoenig
import (
13 TankerkoenigConnectionError,
15 TankerkoenigInvalidKeyError,
16 TankerkoenigRateLimitError,
27 from .const
import CONF_FUEL_TYPES, CONF_STATIONS
29 _LOGGER = logging.getLogger(__name__)
31 type TankerkoenigConfigEntry = ConfigEntry[TankerkoenigDataUpdateCoordinator]
35 """Get the latest data from the API."""
37 config_entry: TankerkoenigConfigEntry
45 """Initialize the data object."""
51 update_interval=
timedelta(minutes=update_interval),
54 self._selected_stations: list[str] = self.
config_entryconfig_entry.data[CONF_STATIONS]
55 self.stations: dict[str, Station] = {}
56 self.fuel_types: list[str] = self.
config_entryconfig_entry.data[CONF_FUEL_TYPES]
57 self.show_on_map: bool = self.
config_entryconfig_entry.options[CONF_SHOW_ON_MAP]
60 api_key=self.
config_entryconfig_entry.data[CONF_API_KEY],
65 """Set up the tankerkoenig API."""
66 for station_id
in self._selected_stations:
68 station = await self.
_tankerkoenig_tankerkoenig.station_details(station_id)
69 except TankerkoenigInvalidKeyError
as err:
71 "invalid key error occur during setup of station %s %s",
76 except TankerkoenigConnectionError
as err:
78 "connection error occur during setup of station %s %s",
83 except TankerkoenigError
as err:
84 _LOGGER.error(
"Error when adding station %s %s", station_id, err)
87 self.stations[station_id] = station
89 entity_reg = er.async_get(self.
hasshass)
90 for entity
in er.async_entries_for_config_entry(
93 if entity.unique_id.split(
"_")[0]
not in self._selected_stations:
94 _LOGGER.debug(
"Removing obsolete entity entry %s", entity.entity_id)
95 entity_reg.async_remove(entity.entity_id)
97 device_reg = dr.async_get(self.
hasshass)
98 for device
in dr.async_entries_for_config_entry(
102 (ATTR_ID, station_id)
in device.identifiers
103 for station_id
in self._selected_stations
105 _LOGGER.debug(
"Removing obsolete device entry %s", device.name)
106 device_reg.async_update_device(
107 device.id, remove_config_entry_id=self.
config_entryconfig_entry.entry_id
110 if len(self.stations) > 10:
112 "Found more than 10 stations to check. "
113 "This might invalidate your api-key on the long run. "
114 "Try using a smaller radius"
118 """Get the latest data from tankerkoenig.de."""
119 station_ids =
list(self.stations)
124 for index
in range(ceil(len(station_ids) / 10)):
125 stations = station_ids[index * 10 : (index + 1) * 10]
127 data = await self.
_tankerkoenig_tankerkoenig.prices(stations)
128 except TankerkoenigInvalidKeyError
as err:
130 "invalid key error occur during update of stations %s %s",
135 except TankerkoenigRateLimitError
as err:
137 "API rate limit reached, consider to increase polling interval"
140 except (TankerkoenigError, TankerkoenigConnectionError)
as err:
142 "error occur during update of stations %s %s",
dict[str, PriceInfo] _async_update_data(self)
None __init__(self, HomeAssistant hass, str name, int update_interval)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)