Пример #1
0
    def setup(self) -> bool:
        """Set up the tankerkoenig API."""
        for station_id in self._selected_stations:
            try:
                station_data = pytankerkoenig.getStationData(
                    self._api_key, station_id)
            except pytankerkoenig.customException as err:
                if any(x in str(err).lower() for x in ("api-key", "apikey")):
                    raise ConfigEntryAuthFailed(err) from err
                station_data = {
                    "ok": False,
                    "message": err,
                    "exception": True,
                }

            if not station_data["ok"]:
                _LOGGER.error(
                    "Error when adding station %s:\n %s",
                    station_id,
                    station_data["message"],
                )
                continue
            self.add_station(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
Пример #2
0
    def setup(self, latitude, longitude, radius, additional_stations):
        """Set up the tankerkoenig API.

        Read the initial data from the server, to initialize the list of fuel stations to monitor.
        """
        _LOGGER.debug("Fetching data for (%s, %s) rad: %s", latitude,
                      longitude, radius)
        try:
            data = pytankerkoenig.getNearbyStations(self._api_key, latitude,
                                                    longitude, radius, "all",
                                                    "dist")
        except pytankerkoenig.customException as err:
            data = {"ok": False, "message": err, "exception": True}
        _LOGGER.debug("Received data: %s", data)
        if not data["ok"]:
            _LOGGER.error(
                "Setup for sensors was unsuccessful. Error occurred while fetching data from tankerkoenig.de: %s",
                data["message"],
            )
            return False

        # Add stations found via location + radius
        nearby_stations = data["stations"]
        if not nearby_stations:
            if not additional_stations:
                _LOGGER.error(
                    "Could not find any station in range."
                    "Try with a bigger radius or manually specify stations in additional_stations"
                )
                return False
            _LOGGER.warning(
                "Could not find any station in range. Will only use manually specified stations"
            )
        else:
            for station in data["stations"]:
                self.add_station(station)

        # Add manually specified additional stations
        for station_id in additional_stations:
            try:
                additional_station_data = pytankerkoenig.getStationData(
                    self._api_key, station_id)
            except pytankerkoenig.customException as err:
                additional_station_data = {
                    "ok": False,
                    "message": err,
                    "exception": True,
                }

            if not additional_station_data["ok"]:
                _LOGGER.error(
                    "Error when adding station %s:\n %s",
                    station_id,
                    additional_station_data["message"],
                )
                return False
            self.add_station(additional_station_data["station"])
        return True
Пример #3
0
def update_metrics():
    data = api.getStationData(api_key, station_id)

    price_e5.set(data['station']['e5'])
    price_e10.set(data['station']['e10'])
    price_diesel.set(data['station']['diesel'])
    if data['station']['isOpen']:
        fuel_station_open.state('open')
    else:
        fuel_station_open.state('closed')
Пример #4
0
import pytankerkoenig
import os

print(
    pytankerkoenig.getStationData(os.environ['api_key'],
                                  '5bd71e9d-7001-4908-a29d-36c28d6eb615'))

print(
    pytankerkoenig.getNearbyStations(os.environ['api_key'], 50.75, 7.25, 3,
                                     'all', 'dist'))

print(
    pytankerkoenig.getPriceList(os.environ['api_key'], [
        '5bd71e9d-7001-4908-a29d-36c28d6eb615',
        '005056ba-7cb6-1ed2-bceb-92a737c6ad35'
    ]))
Пример #5
0
                    "Could not find any station in range."
                    "Try with a bigger radius or manually specify stations in additional_stations"
                )
                return False
            _LOGGER.warning(
                "Could not find any station in range. Will only use manually specified stations"
            )
        else:
            for station in nearby_stations:
                self.add_station(station)

        # Add manually specified additional stations
        for station_id in additional_stations:
            try:
                additional_station_data = pytankerkoenig.getStationData(
                    self._api_key, station_id
                )
            except pytankerkoenig.customException as err:
                additional_station_data = {
                    "ok": False,
                    "message": err,
                    "exception": True,
                }

            if not additional_station_data["ok"]:
                _LOGGER.error(
                    "Error when adding station %s:\n %s",
                    station_id,
                    additional_station_data["message"],
                )
                return False