8000 Remove LIFX devices with no entities by amelchio · Pull Request #65964 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove LIFX devices with no entities #65964

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

Merged
merged 1 commit into from
Feb 7, 2022

Conversation

amelchio
Copy link
Contributor
@amelchio amelchio commented Feb 6, 2022

Proposed change

Delete the matching device when a LIFX entity is removed.

We have to search through all our devices because retired lights will be restored entities that the running integration does not know about.

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)
  • 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:

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
  • The code has been formatted using Black (black --fast 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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

Copy link
Member
@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

This is great. It would be nice to make it a more general purpose helper as lots of integrations could use it

@amelchio amelchio merged commit 9e0926f into home-assistant:dev Feb 7, 2022

async def remove_empty_devices(self):
"""Remove devices with no entities."""
entity_reg = await er.async_get_registry(self.hass)
Copy link
Member

Choose a reason for hiding this comment

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

We prefer using er.async_get. The other is legacy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, thank you.

device_entry.id,
include_disabled_entities=True,
):
device_reg.async_remove_device(device_entry.id)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe only remove the config entry id from this device? If it's the only config entry, the device will be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how to do that. Anyway, for LIFX I think it does not matter since there is 1 config entry, N devices and 1 entity per device?

Copy link
Member

Choose a reason for hiding this comment

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

It could matter if another integration has the device mapped via the mac address, but that probably isn't a problem here since LiFX is the main integration of these devices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Can you give me a hint on how to implement your proposal?

Copy link
Member

Choose a reason for hiding this comment

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

Pass the config entry_id that you want to remove from the device entry to async_update_device.

remove_config_entry_id: str | UndefinedType = UNDEFINED,

self.async_add_entities = async_add_entities
self.effects_conductor = aiolifx_effects().Conductor(hass.loop)
self.discoveries = []
self.cleanup_unsub = self.hass.bus.async_listen(
EVENT_HOMEASSISTANT_STOP, self.cleanup
)
self.entity_registry_updated_unsub = self.hass.bus.async_listen(
Copy link
Member

Choose a reason for hiding this comment

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

We can use config_entry.async_on_unload to handle the clean up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to clean up as each entity is deleted, I don't think async_on_unload would do that?

Copy link
Member

Choose a reason for hiding this comment

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

I'm referring to the unsubscribe callback that we call when cleaning up the manager. Instead of storing that in entity_registry_updated_unsub we can pass it to config_entry.async_on_unload.

Copy link
Member

Choose a reason for hiding this comment

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

We could also make this a bit more efficient by using async_track_entity_registry_updated_event and updating the listener when entities are added/removed since entity registry updates can be a bit of a fire hose on large systems

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MartinHjelmare I looked into this and it seems that async_on_unload is only for deleting a config entry, it is not called on shutdown? I realize that cleanup of this listener is probably not strictly required on shutdown but I would find it odd to have two different cleanup paths.

Copy link
Contributor Author
@amelchio amelchio Feb 8, 2022

Choose a reason for hiding this comment

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

Also, 27k+ views say that users would like a UI button to delete a device which I think could remove the guesswork. https://community.home-assistant.io/t/delete-individual-device-and-entity-from-integration/157428

Edit: Actually around 100k views when linked posts are included.

Copy link
Member

Choose a reason for hiding this comment

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

I'll try to take a stab at this

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand. Users can still remove auto-restored entities and they will – but are then left with lingering, empty devices.

@amelchio yes, you're correct.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's great @bdraco.

Since it looks like there is some core movement on this feature I will not implement the post-merge comments for now.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0