1 """The nsw_fuel_station component."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
9 from nsw_fuel
import FuelCheckClient, FuelCheckError, Station
16 from .const
import DATA_NSW_FUEL_STATION
18 _LOGGER = logging.getLogger(__name__)
20 DOMAIN =
"nsw_fuel_station"
21 SCAN_INTERVAL = datetime.timedelta(hours=1)
23 CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN)
26 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
27 """Set up the NSW Fuel Station platform."""
28 client = FuelCheckClient()
30 async
def async_update_data():
31 return await hass.async_add_executor_job(fetch_station_price_data, client)
38 update_interval=SCAN_INTERVAL,
39 update_method=async_update_data,
41 hass.data[DATA_NSW_FUEL_STATION] = coordinator
43 await coordinator.async_refresh()
50 """Data structure for O(1) price and name lookups."""
52 stations: dict[int, Station]
53 prices: dict[tuple[int, str], float]
57 """Fetch fuel price and station data."""
59 raw_price_data = client.get_fuel_prices()
63 stations={s.code: s
for s
in raw_price_data.stations},
65 (p.station_code, p.fuel_type): p.price
for p
in raw_price_data.prices
69 except FuelCheckError
as exc:
71 f
"Failed to fetch NSW Fuel station price data: {exc}"
StationPriceData|None fetch_station_price_data(FuelCheckClient client)
bool async_setup(HomeAssistant hass, ConfigType config)