8000 Migrate nsw_fuel_station to config flow by buxtronix · Pull Request #146001 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

buxtronix
Copy link
@buxtronix buxtronix commented Jun 1, 2025

Proposed change

This migrates the nsw_fuel_station integration from legacy YAML config to a config_flow with a much simplified setup experience. Currently, users have to sniff the official web page to fetch station ID, with this change the config flow will allow search for the stations of choice.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

buxtronix added 3 commits May 22, 2025 12:04
This contains minimal changes required to support config flow and import yaml entries. A PR in progress will restructure and enhance the features further.
@home-assistant home-assistant bot added cla-needed code-quality config-flow This integration migrates to the UI by adding a config flow has-tests labels Jun 1, 2025
home-assistant[bot]
home-assistant bot previously requested changes < 8000 span class="color-fg-muted"> Jun 1, 2025
Copy link
@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @buxtronix

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link
home-assistant bot commented Jun 1, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
Copy link
home-assistant bot commented Jun 1, 2025

Hey there @nickw444, mind taking a look at this pull request as it has been labeled with an integration (nsw_fuel_station) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of nsw_fuel_station can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign nsw_fuel_station Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@buxtronix buxtronix marked this pull request as ready for review June 2, 2025 09:34
@home-assistant home-assistant bot dismissed their stale review June 2, 2025 09:34

Stale

Copy link
Contributor
@NoRi2909 NoRi2909 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three quick comments from me as a translator.

"fetch_failed": "Failed to fetch fuel price data, check logs",
"no_matching_stations": "No stations matched the provided search string",
"missing_fuel_types": "Please select one or more fuel types",
"station_exists": "Already have entry for that station. Remove and re-add to change fuel types."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid language that uses first-person singular or plural. Here "Already have …" can be read as both "I already have …" and "We already have …" which ends up with bad translations.

Perhaps you can use "An entry for that station already exists. …" here instead.

"title": "Add new petrol station",
"description": "Enter a string to search by name or address",
"data": {
"search_string": "Name/Address search"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be sentence-cased -> "Name/address search"

"select_fuel": {
"description": "Choose fuel types to display",
"data": {
"fuel_types": "Fuel Types"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also needs sentence-casing -> "Fuel types"

@erwindouna
Copy link
Contributor

This PR is rather large, in the sense you describe you only migrate to the config flow, whilst also a coordinator and adjustments to the sensors have been added. General advise would be to keep PR's small, so cut it in pieces:

  1. Config flow
  2. Coordinator addition

Copy link
Contributor
@erwindouna erwindouna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your first contribution! Here is my initial feedback, focusing only on the config flow.

self.selected_station = next(
station for station in self.stations if station.code == station_id
)
for existing_entry in self._async_current_entries():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you create an entry with a unique ID, you don't have to go through a loop. Try using code like this:

 await self.async_set_unique_id(YOUR_UNIQUE_STATION_ID)
 self._abort_if_unique_id_configured(updates=self._data)

errors: dict[str, str] = {}

if user_input is not None:
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check here. Make the fuel type required in your schema - it will save this check.

pytestmark = pytest.mark.usefixtures("mock_setup_entry")


@pytest.mark.usefixtures("mock_fuelcheckclient")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already configured in conftest. This can be removed.

assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

result2 = await hass.config_entries.flow.async_configure(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't create a new variable. Re-use result, to keep the context straight and read-able.

hass: HomeAssistant, mock_fuelcheckclient: MagicMock
) -> None:
"""Test we get the form."""
mock_fuelcheckclient.get_reference_data.side_effect = FuelCheckError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these all errors that can arise? Consider using pytest paramters to create multiple tests at once, within one method.



@pytest.mark.usefixtures("mock_fuelcheckclient")
async def test_invalid_fueltype(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check more general on the form exceptions and ensure that it can recover itself after exceptions - so a successful end path.

@@ -0,0 +1,126 @@
"""Test the nsw_fuel_station config flow."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test if a entry is already configured as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure you're getting a 100% config_flow code coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0