2020-03-01 15:49:07 +00:00
|
|
|
"""Tankerkoenig sensor integration."""
|
2022-01-03 18:20:39 +00:00
|
|
|
from __future__ import annotations
|
2020-03-01 15:49:07 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2022-03-28 07:48:25 +00:00
|
|
|
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
2022-03-30 03:23:30 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-15 21:01:01 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
2022-03-30 03:23:30 +00:00
|
|
|
ATTR_ID,
|
2020-09-15 21:01:01 +00:00
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
|
|
|
CURRENCY_EURO,
|
|
|
|
)
|
2022-01-03 18:20:39 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-03-30 03:23:30 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 18:20:39 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-03-30 03:23:30 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-03-01 15:49:07 +00:00
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
from . import TankerkoenigDataUpdateCoordinator
|
|
|
|
from .const import DOMAIN, FUEL_TYPES
|
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"
|
|
|
|
|
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2022-01-03 18:20:39 +00:00
|
|
|
) -> None:
|
2020-03-01 15:49:07 +00:00
|
|
|
"""Set up the tankerkoenig sensors."""
|
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
coordinator: TankerkoenigDataUpdateCoordinator = hass.data[DOMAIN][entry.unique_id]
|
2020-03-01 15:49:07 +00:00
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
stations = coordinator.stations.values()
|
2020-03-01 15:49:07 +00:00
|
|
|
entities = []
|
|
|
|
for station in stations:
|
2022-03-30 03:23:30 +00:00
|
|
|
for fuel in coordinator.fuel_types:
|
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(
|
2020-04-03 13:14:42 +00:00
|
|
|
fuel,
|
|
|
|
station,
|
|
|
|
coordinator,
|
2022-03-30 03:23:30 +00:00
|
|
|
coordinator.show_on_map,
|
2020-03-01 15:49:07 +00:00
|
|
|
)
|
|
|
|
entities.append(sensor)
|
|
|
|
_LOGGER.debug("Added sensors %s", entities)
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class FuelPriceSensor(CoordinatorEntity, SensorEntity):
|
2020-03-01 15:49:07 +00:00
|
|
|
"""Contains prices for fuel in a given station."""
|
|
|
|
|
2022-03-28 07:48:25 +00:00
|
|
|
_attr_state_class = STATE_CLASS_MEASUREMENT
|
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
def __init__(self, fuel_type, station, coordinator, show_on_map):
|
2020-03-01 15:49:07 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-08-30 16:27:06 +00:00
|
|
|
super().__init__(coordinator)
|
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"]
|
2022-03-30 03:23:30 +00:00
|
|
|
self._brand = self._station["brand"]
|
2020-03-01 15:49:07 +00:00
|
|
|
self._price = station[fuel_type]
|
2020-04-03 13:14:42 +00:00
|
|
|
self._show_on_map = show_on_map
|
2020-03-01 15:49:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2022-03-30 03:23:30 +00:00
|
|
|
return f"{self._brand} {self._street} {self._house_number} {FUEL_TYPES[self._fuel_type]}"
|
2020-03-01 15:49:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2020-03-01 15:49:07 +00:00
|
|
|
"""Return unit of measurement."""
|
2020-09-15 21:01:01 +00:00
|
|
|
return CURRENCY_EURO
|
2020-03-01 15:49:07 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self):
|
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
|
2020-08-30 16:27:06 +00:00
|
|
|
return self.coordinator.data[self._station_id].get(self._fuel_type)
|
2020-03-01 15:49:07 +00:00
|
|
|
|
2020-04-03 13:14:42 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique identifier for this entity."""
|
|
|
|
return f"{self._station_id}_{self._fuel_type}"
|
|
|
|
|
2022-03-30 03:23:30 +00:00
|
|
|
@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",
|
|
|
|
)
|
|
|
|
|
2020-03-01 15:49:07 +00:00
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-03-01 15:49:07 +00:00
|
|
|
"""Return the attributes of the device."""
|
2020-08-30 16:27:06 +00:00
|
|
|
data = self.coordinator.data[self._station_id]
|
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,
|
|
|
|
}
|
2020-04-03 13:14:42 +00:00
|
|
|
|
|
|
|
if self._show_on_map:
|
|
|
|
attrs[ATTR_LATITUDE] = self._latitude
|
|
|
|
attrs[ATTR_LONGITUDE] = self._longitude
|
|
|
|
|
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
|