feat: keep charger devices in sync automatically - #78
Open
michaeln31 wants to merge 1 commit into
Open
Conversation
Standardizes device info across the sensor and switch platforms on Home Assistant's typed DeviceInfo helper (lock already used it), and hoists the duplicated model-update logic into a shared mixin (entity.py). Adds two Gold-tier quality behaviors: - dynamic-devices: if a new charger appears on the account, its entities are added automatically without needing a Home Assistant restart. - stale-devices: if a charger is removed from the account, its device registry entry is cleaned up automatically. quality_scale.yaml: mark dynamic-devices and stale-devices as done.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the Andersen EV Home Assistant integration to keep the set of charger devices in sync at runtime (adding new devices without restart and cleaning up removed devices), while also standardizing entity device_info and consolidating duplicated “model-from-status” logic into a shared mixin.
Changes:
- Add coordinator listeners in each platform to dynamically add entities for newly discovered devices.
- Add an integration-level coordinator listener to remove stale device registry entries when devices disappear from the API.
- Standardize
DeviceInfousage and centralize model-update logic in a sharedAndersenEvDeviceInfoMixin, with new/updated tests and quality scale updates.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| custom_components/andersen_ev/init.py | Registers a stale-device cleanup listener to remove device registry entries when devices disappear. |
| custom_components/andersen_ev/entity.py | Introduces a shared mixin to update device model info from API/device status consistently. |
| custom_components/andersen_ev/lock.py | Adds dynamic device entity creation via coordinator listener and adopts shared device-info mixin. |
| custom_components/andersen_ev/sensor.py | Adds dynamic device entity creation via coordinator listener and adopts shared device-info mixin. |
| custom_components/andersen_ev/switch.py | Refactors switch entity creation, adds dynamic device support, and adopts typed DeviceInfo + shared mixin. |
| custom_components/andersen_ev/tests/test_entity.py | Adds unit tests for the shared model-update helper logic. |
| custom_components/andersen_ev/tests/test_init.py | Adds tests for stale-device cleanup listener registration and behavior. |
| custom_components/andersen_ev/tests/test_lock.py | Adds tests validating dynamic device entity creation for locks. |
| custom_components/andersen_ev/tests/test_sensor.py | Adds tests validating dynamic device entity creation for sensors. |
| custom_components/andersen_ev/tests/test_switch.py | Adds tests validating dynamic device entity creation for schedule switches (including async task scheduling). |
| custom_components/andersen_ev/quality_scale.yaml | Marks dynamic-devices and stale-devices as done. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+31
to
+34
| def _entities_for_new_devices() -> list[AndersenEvLock]: | ||
| """Build lock entities for any device not seen before.""" | ||
| new_devices = [device for device in coordinator.data if device.device_id not in known_device_ids] | ||
| entities = [] |
Comment on lines
+43
to
+46
| def _entities_for_new_devices() -> list[SensorEntity]: | ||
| """Build sensor entities for any device not seen before.""" | ||
| new_devices = [device for device in coordinator.data if device.device_id not in known_device_ids] | ||
| entities: list[SensorEntity] = [] |
Comment on lines
+75
to
+79
| def _handle_coordinator_update() -> None: | ||
| """Schedule switch creation for any device not seen before.""" | ||
| new_devices = [device for device in coordinator.data if device.device_id not in known_device_ids] | ||
| if not new_devices: | ||
| return |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What this does
Part of the Gold tier of the CI/CD to Platinum quality scale climb (item G2).
DeviceInfohelper across the sensor andswitch platforms (the lock platform already used it), and moves the duplicated "update model
from device status" logic into one shared helper (
entity.py) instead of five copies.entities now show up without needing to restart Home Assistant.
its device registry entry is now removed automatically instead of lingering.
dynamic-devicesandstale-devicesas done in the quality scale checklist.Testing
./run-tests.ps1 --cov=custom_components/andersen_ev --cov-fail-under=95locally in Docker:317 passed, 99.66% coverage (gate is 95%).
ruff checkandruff formatclean on all changed files.platform, and a device disappearing (registry cleanup).
Manual test plan (before merge to main / after beta install)
andersen_ev: confirm no errors on load.Settings > Devices & Services > Andersen EV, with correct model/name/serial number.
which most users will not be able to trigger; the important thing is that nothing regresses
for the single, unchanged device case above.