1 """Sensor platform to display the current fuel prices at a NSW fuel station."""
3 from __future__
import annotations
7 import voluptuous
as vol
10 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
20 DataUpdateCoordinator,
23 from .
import DATA_NSW_FUEL_STATION, StationPriceData
25 _LOGGER = logging.getLogger(__name__)
27 ATTR_STATION_ID =
"station_id"
28 ATTR_STATION_NAME =
"station_name"
30 CONF_STATION_ID =
"station_id"
31 CONF_FUEL_TYPES =
"fuel_types"
32 CONF_ALLOWED_FUEL_TYPES = [
45 CONF_DEFAULT_FUEL_TYPES = [
"E10",
"U91"]
47 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
49 vol.Required(CONF_STATION_ID): cv.positive_int,
50 vol.Optional(CONF_FUEL_TYPES, default=CONF_DEFAULT_FUEL_TYPES): vol.All(
51 cv.ensure_list, [vol.In(CONF_ALLOWED_FUEL_TYPES)]
60 add_entities: AddEntitiesCallback,
61 discovery_info: DiscoveryInfoType |
None =
None,
63 """Set up the NSW Fuel Station sensor."""
65 station_id = config[CONF_STATION_ID]
66 fuel_types = config[CONF_FUEL_TYPES]
68 coordinator = hass.data[DATA_NSW_FUEL_STATION]
70 if coordinator.data
is None:
71 _LOGGER.error(
"Initial fuel station price data not available")
75 for fuel_type
in fuel_types:
76 if coordinator.data.prices.get((station_id, fuel_type))
is None:
78 "Fuel station price data not available for station %d and fuel type %s",
90 CoordinatorEntity[DataUpdateCoordinator[StationPriceData]], SensorEntity
92 """Implementation of a sensor that reports the fuel price for a station."""
94 _attr_attribution =
"Data provided by NSW Government FuelCheck"
98 coordinator: DataUpdateCoordinator[StationPriceData],
102 """Initialize the sensor."""
110 """Return the name of the sensor."""
112 return f
"{station_name} {self._fuel_type}"
116 """Return the state of the sensor."""
117 if self.coordinator.data
is None:
120 prices = self.coordinator.data.prices
125 """Return the state attributes of the device."""
133 """Return the units of measurement."""
134 return f
"{CURRENCY_CENT}/{UnitOfVolume.LITERS}"
137 default_name = f
"station {self._station_id}"
138 if self.coordinator.data
is None:
141 station = self.coordinator.data.stations.get(self.
_station_id_station_id)
149 """Return a unique ID."""
150 return f
"{self._station_id}_{self._fuel_type}"
dict[str, int|str] extra_state_attributes(self)
str native_unit_of_measurement(self)
def _get_station_name(self)
None __init__(self, DataUpdateCoordinator[StationPriceData] coordinator, int station_id, str fuel_type)
float|None native_value(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)