-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Migrate nsw_fuel_station to config flow #146001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
buxtronix
wants to merge
10
commits into
home-assistant:dev
Choose a base branch
from
buxtronix:nsw_fuel_coord
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0991878
Migrate nsw_fuel_station to config flows
buxtronix 5540a25
Migrate nsw_fuel to DataCoordinator
buxtronix 5efd47b
Update unit tests
buxtronix 25ee3e2
use multi_select
buxtronix 4568077
Merge branch 'dev' into nsw_fuel_coord
buxtronix 3895292
Address review comments.
buxtronix 5000a9b
address comments
buxtronix 7c667e6
Use consts in tests
buxtronix e084107
All fuel types, not just selected, plus other updates
buxtronix af9d5a1
Remove coordinator from config_flow
buxtronix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Migrate nsw_fuel to DataCoordinator
- Loading branch information
commit 5540a25c43488c207b2905b0b09b9227dbd122f3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or co
8000
mpiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Data coordinator for nsw_fuel.""" | ||
|
||
from dataclasses import dataclass | ||
from datetime import timedelta | ||
import logging | ||
|
||
from nsw_fuel import FuelCheckClient, Station | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
||
from .const import DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
type NswFuelStationConfigEntry = ConfigEntry[NswFuelStationDataUpdateCoordinator] | ||
|
||
|
||
@dataclass | ||
class NswFuelStationCoordinatorData: | ||
"""Data class for storing coordinator data.""" | ||
|
||
stations: dict[int, Station] | ||
prices: dict[tuple[int, str], float] | ||
fuel_types: dict[str, str] | ||
|
||
|
||
class NswFuelStationDataUpdateCoordinator( | ||
DataUpdateCoordinator[NswFuelStationCoordinatorData] | ||
): | ||
"""Coordinator for Nsw Fuel data.""" | ||
|
||
client: FuelCheckClient | ||
config_entry: NswFuelStationConfigEntry | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
) -> None: | ||
"""Initialise the data coordinator.""" | ||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name=DOMAIN, | ||
update_interval=timedelta(hours=1), | ||
) | ||
self.client = FuelCheckClient() | ||
|
||
async def _async_update_data(self) -> NswFuelStationCoordinatorData: | ||
"""Fetch updated data.""" | ||
price_data = NswFuelStationCoordinatorData( | ||
stations={}, | ||
prices={}, | ||
fuel_types={}, | ||
) | ||
raw_price_data = await self.hass.async_add_executor_job( | ||
self.client.get_fuel_prices | ||
) | ||
if self.data is None or len(self.data.fuel_types) < 1: | ||
reference_data = await self.hass.async_add_executor_job( | ||
self.client.get_reference_data | ||
) | ||
price_data.fuel_types = {f.code: f.name for f in reference_data.fuel_types} | ||
else: | ||
price_data.fuel_types = self.data.fuel_types | ||
|
||
# Restructure prices and station details to be indexed by station code | ||
# for O(1) lookup | ||
price_data.stations = {s.code: s for s in raw_price_data.stations} | ||
price_data.prices = { | ||
(p.sta C799 tion_code, p.fuel_type): p.price for p in raw_price_data.prices | ||
} | ||
|
||
return price_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.