Fix tankerkoenig with more than 10 stations (#33098)

* Fix tankerkoenig with more than 10 stations

There seemed to be a problem, if more than 10 fuel stations were tracked.
Added a warning in this case, and split the calls to the API in chunks of
10, so that the data can be fetched anyway.

* Update homeassistant/components/tankerkoenig/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
guillempages 2020-03-21 23:12:14 +01:00 committed by GitHub
parent 403b4a2e0b
commit 79f6d55fe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
"""Ask tankerkoenig.de for petrol price information.""" """Ask tankerkoenig.de for petrol price information."""
from datetime import timedelta from datetime import timedelta
import logging import logging
from math import ceil
from uuid import UUID from uuid import UUID
import pytankerkoenig import pytankerkoenig
@ -180,27 +181,41 @@ class TankerkoenigData:
) )
return False return False
self.add_station(additional_station_data["station"]) self.add_station(additional_station_data["station"])
if len(self.stations) > 10:
_LOGGER.warning(
"Found more than 10 stations to check. "
"This might invalidate your api-key on the long run. "
"Try using a smaller radius"
)
return True return True
async def fetch_data(self): async def fetch_data(self):
"""Get the latest data from tankerkoenig.de.""" """Get the latest data from tankerkoenig.de."""
_LOGGER.debug("Fetching new data from tankerkoenig.de") _LOGGER.debug("Fetching new data from tankerkoenig.de")
station_ids = list(self.stations) station_ids = list(self.stations)
data = await self._hass.async_add_executor_job(
pytankerkoenig.getPriceList, self._api_key, station_ids
)
if data["ok"]: prices = {}
# The API seems to only return at most 10 results, so split the list in chunks of 10
# and merge it together.
for index in range(ceil(len(station_ids) / 10)):
data = await self._hass.async_add_executor_job(
pytankerkoenig.getPriceList,
self._api_key,
station_ids[index * 10 : (index + 1) * 10],
)
_LOGGER.debug("Received data: %s", data) _LOGGER.debug("Received data: %s", data)
if not data["ok"]:
_LOGGER.error(
"Error fetching data from tankerkoenig.de: %s", data["message"]
)
raise TankerkoenigError(data["message"])
if "prices" not in data: if "prices" not in data:
_LOGGER.error("Did not receive price information from tankerkoenig.de") _LOGGER.error("Did not receive price information from tankerkoenig.de")
raise TankerkoenigError("No prices in data") raise TankerkoenigError("No prices in data")
else: prices.update(data["prices"])
_LOGGER.error( return prices
"Error fetching data from tankerkoenig.de: %s", data["message"]
)
raise TankerkoenigError(data["message"])
return data["prices"]
def add_station(self, station: dict): def add_station(self, station: dict):
"""Add fuel station to the entity list.""" """Add fuel station to the entity list."""