Drakulix / googlehome

Google Home Integration for Home-Assistant
Apache License 2.0
29 stars 3 forks source link

Scan failed, scheduling token update #21

Open nvx opened 3 years ago

nvx commented 3 years ago

New install, I had to get a master token using the python script and use that due to the issues described in #14

Setup seemed fine, except it can't ever scan. Errors in the log saying "DEBUG (MainThread) [custom_components.googlehome] Scan failed, scheduling token update" pretty constantly.

It seems to successfully be able to update Google Home info for the devices it's detecting however:

2021-02-15 21:59:50 DEBUG (MainThread) [custom_components.googlehome] Updating Google Home info for <ip>
2021-02-15 21:59:50 DEBUG (MainThread) [custom_components.googlehome] {<bulk json about one of my Google Home Minis here>}

Following the instructions at https://rithvikvibhu.github.io/GHLocalApi/#section/Google-Home-Local-API/Authentication I was able to use the access token to successfully get a local token and hit some endpoints on my Google Home Minis, so I'm confident that the master token works since that's how the get_tokens.py script gets the access token that worked. I do note that gpsoauth is a couple of patch versions behind, of which I note https://github.com/simon-weber/gpsoauth/commit/6a52a38b35db9b0732d4cec04f734963ca1dc578 is in 0.4.3 that might be relevant to this issue.

nvx commented 3 years ago

I updated the gpsoauth dependency to 0.4.3 which resolved the password login issue, but I'm still getting the same the scan failed message continually.

nvx commented 3 years ago

Looking a bit deeper, it looks like the error handling is a bit wrong. Adding some debug logs around here:

https://github.com/Drakulix/googlehome/blob/master/custom_components/googlehome/__init__.py#L113-L114

It turns out bluetooth_data is []. If I log the IP and token used at that call, I can GET /setup/bluetooth/scan_results on that Google Home Mini with that token and it works (HTTP 200, if I set the cast-local-authorization-token to a dummy value I get a HTTP 401), although the returned json body is []. So looks like the issue isn't "why is the auth not working", but rather "why is the scan results empty".

Is that if statement better off being something like if bluetooth_data is None:?

Drakulix commented 3 years ago

Thanks for opening and the debugging so far.

Yes you are right, an empty result is no wrong result, that should print an error message.

However are you expecting an empty result? Or is there still some problem with bluetooth scanning on your home mini? (Btw while the scan feature appears to be working for me, it is currently not in a very good state, but for entirely different reasons: #12)

nvx commented 3 years ago

So I thought this scanned for BLE devices, of which I can see a half dozen in range so thought something was definitely broken. To test though I opened Bluetooth pairing on my phone and low and behold it started showing up in the scans (of the nearest few Google Home devices anyway, the ones on the other side of the house can't see it naturally) and of course #12 bit me with the bathroom Google Home writing over the much closer Google Home that is next to me.

This was with that if statement changed to a None check.

Did Google change it from BLE to just regular BT? Or is it only looking for devices in pairing mode so not showing up the devices I see with a BLE scanner app?

Drakulix commented 3 years ago

Well the feature is used in the google home app to pair a speaker. So although the home mini has BLE functionality, it might not pick up every device or expose all result via the API endpoint. (Those get rendered in the google home app after all.)

However I never had a problem with smartphones showing up (without any pairing). So I am not sure whats going on. Maybe you can poke around in the api yourself? E.g. check your bluetooth status? https://rithvikvibhu.github.io/GHLocalApi/#operation/Status