Skip to content

Conversation

heindrichpaul
Copy link

@heindrichpaul heindrichpaul commented Jul 3, 2025

Proposed change

This PR enhances the Nederlandse Spoorwegen integration by implementing a full UI configuration flow and raising the integration quality to meet the Silver tier requirements. It adds robust reauthentication and reconfiguration support, improves error handling, and ensures a better user experience through clear validation and messaging. These changes make the integration easier to set up, maintain, and use within Home Assistant.

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • 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:

Similar code found with 1 license type

@Copilot Copilot AI review requested due to automatic review settings July 3, 2025 09:03
@heindrichpaul heindrichpaul requested a review from a team as a code owner July 3, 2025 09:03
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 @heindrichpaul

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 home-assistant bot added cla-needed code-quality config-flow This integration migrates to the UI by adding a config flow has-tests integration: nederlandse_spoorwegen labels Jul 3, 2025
@home-assistant
Copy link

home-assistant bot commented Jul 3, 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 home-assistant bot marked this pull request as draft July 3, 2025 09:03
@joostlek
Copy link
Member

Oh and please don't merge dev in your branch that often, currently the CI doesn't run because you haven't merged anything yet, but after that it would cost quite a bit of CI resources :)

We generally only do that when it has been out of sync for a LONG time, or when we know it will break, or for other reasons

…eData for runtime data management, enhancing structure and readability. Added comprehensive subentry flow tests for route management, including validation, error handling, and successful route creation scenarios. Improved test coverage for options flow and reconfiguration processes, ensuring robust handling of user inputs and station data.
…chpaul/core into feature/improving_NS_integration
…ation

- Introduced , , and  to analyze device and entity registries post-migration.
- Implemented JSON error handling tests in  to ensure graceful handling of JSON parsing errors.
- Updated  to remove obsolete route service tests and ensure proper functionality of the coordinator.
- Refactored  to simplify async setup tests and removed service registration assertions.
- Enhanced  with a new test for device association after migration, ensuring correct entity and device creation.
- Deleted  as its functionality is now covered in other tests.
- Improved  to validate station options formatting and ensure correct handling of various station formats.
- Updated test_migration.py to use AsyncMock for NSAPIWrapper and refactored legacy route migration tests.
- Added new test_migration_entry.py to cover config entry migration scenarios.
- Removed outdated tests from test_sensor.py related to service and trip sensors, adapting to new architecture.
- Introduced test_station_parsing.py to validate station string parsing functionality.
- Deleted test_sensor_past_trips.py as it was no longer relevant.
- Updated test_subentry_flow.py to ensure time format consistency in route configurations.
…ion and reconfiguration steps. Update strings for improved clarity and consistency in route management.
- Implement tests for sensor logic in test_sensor.py, covering various scenarios including device association, availability, and native value handling.
- Create new test files: test_sensor_edge_cases.py and test_sensor_new.py for additional edge case testing and new sensor functionalities.
- Introduce utility tests in test_utils.py to validate time formatting, route validation, and safe data extraction methods.
- Ensure coverage for scenarios with missing or invalid data in both sensors and utilities.
@heindrichpaul heindrichpaul marked this pull request as ready for review July 21, 2025 10:55
@home-assistant home-assistant bot requested a review from joostlek July 21, 2025 10:55
@heindrichpaul
Copy link
Author

@joostlek I have implemented the sub entries and made all the requested changes. Once we know if this is the final state I will update the docs again.

Comment on lines +85 to +92
async def _async_migrate_legacy_routes(
hass: HomeAssistant, entry: NSConfigEntry
) -> None:
"""Migrate legacy routes from configuration data into subentries.

This handles routes stored in entry.data[CONF_ROUTES] from legacy YAML config.
One-time migration to avoid duplicate imports.
"""
Copy link
Member

Choose a reason for hiding this comment

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

Okay, so instead of importing the yaml into a config entry and only then migrating that config entry into subentries, we should migrate to subentries directly instead. Less moving objects, less room for error :)

Copy link
Author

@heindrichpaul heindrichpaul Jul 21, 2025

Choose a reason for hiding this comment

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

I'm actually already doing exactly what you suggested. The current _async_migrate_legacy_routes function directly converts YAML routes to subentries without any intermediate steps:

  1. Legacy YAML routes (stored in entry.data[CONF_ROUTES])
  2. → Directly converted to ConfigSubentry objects
  3. → Added as subentries using hass.config_entries.async_add_subentry(entry, subentry)

There's no intermediate step of creating separate config entries first. The flow is:

  • Take YAML routes from entry.data[CONF_ROUTES]
  • Create ConfigSubentry objects directly from that YAML data
  • Add them as subentries to the existing config entry
  • Clean up the legacy CONF_ROUTES from the main entry data

@joostlek
Could you clarify if I'm missing something or if there's a specific aspect of the migration that should be improved?

Copy link
Member

Choose a reason for hiding this comment

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

Yes and no, you're running that code in async_setup_entry, which means that there already should be an entry to reach that code.

def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:

This is the current entrypoint of the integration and we should use this (or the async counterpart) to start an import flow, and then the code in async_step_import in the config flow should make sure all the right subentries are created.

Copy link
Author

@heindrichpaul heindrichpaul Jul 21, 2025

Choose a reason for hiding this comment

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

@joostlek I have now added the logic to the async_setup as well. I have also run multiple tests locally and it imports the routes and config without issues

@home-assistant home-assistant bot marked this pull request as draft July 21, 2025 11:21
@heindrichpaul heindrichpaul marked this pull request as ready for review July 21, 2025 11:58
@home-assistant home-assistant bot requested a review from joostlek July 21, 2025 11:58
@joostlek
Copy link
Member

You should use async_setup_platform in sensor.py instead of async_setup as we should keep the current entrypoint for YAML

@heindrichpaul
Copy link
Author

heindrichpaul commented Jul 21, 2025

You should use async_setup_platform in sensor.py instead of async_setup as we should keep the current entrypoint for YAML

@joostlek It is now fixed. Sorry had the wrong config missed the sensor and platform that was why I didn't see it.

@joostlek
Copy link
Member

Will check!

@heindrichpaul
Copy link
Author

@joostlek did you have time to take a look? I wanted to update the docs PR but am first waiting on this one's go ahead before I finish that.

@joostlek
Copy link
Member

joostlek commented Aug 8, 2025

Can you maybe send me a message on Discord?

@heindrichpaul
Copy link
Author

@joostlek I created a smaller PR #151567

@heindrichpaul heindrichpaul marked this pull request as draft September 2, 2025 09:41
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.

5 participants