Skip to content

Add support for unloading entries to device trackers#20237

Closed
rohankapoorcom wants to merge 6 commits into
home-assistant:devfrom
rohankapoorcom:device-tracker-unload
Closed

Add support for unloading entries to device trackers#20237
rohankapoorcom wants to merge 6 commits into
home-assistant:devfrom
rohankapoorcom:device-tracker-unload

Conversation

@rohankapoorcom
Copy link
Copy Markdown
Member

@rohankapoorcom rohankapoorcom commented Jan 19, 2019

Description:

Device tracker platforms did not support having their entries unloaded. This PR adds support for that and demonstrates how it works with the gpslogger platform. This came out of a couple of discussions with @MartinHjelmare #20083 (comment) and #20079 (comment).

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Do we also need to ask the DeviceTracker instance to remove the entities of the platform when unloading it?

Comment thread homeassistant/components/device_tracker/__init__.py Outdated
Comment thread homeassistant/components/device_tracker/__init__.py
Comment thread homeassistant/components/device_tracker/__init__.py Outdated
@rohankapoorcom
Copy link
Copy Markdown
Member Author

rohankapoorcom commented Jan 19, 2019

Do we also need to ask the DeviceTracker instance to remove the entities of the platform when unloading it?

I'm not sure what you mean by this.

Any suggestions on additional tests to add? Should I test the manager methods?

@MartinHjelmare
Copy link
Copy Markdown
Member

The DeviceTracker instance manages all the entities (devices) of this component. For EntityComponent platforms we remove the corresponding entities from the state machine when unloading a config entry. Should we do the same here?

@MartinHjelmare
Copy link
Copy Markdown
Member

Yes, we should test the manager. But let's wait for input from @home-assistant/core. They need to approve before we continue with this approach.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jan 21, 2019

I think that it's not a good idea to create a new approach to solve a problem that has been previously solved.

However, given how device tracker "merges" devices (Mac/dev_id), it would not be possible to create a method that would allow to leverage entity component.

However, there is hope! There is a proposal for a "person" component, which will be responsible to do merging of devices. Once that is added, we can drop merging from device tracker (as it's wonky at best anyway) and migrate it to EntityComponent.

@rohankapoorcom
Copy link
Copy Markdown
Member Author

All of that makes sense for the final state that we want to be in. Are you okay with introducing this new logic as a stopgap until the Person component is created and Device Trackers and migrated over to EntityComponent?

I think this approach makes a good stop gap because it adds very little code and is easily testable. It's also very unintuitive that Device Trackers don't support being unloaded.

return
if platform_name in self.platforms:
_LOGGER.warning(
'Cannot add duplicate platform %s to platform manager',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A person can have duplicates of a platform in their config.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep - I totally blanked on that. Switching to storing a namedtuple with the count of loading those platforms, when the last once is unloaded, remove the platform from the manager.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But that means we don't unload entities until they are all unloaded? That seems weird. It's better if we can distinguish where the entities came from.

I really think that it's not simple to do this, it brings a lot of complexities and we're introducing a bunch of more bugs probably. I rather not have the stopgap. There is also a person component PR #20290

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, the entity would still be unloaded. The platform manager would still keep a reference to the platform, allowing other entities from the platform to be unloaded up until the last one was unloaded.

If you feel strongly about not pursuing the stopgap, I could just mark the appropriate tests as xfail until the Person component is merged and we've switched to EntityComponent.

@MartinHjelmare
Copy link
Copy Markdown
Member

I suggested a PR like this, but maybe we should focus on getting to EntityComponent instead? Yesterday I thought more about how we can refactor and migrate device trackers to EntityComponent and I don't think it has to be very bad. I'm hoping we can do it one platform at a time too. I haven't looked into details yet though.

What do you think?

@rohankapoorcom I'm sorry if it turns out we wasted some time here. I hope you will want to continue getting entry unloading to device trackers even if it means we have to take the long route.

@rohankapoorcom
Copy link
Copy Markdown
Member Author

Sounds good @MartinHjelmare. Let's discuss further on discord and see what would be needed to get this working incrementally. I'm certainly happy to help.

I'm going to close this out and make a new PR for the associated platform loading/unloading work and tests.

@MartinHjelmare
Copy link
Copy Markdown
Member

Ok 👍

FYI, I have a WIP branch here with my attempt at getting device tracker to EntityComponent. As soon as I get all tests passing, I'll make a WIP PR for discussion.

https://github.com/MartinHjelmare/home-assistant/tree/add-device_tracker-entity-component

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