home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.31k stars 639 forks source link

Add iBeacon capability to the Wear OS app #2183

Closed rpatel3001 closed 1 year ago

rpatel3001 commented 2 years ago

Is your feature request related to a problem? Please describe.

Room presence is a fun addition to a home automation setup, and the phone app beacon has been working well for me, but I sometimes leave my phone in a different room. Tracking a watch that's always on my wrist would be nice.

Describe the solution you'd like

An additional setting for the companion app to enable iBeacon advertisements, similar to the phone app.

Describe alternatives you've considered, if any

I'm going to tinker with writing my own app as the APIs for this don't seem too convoluted, but I've not done it before and it's not scalable.

Additional context

dshokouhi commented 2 years ago

All current phone sensors will be evaluated once the wear OS app has a sensor UI for enabling and adding settings. Will come sometime after #2033

rpatel3001 commented 2 years ago

Got it, thanks

rpatel3001 commented 2 years ago

If anyone else is interested in this and lands at this issue, I have a working proof of concept at https://github.com/rpatel3001/BeaconWear

Credit to the good folks at @AltBeacon, this was forked from https://github.com/davidgyoung/android-beacon-library-reference-kotlin and modified with code from https://altbeacon.github.io/android-beacon-library/beacon-transmitter.html.

There's still a ton of cruft for scanning and absolutely no user interface, but I'll probably clean it up eventually.

wifiuk commented 2 years ago

be handy to have beacon capability on the home assistant wear os app for sure.

maartendamen commented 2 years ago

Would love to see this in combination with ESPresense. @rpatel3001 can you please explain a bit more about your PoC. Can I use it on my GW4 by just compiling and installing it?

rpatel3001 commented 2 years ago

@maartendamen there is an apk available here. The UI doesn't actually do anything, the UUID and reference power are hard coded. You can clone the repo and build in Android Studio if you need to change those values.

maartendamen commented 2 years ago

@maartendamen there is an apk available here. The UI doesn't actually do anything, the UUID and reference power are hard coded. You can clone the repo and build in Android Studio if you need to change those values.

Very cool, I got it working. Thanks for your efforts! I will do some writing about this on my blog soon if you don't mind.

rpatel3001 commented 2 years ago

Very cool, I got it working. Thanks for your efforts! I will do some writing about this on my blog soon if you don't mind.

Not at all, drop a link here when you do.

ignacio82 commented 2 years ago

Any updates about this?

dshokouhi commented 2 years ago

Any updates about this?

the wear OS app still needs to handle permissions and sensor settings before beacon capabilities can be added.

dshokouhi commented 2 years ago

Thinking more about this request, in addition to needing permissions and sensor settings I think we will also need notifications supported directly in Wear OS so the user can control the transmitter. I don't think it makes sense to add this sensor until that is the case given that it can drain the battery and I don't expect any user to want this enabled 24/7.

I am open to consider other ideas but we should do some proper consideration on how this feature will work on Wear OS before adding it.

marazmarci commented 2 years ago

Idea: to not drain battery, the transmitter could be enabled for a specific (short) amount of time, for eg. quickly detecting which room the user is in.

Ulrar commented 2 years ago

I don't see how you'd implement that, the watch won't know when you're changing room and if you're talking about enabling it only every few minutes it's going to make it a bit useless for automation.

I do like the idea of enabling it / disabling it through notifications though, you could have HA enable it when you're at home and disable it when away for example, neat but not essential IMHO

marazmarci commented 2 years ago

Sorry, I didn't explain my whole concept that I had in my head.

Ulrar commented 2 years ago

Not sure what the delay on detection might actually be but that's a neat idea. Sounds like the opposite might be better suited for this (having beacons broadcasting which room you're in and let the watch relay that over to HA) but that's a whole different feature.

Either way IMHO a first version always on / always off would be great, and it can be refined later with notification or events, but that's ultimately left to people who know how to make WearOS stuff :)

dshokouhi commented 2 years ago

Idea: to not drain battery, the transmitter could be enabled for a specific (short) amount of time, for eg. quickly detecting which room the user is in.

how would that work? How would the transmitter know to turn itself on and off to make it useful to any automation when we cant send that data back to the watch to process (i.e. notification command on the watch)?

I do like the idea of enabling it / disabling it through notifications though, you could have HA enable it when you're at home and disable it when away for example, neat but not essential IMHO

Thats what the phone app supports. The reason why its essential for a watch is because the only way you can enable and disable a sensor is to dig into settings on the watch, find the sensor and enable or disable it as you please. On a watch thats not really ideal. When this sensor was added to the phone app the notification command was added at the same time because you need a good and reliable way to enable or disable it. Not having it automated defeats the purpose of a wearable here where its all about short interactions instead of needing to dig into places.

  • It could be enabled triggered by motion sensors in the roooms.

thats exactly what a notification command will allow you to do

  • Some watches have multiple buttons. If the HA Wear OS app would have multiple launcher icons, that would trigger some event in HA, they could be used to eg. toggle the light in the current room you're in, by pressing the button on the watch, then enabling the BLE beacon for a short amount of time, then one of the beacon monitors would pick the signal up, and an automation can toggle the lights in the detected room that you're in. (FYI there's an app called Button Launcher that lets you open more than one app or launcher item with a physical button. It could be useful for these kind of things.)

I believe apps only have access to those buttons when the app is open.

https://developer.android.com/training/wearables/user-input/physical-buttons

The link to the button launcher is giving a 404

Not sure what the delay on detection might actually be but that's a neat idea.

the delay may be there until the screen turns on the wearable due to doze restrictions

Sounds like the opposite might be better suited for this (having beacons broadcasting which room you're in and let the watch relay that over to HA) but that's a whole different feature.

we have the opposite already on teh phone side, a beacon monitor sensor to report distance on nearby beacons. However this one also has a corresponding notification command that was introduced alongside the sensor and has the same battery considerations as the transmitter.

Either way IMHO a first version always on / always off would be great, and it can be refined later with notification or events, but that's ultimately left to people who know how to make WearOS stuff :)

So if you can only enable and disable by physically interacting with the app how do you see that being useful? Are you goign to manually turn the sensor when you park your car and turn it off when you get in your house? Are you going to remember to turn it on and off before and after bed? What about when you leave the house? The first version introduced in the phone side contained the notification command.

Ulrar commented 2 years ago

So if you can only enable and disable by physically interacting with the app how do you see that being useful? Are you goign to manually turn the sensor when you park your car and turn it off when you get in your house? Are you going to remember to turn it on and off before and after bed? What about when you leave the house? The first version introduced in the phone side contained the notification command.

I'd just leave it on all the time, most likely. The notification support would be great, just saying I don't see it as a blocker myself as long as it defaults off. But I understand the blocker now is the settings menu so this is moot

dshokouhi commented 2 years ago

I'd just leave it on all the time, most likely.

Good to know I did not expect anyone to want to leave it on all the time lol

The notification support would be great, just saying I don't see it as a blocker myself as long as it defaults off.

it defaults to on actually because you are enabling the sensor so the expectation is that the transmitter also gets enabled. The transmitter itself is controlled by a sensor setting which is not available in Wear OS yet, the notification command makes it easier to use and automate.

We still need to do battery implication testing too, for example would you really want the sensor if your battery goes to 0 in 2 hours?

One thing I also considered was maybe adding a tile for turning the transmitter/monitor on and off, while it would make it faster it still wouldn't be automated.

marazmarci commented 2 years ago

The link to the button launcher is giving a 404

Hmm, maybe it's because the developer didn't update it to comply with the latest targetSdkVersion requirements, and the link works for me just because I still have it installed. Btw here's the app's source code on GitHub: https://github.com/moneytoo/ButtonLauncher

gsicilia82 commented 1 year ago

About battery drain: I am using Xiaomi Mi Band 6 as "Beacon simulator". Sending ble advertisements is switched on all the time. It sends every 5 seconds. Battery capacity from Mi Band is small, but it holds for more than 10 days. Therefore, I don't expect to face battery drain on wear OS. It should have an impact, but not so heavy as discussed. Has anyone tested POC from @rpatel3001 and checked battery impact?

Mausbiber commented 1 year ago

I am trying to install the apk from @rpatel3001. When I then try to start the app on my smartphone, I always get an error message:

But I can't find a way to give the app the desired permission.

Oh yes, I use a Pixel 7 Pro in conjunction with the Pixel Watch.

rpatel3001 commented 1 year ago

@Mausbiber there's no error message in your comment, but it doesn't matter because the apk is meant to be installed on the watch not the phone. I can't say if the pixel watch will work, I've only tried on the galaxy watch 4.

Mausbiber commented 1 year ago

okay, trying to sideload

Mausbiber commented 1 year ago

Okay, I installed the app on the Pixel Watch via adb. I can also start the app and the "useless" GUI appears. But that's it, I'm not asked for rights and no iBeacon is sent.

dshokouhi commented 1 year ago

Hey guys as this app you are installing is not the HA android app I am going to ask you to please move this conversation to the appropriate location.

soloam commented 1 year ago

This would be a great addition, nothing better to make tracking that a watch!

garv3 commented 1 year ago

Still waiting!

Scags104 commented 1 year ago

@maartendamen there is an apk available here. The UI doesn't actually do anything, the UUID and reference power are hard coded. You can clone the repo and build in Android Studio if you need to change those values.

This saved me from returning my 300$ pixel watch. thank you. this is awesome.

dshokouhi commented 1 year ago

Hey everyone I submitted a draft PR to migrate over the sensors to get some early feedback. I see a lot of users are coming in this thread looking for a quick solution. As of now there are no settings or notification commands but the transmitter and monitor should function just like the phone app does. If possible please test and provide us some feedback regarding your usage and battery life and also if you feel that the other options are a necessity for your use case. Its important for us to get some feedback here to see if things are good as it stands or if we really need to take care of other considerations first.

You can grab the test debug wear APK from the below link, just download the archive and extract it:

https://github.com/home-assistant/android/actions/runs/3744776429

Note you may need to install the debug phone APK in order to login on the watch, both APKs can be found in the archive from above.

once done with the test you can leave it installed or uninstall it, the debug builds are designed to be installed side by side the production apps so they dont interfere with them.

Ulrar commented 1 year ago

Hey @dshokouhi, that's great news, thank you very much !

I've just installed it now and I'll test it over the next while, I'm going to need to setup presence tracking here to see if it works but I'll be off for a few days there so I'll work on that. For anyone else wanting to try this, you need to install the 'full' app on the phone, looks like the minimal one doesn't support wearos.

I'll update back once I've had a chance to test it out properly, thanks again !

soloam commented 1 year ago

Thank you @dshokouhi . Will you push this to the beta?

Tks

Ulrar commented 1 year ago

Battery drain doesn't look noticeably worst so far, but straight away I can already see what I imagine is a bug : image

It looks like it's regularly being picked up as a new device by HA, making it essentially not trackable it seems

dshokouhi commented 1 year ago

It looks like it's regularly being picked up as a new device by HA, making it essentially not trackable it seems

That's to be expected. You have to wait until it has 10 entries then HA core will combine then.

dshokouhi commented 1 year ago

Will you push this to the beta?

Not until after testing

dshokouhi commented 1 year ago

Battery drain doesn't look noticeably worst so far

It's not draining the watch immediately but it is draining faster with it enabled.

Ulrar commented 1 year ago

So after a couple of weeks, it looks like the debug version of the app uses about 0.2% of the battery, whereas the normal app (that I actually use with sensors and stuff) uses about 0.4. So I suppose we may be looking at an extra 0.2, which I personally haven't noticed on daily use.

As for the functionality itself it's been spotty for me, but then again the ESPHome bluetooth_proxy has been having a lot of issues since 2022.12 so it's probably not the app's fault. If the concern here for this preview was the battery use, IMHO, it's all good !

dshokouhi commented 1 year ago

@Ulrar just to confirm did you have just the transmitter sensor enabled or also the beacon monitor?

Ulrar commented 1 year ago

Just the BLE transmitor, that shows up as an ibeacon in HA. (BTW it did merge after a while, all good. I've never used one before so I didn't know)

rpatel3001 commented 1 year ago

I don't have battery numbers as I've just pulled mine off the charger but I'll try and remember to look before charging next. It doesn't seem to have had a large affect, and I'm sure that adding the settings for lower power/less frequent broadcasts would give an even lower impact.

dshokouhi commented 1 year ago

I think its a positive thing that nobody has came here to say the watch died over night when they went to bed with the battery at 100%

is everyones intention then to leave it on all the time? Would a tile to control the state of the transmitter be a good idea or do you think it woudl not get used?

rpatel3001 commented 1 year ago

A way to enable/disable transmitting through automations, like the phone app transmitter, would be ideal.

dshokouhi commented 1 year ago

yea that will come with notifications once the watch gains support for them directly, that would actually negate the need to control settings on the watch as we have commands to control all the settings for this sensor :)

That is actually a separate feature request 😁

soloam commented 1 year ago

Since it's not possible to turn on and off by notification, I would leave it on all day long!

dshokouhi commented 1 year ago

Thanks for the feedback everyone! I went ahead and marked my PR as ready for review. Also updated the docs to mention sensor settings are not currently supported yet so all we can do is enable or disable these sensors.

rpatel3001 commented 1 year ago

Another battery data point: after 48 hours my watch drained from 100% to 5%. The blue app without BLE reports 0.5% usage and the red app with transmitting enabled the whole time reports 0.7% usage.

Scags104 commented 1 year ago

installed latest betas with this merged. is there a way to change the transmit power from ultra low that im not aware of? Watch app doesn't seem to have the settings behind each sensor like the phone app?

Not seeing it broadcasting as an iBeacon with MQTT explorer.

Thanks

dshokouhi commented 1 year ago

@Scags104 it's been mentioned a few times in this issue that sensor settings do not work on wear os. Also mentioned in the docs.

soloam commented 1 year ago

@dshokouhi is this in Beta? I'm a beta user and still didn't received the update!

Thank you for all your work on this

dshokouhi commented 1 year ago

Beta gets pushed every Saturday night to play store then waits for review before being available. You can grab the APK off the GitHub release page and install it now.

Mausbiber commented 1 year ago

Where did i find the apk? Can someone please provide me link to the beta apk?