8000 Remove old Airthings devices by LaStrada · Pull Request #145914 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Remove old Airthings devices #145914

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 8 commits into
base: dev
Choose a base branch
from

Conversation

LaStrada
Copy link
Member

Breaking change

Proposed change

I had a long list of old Airthings devices that had all been removed, but I wasn't able to remove them. This PR will make sure to remove old devices that are no longer included in the list of devices from the API. This will happen if you remove (unpair) a device.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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:

@home-assistant
Copy link

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

Code owner commands

Code owners of airthings 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 airthings 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.

Comment on lines 53 to 69
def _remove_old_devices(
hass: HomeAssistant,
entry: AirthingsConfigEntry,
airthings_devices: dict[str, AirthingsDevice],
) -> None:
device_registry = dr.async_get(hass)

for registered_device in device_registry.devices.get_devices_for_config_entry_id(
entry.entry_id
):
device_id = next(
(i[1] for i in registered_device.identifiers if i[0] == DOMAIN), None
)
if device_id and device_id not in airthings_devices:
device_registry.async_update_device(
registered_device.id, remove_config_entry_id=entry.entry_id
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the accumulation of old devices something that we can avoid? For example could they be removed on a case by case basis when they are unpaired?

If that is possible, then stray devices could be removed via a repair action where the user has to confirm it.

My concern is that if something goes wrong with the API, this code might delete a load of user settings that they might want.

Copy link
Member

Choose a reason for hiding this comment

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

Automatically removing devices is fine and in theory is the behavior we want, BUT the api needs to be reliable. As in, if a device isn't returned when it's offline, then we should not do it.

We can't avoid API errors where the API would return zero devices, but I think that's very rare.

A good thing to keep in mind is that if a device is created again with the same unique id, the old name and such is used again.

Copy link
Member Author

Choose a reason for hiding this comment

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

The API will never remove the device without the user removing it from the app. If the device is offline, we'll just get the latest values. If something fails, the API will return a 5xx error, not return an incomplete device list.

But of course, theoretically something could go wrong and the API could miss a device or something. So maybe I should just change the implementation?

A good thing to keep in mind is that if a device is created again with the same unique id, the old name and such is used again.

We use serial number as unique id, so the old one will be used.

Copy link
Member

Choose a reason for hiding this comment

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

But of course, theoretically something could go wrong and the API could miss a device or something. So maybe I should just change the implementation?

Generally we don't take that into account, so I think this isn't as much of a concern

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking of that occasional problem where some elements fail to load on a web page and you have to refresh it.
For example if logging in and downloading the device list are done in two transactions.

But if that's impossible or a low enough risk then ok by me!

I would suggest that if there's no user confirmation we should put a message in the log when autodeleting something from the config.

Copy link
Member

Choose a reason for hiding this comment

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

I think an integration should retry set up if it does not have all the information needed to set up :)

Copy link
Member Author

Choose a reason for hiding this comment

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

pyAirthings will raise an exception if anything fails:
https://github.com/Danielhiversen/pyAirthings/blob/main/airthings/__init__.py#L184

We need locations (which is cached) + devices.

I would suggest that if there's no user confirmation we should put a message in the log when autodeleting something from the config.

That I can do.

And FYI: I more or less followed the first example here:
https://developers.home-assistant.io/docs/core/integration-quality-scale/rules/stale-devices
If this is not the "best" way of doing it, should have logs when doing it etc, this should probably be updated with some more details

Copy link
Contributor
@davet2001 davet2001 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 adding the logging, minor rewording but otherwise ok.

Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com>
Copy link
Member
@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

Would it make sense to extend the test a slight bit so we also add a device that does belong there? This way we also test that devices that do belong there are staying

)
first_entry.add_to_hass(hass)

device_registry = dr.async_get(hass)
Copy link
Member

Choose a reason for hiding this comment

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

Device registry can be loaded as test parameter instead


# Fetch new data with no devices
with patch(
"homeassistant.components.airthings.coordinator.AirthingsDataUpdateCoordinator._update_method",
Copy link
Member

Choose a reason for hiding this comment

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

Why do we patch an internal? I'd rather mock the library

@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 home-assistant bot marked this pull request as draft June 1, 2025 16:05
@LaStrada
Copy link
Member Author
LaStrada commented Jun 2, 2025

@joostlek I’ve rewritten the test and added a few more to ensure I didn't break anything (these probably belonged in a separate PR).

The new tests brings us to 100% test coverage. Let me know if you’d prefer I move the extra tests to a new PR or update the existing Airthings test coverage PR instead.

@LaStrada LaStrada marked this pull request as ready for review June 2, 2025 18:50
@home-assistant home-assistant bot requested a review from joostlek June 2, 2025 18:50
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.

4 participants
0