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

Actually check the paired state instead of assuming it for bluetooth connection sensor #2738

Merged
merged 5 commits into from Aug 3, 2022

Conversation

dshokouhi
Copy link
Member

Summary

Fixes: #2736 by actually checking the bonded state. I am also suppressing the lint message as its not valid in our use case and makes the page look like it won't compile. The main app already requests the proper permission and its not necessary for the Wear app.

Screenshots

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

@dshokouhi dshokouhi changed the title Actually check the paired state instead of assuming it Actually check the paired state instead of assuming it for bluetooth connection sensor Jul 31, 2022
@jpelgrom
Copy link
Member

Now a single device is showing up multiple times in the same attribute in the Bluetooth Connection sensor for me:

Sensor attributes showing a single device appearing multiple times in one list

I think a check should be added to make sure that even if allowDuplicates is true, it should filter out devices where the BluetoothDevice data class is exactly the same.

@dshokouhi
Copy link
Member Author

I think a check should be added to make sure that even if allowDuplicates is true, it should filter out devices where the BluetoothDevice data class is exactly the same.

I think a better way instead of adding another condition is to just call distinct() on the lists. At least in my setup it works but curious if you still get duplicates :)

@jpelgrom
Copy link
Member

distinct works for the attributes but still results in an incorrect state / total of connected devices, because the device is still included in the list and there is no filter/distinct on the count.
The Bluetooth Connection sensor showing "2 connection(s)" as a state but only 1 connected device in the attributes

@dshokouhi
Copy link
Member Author

ok now i think it should be good 🤞

@jpelgrom
Copy link
Member

Unfortunately, I don't think it is. It works for me, but Android's BluetoothDevice.equals only compares the device address, so if I understood your comments in #2736 correctly, that Mi Band would now never show up as connected again (it is paired, but the connected device isn't recognized as paired)? If it is only comparing the device address we might as well remove the allowDuplicates parameter.

Assuming my interpretation of your comments in the issue is correct, suggestion:

for (btDev in btConnectedDevices) {
    if (!allowDuplicates && devices.any { it.address == btDev.address }) continue
    
    val name = btDev.name ?: btDev.address
    val device = BluetoothDevice(
        btDev.address,
        name,
        btDev.bondState == android.bluetooth.BluetoothDevice.BOND_BONDED,
        isConnected(btDev)
    )
    if (!devices.contains(device)) devices.add(device)
}

@dshokouhi
Copy link
Member Author

so if I understood your comments in #2736 correctly, that Mi Band would now never show up as connected again (it is paired, but the connected device isn't recognized as paired)?

it actually did show up as connected when I tested the latest change. In my setup the attributes have always been correct in each of the changes, which makes the testing part difficult lol. Even when I test your provided code just now I see no changes.

My Mi band is actually not a paired device (not paired to the OS but paired to its own app) so it should only show up as connected_not_paired_devices which it always has with each change.

If it is only comparing the device address we might as well remove the allowDuplicates parameter.

I always took it as allow duplicates was meant more to hide the non-paired devices. At least thats how I initially read it. With that said IMO the device address is really the only thing that counts.

@jpelgrom
Copy link
Member

jpelgrom commented Jul 31, 2022

My Mi band is actually not a paired device (not paired to the OS but paired to its own app) so it should only show up as connected_not_paired_devices which it always has with each change.

In that case I didn't understand your comments, sorry. For connected, not paired devices there should indeed be no changes 🙈

If it is only comparing the device address we might as well remove the allowDuplicates parameter.

I always took it as allow duplicates was meant more to hide the non-paired devices. At least thats how I initially read it. With that said IMO the device address is really the only thing that counts.

No it was added in #2591 to stop the settings from crashing due to device addresses being included twice in the list, while still allowing this sensor to get a list with a device with the same address showing up in both the paired connected and not paired connected lists. As you can see it doesn't do anything for non-paired devices.

Considering the issue this PR solves and the confusion it creates, looking back at it I think it is no longer necessary? The app will now always check paired + connected status irregardless of the source -> duplicates should never happen.

@dshokouhi
Copy link
Member Author

Considering the issue this PR solves and the confusion it creates, looking back at it I think it is no longer necessary? The app will now always check paired + connected status irregardless of the source.

one of those things were code wise it doesnt look right, until you look at the end result and realize why it was like that 😅

So I guess I should close the PR then?

@jpelgrom
Copy link
Member

jpelgrom commented Jul 31, 2022

So I guess I should close the PR then?

No the PR is still good to have! 'it' was referring to allowDuplicates, not the PR.

Keep the changes from the first commit, remove allowDuplicates and always treat it as true false / always apply this:

if (!allowDuplicates && devices.any { it.address == btDev.address }) continue

Copy link
Member

@jpelgrom jpelgrom 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 your patience @dshokouhi ❤️

Tested and resolves the issue

@dshokouhi
Copy link
Member Author

Thanks for your patience @dshokouhi ❤️

Tested and resolves the issue

Thanks for your help in testing and code suggestions 😃

@JBassett JBassett merged commit db0ddf2 into home-assistant:master Aug 3, 2022
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.

Bluetooth Connection sensor incorrectly assumes connected devices are not paired
4 participants