core/homeassistant/components/tankerkoenig/sensor.py

146 lines
4.6 KiB
Python
Raw Normal View History

Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Tankerkoenig sensor integration."""
from __future__ import annotations
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
import logging
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_ID,
ATTR_LATITUDE,
ATTR_LONGITUDE,
CURRENCY_EURO,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
from . import TankerkoenigDataUpdateCoordinator
from .const import DOMAIN, FUEL_TYPES
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
_LOGGER = logging.getLogger(__name__)
ATTR_BRAND = "brand"
ATTR_CITY = "city"
ATTR_FUEL_TYPE = "fuel_type"
ATTR_HOUSE_NUMBER = "house_number"
ATTR_IS_OPEN = "is_open"
ATTR_POSTCODE = "postcode"
ATTR_STATION_NAME = "station_name"
ATTR_STREET = "street"
ATTRIBUTION = "Data provided by https://creativecommons.tankerkoenig.de"
ICON = "mdi:gas-station"
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Set up the tankerkoenig sensors."""
coordinator: TankerkoenigDataUpdateCoordinator = hass.data[DOMAIN][entry.unique_id]
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
stations = coordinator.stations.values()
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
entities = []
for station in stations:
for fuel in coordinator.fuel_types:
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
if fuel not in station:
_LOGGER.warning(
"Station %s does not offer %s fuel", station["id"], fuel
)
continue
sensor = FuelPriceSensor(
fuel,
station,
coordinator,
coordinator.show_on_map,
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
)
entities.append(sensor)
_LOGGER.debug("Added sensors %s", entities)
async_add_entities(entities)
class FuelPriceSensor(CoordinatorEntity, SensorEntity):
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Contains prices for fuel in a given station."""
_attr_state_class = STATE_CLASS_MEASUREMENT
def __init__(self, fuel_type, station, coordinator, show_on_map):
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Initialize the sensor."""
super().__init__(coordinator)
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
self._station = station
self._station_id = station["id"]
self._fuel_type = fuel_type
self._latitude = station["lat"]
self._longitude = station["lng"]
self._city = station["place"]
self._house_number = station["houseNumber"]
self._postcode = station["postCode"]
self._street = station["street"]
self._brand = self._station["brand"]
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
self._price = station[fuel_type]
self._show_on_map = show_on_map
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
@property
def name(self):
"""Return the name of the sensor."""
return f"{self._brand} {self._street} {self._house_number} {FUEL_TYPES[self._fuel_type]}"
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
@property
def icon(self):
"""Icon to use in the frontend."""
return ICON
@property
def native_unit_of_measurement(self):
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Return unit of measurement."""
return CURRENCY_EURO
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
@property
def native_value(self):
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""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
return self.coordinator.data[self._station_id].get(self._fuel_type)
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
@property
def unique_id(self) -> str:
"""Return a unique identifier for this entity."""
return f"{self._station_id}_{self._fuel_type}"
@property
def device_info(self) -> DeviceInfo | None:
"""Return device info."""
return DeviceInfo(
identifiers={(ATTR_ID, self._station_id)},
name=f"{self._brand} {self._street} {self._house_number}",
model=self._brand,
configuration_url="https://www.tankerkoenig.de",
)
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
@property
def extra_state_attributes(self):
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
"""Return the attributes of the device."""
data = self.coordinator.data[self._station_id]
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
attrs = {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_BRAND: self._station["brand"],
ATTR_FUEL_TYPE: self._fuel_type,
ATTR_STATION_NAME: self._station["name"],
ATTR_STREET: self._street,
ATTR_HOUSE_NUMBER: self._house_number,
ATTR_POSTCODE: self._postcode,
ATTR_CITY: self._city,
}
if self._show_on_map:
attrs[ATTR_LATITUDE] = self._latitude
attrs[ATTR_LONGITUDE] = self._longitude
Add Tankerkoenig integration (#28661) * Initial version Parse configuration, but return a fixed value * Add basic functionality Request real data from the server Currently the prices are not getting updated, but the petrol station data is real * Update values regularly The tankerkoenig values get updated regularly with real data. * Move base functionality for the sensor to a base class And move that to an own file, so that it can be inherited * Reduce calls to tankerkoenig api Use a master/slave concept for sensors; one master gets the data and updates it into the slaves. * Update requirements files * Update all gas stations at once * Remove tests directory Currently there are no tests for the integration; will be added in a future commit. * Fix slaves not being updated Let the base class regularly poll, so that slaves are also updated * Refactor entity creation Create an auxiliary method to add a station to the entity list, in preparation to allowing extra stations. * Add possibility to manually add stations Add a new configuration option "stations" to manually add extra stations * Fix style issues Make the code more pythonic * Remove redundant code Implement suggestions from the code review * Change to platform component Remove the master/slave concept, in favor of a platform with dummy sensors. The platform takes care of contacting the server and fetching updates atomically, and updating the data on the sensors. * Rename ATTR_STATE Rename the attribute to "IS_OPEN", to avoid confusion with the sensor state. * Minor updates Combine two consecutive error logs into a single one. Update the sensor's icon * Separate address into different fields * Style updates Use "[]" syntax instead of ".get()" for required parameters Use warning log level for not available fuel types * Implement review comments Fix style issues Improve error messages Remove redundant options * Refactor using DataUpdateCoordinator Use the new DataUpdateCoordinator to fetch the global data from the API, instead of implementing an own method. Also fix comments from the PR * Implement PR comments Implement suggestions to improve code readability and keep the Home Assistant style. Also separate fetching data to an async thread
2020-03-01 15:49:07 +00:00
if data is not None and "status" in data:
attrs[ATTR_IS_OPEN] = data["status"] == "open"
return attrs