Skip to content
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

Overlapping scanning devices #12

Open
Edo78 opened this issue Nov 16, 2020 · 3 comments
Open

Overlapping scanning devices #12

Edo78 opened this issue Nov 16, 2020 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Edo78
Copy link

Edo78 commented Nov 16, 2020

I'm not 100% sure and it's hard to debug but it seems that a device_tracker is updated with the information from the latest scan.

If a device is in range of multiple google home the RSSI and the ghname seems to be the one reported from the last google home to respond while they should be from the one with the better RSSI.

Can you confirm this behaviour?

@Drakulix
Copy link
Owner

Yes, that is correct.

Unfortunately there is no easy way to fix this, because that is how the legacy device_tracker api works. I cannot set a new 'last_seen'-time without overriding the previous values (e.g. RSSI and ghname).

This should be fixable by migrating to the new api. A good chunk of code needs to be refactored to do this though.
So while this is on my TODO list, it might take some time, if nobody wants to set in and help. I am happy to mentor anyone willing to give this a shot.

@Drakulix Drakulix added enhancement New feature or request help wanted Extra attention is needed labels Nov 17, 2020
@Edo78
Copy link
Author

Edo78 commented Nov 17, 2020

I'm a developer but the last and only time I used python was more than 15 years ago to write a blender script but this may be the right time to give python a try.
This evening I'm looking at the code to try to figure how it works and if I'm feeling confident enought I'll be back to talk about a refactor.

@Drakulix
Copy link
Owner

Drakulix commented Nov 17, 2020

The home-assistant plugin system is an interesting one. Let me give you a quick rundown of how this works, before you take a look at it. Especially if you are not too familiar with python anymore.

Initialization of the component happens here

Per config_entry (that is per linked google account in the home-assistant UI, usually one, but may be more) this function is called and forwards setup to the sensor-platform (for timers and alarms) and the device_tracker-platform (bluetooth monitoring).

So initialization for the device-tracker component happens here.

This creates one DeviceTrackerMonitor per call, meaning again per config_entry.
The Monitor shares some common code with the sensor component to discover chromecast devices, which is why it inherits the ChromecastMonitor class. But that really is not important for the functionality of the device_tracker.

Setup continues, when said helper class calls setup per discovered chromecast, that has bluetooth capabilities, which in turn creates a GoogleHomeDeviceScanner.

And that is the one, that inherits the "old" DeviceTracker class from home-assistant.

What should be done instead is creating an entity per discovered bluetooth device, that inherits the TrackerEntity-class and add those via the provided async_add_entities-function.

Then those devices can be updated on incoming scans (instead of overriding all values), but additionally we need to implement logic to remove devices that were not seen for a given time again. I believe, that the mentioned DeviceTracker legacy-api is built on top this new api and has logic to track this. Internally it even has it's own device class. So that code might be used as a starting point.

Additionally all components already using the new api can of course be used to study this.
I have not yet found an up-to-date documentation on the subject, but once you get familiar enough with the home-assistant codebase, the existing components really provide a nice set of examples.

Let me know in case there is anything else, I can help you with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants