home-assistant / android

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

Wear OS - Sensors - Add sleep and/or bedtime mode #2308

Closed Ulrar closed 1 year ago

Ulrar commented 2 years ago

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

Describe the solution you'd like Two new sensors :

Samsung's GW4 is very good at telling precisely when I fall asleep and wake up, and the latest version supposedly can trigger smart things automations based on that. I don't know if there's any way to get that information out of their services without using smart things but it'd be a very handy sensor.

A sensor on whether or not bedtime mode is enabled might be easier to implement, I don't know if that's a Samsung only feature or not but I could see automations trigger when I enable or disable it. Maybe turn off all the lights and enable alarms when enabling / going to sleep then disable the alarm when disabling it / waking up, or stuff like that.

Describe alternatives you've considered, if any None

Additional context None

dshokouhi commented 2 years ago

Are these sensors documented on the official Wear OS docs or is this a samsung specific feature?

Ulrar commented 2 years ago

I don't expect the sleep status to be, I believe that's a samsung health feature and I don't know if it's even possible to access. I certainly wouldn't know where to look for that.

For the bedtime mode again I don't know if that's a samsung feature or a wear os one, but AutoWear seems to be able to trigger based on it so presumably there is a way to get that status, see this reddit thread.

dshokouhi commented 2 years ago

For the bedtime mode again I don't know if that's a samsung feature or a wear os one, but AutoWear seems to be able to trigger based on it so presumably there is a way to get that status, see this reddit thread.

autowear mimics end user behavior to do what it needs to do, the app will not do that

if we cant find official developer documentation then these sensors may not get added.

Ulrar commented 2 years ago

I know essentially nothing about this, I'm just a user so nevermind if it can't be done I suppose, I was just hoping for it.

If and when #2183 gets implemented, room presence could be a decent alternative to know when users are in the bedroom at or after sleep time. So I guess I'll wait on that one instead.

Thanks

dshokouhi commented 2 years ago

If you have the phone app, then consider enabling the Sleep Confidence sensor. You may find it useful for this purpose :)

Personally I use sleep as android and retrieve those events in the house, as long as you use sleep tracking you can keep track of when the user is sleeping and what not.

Ulrar commented 2 years ago

I've tried the phone sleep sensor but I find it useless, even when I do have my phone near me. It sometimes thinks I'm asleep during the day, and is often hours off my actual sleep time at night. The samsung health watch in comparison, using the watch, is precise to a couple of minutes usually.

I've never tried sleep as android, looks like it has a wear os app. Worth a try, thanks !

terminusxx commented 2 years ago

@dshokouhi Doesn't the Health Services API's event state track when a user falls asleep already? I assume this will be available to be exposed to HA now that #2033 has officially been merged? Sorry if I've completely misread the API docs and am completely wrong! I love the work done so far on the project.

dshokouhi commented 2 years ago

@dshokouhi Doesn't the Health Services API's event state track when a user falls asleep already? I assume this will be available to be exposed to HA now that #2033 has officially been merged? Sorry if I've completely misread the API docs and am completely wrong! I love the work done so far on the project.

It might work but seems it only supports one device so a developer with that device will need to investigate. If it's anything like Google fit which requires the app to have the users API key hard codes then we probably can't do that in the app. Hard to tell if it requires that.

dshokouhi commented 1 year ago

Just a quick update here, does not look like we will be able to use the new Health Services API as it requires a minimum SDK version of 30 which means the app can only support Wear OS 3 devices in order to use that library. Still need to research bed time mode to see whats possible, as of now I don't see any official APIs so we may need to look at logcat and see if we can detect the changes to the toggle.

cvb941 commented 1 year ago

What's the problem, we can just have it enabled on 30+ devices only..

dshokouhi commented 1 year ago

What's the problem, we can just have it enabled on 30+ devices only..

In order to include the library as a dependency we have to change the minimum sdk version for the entire app.

cvb941 commented 1 year ago

We can create a new flavour with different minSdk and add the dependency to only one of them.

dshokouhi commented 1 year ago

Yea that is definitely doable, just means another flavor for the contributors to maintain.

One thing I do not see in the data types is that there is no reference to a user being asleep here.

Sleeping may be calculated by other means, unless I am not seeing it in the documentation for this?

Google does offer Health Connect however it is only available on phones and not on Wear OS.

@cvb941 have you found any documentation on bedtime mode detection?

jpelgrom commented 1 year ago

In order to include the library as a dependency we have to change the minimum sdk version for the entire app.

Have you tried ignoring/overriding the minimum SDK? As long as the library doesn't do auto initialization stuff I think it should be fine and we can work around it with version checks like for platform features.

dshokouhi commented 1 year ago

Have you tried ignoring/overriding the minimum SDK?

Thanks for pointing that out :) it does indeed build now without any errors. Unfortunately though I don't think this API has anything to do with sleep like this request was for originally.

dshokouhi commented 1 year ago

There seems to be multiple health APIs here. The originally linked API up above was actually for Health Platform which has been deprecated in favor of Health Connect however that only exists on the phone. For wearables there seems to be Health Services but that does not contain sleep data like Health Connect would. Its all a bit confusing. Health Services is in early alpha stages now and the dependency was last updated a year ago. Maybe its next update will contain the sleep data we are looking for?

Either way I think bedtime mode is unrelated to this. Either its controlled by an intent (🤞) or we have some setting config to read to get its value. I was waiting to look closer into this after we get the DND sensor and others added in.

dshokouhi commented 1 year ago

found some time to inspect logcat when bedtime mode is enabled and disabled. Now to hunt down where these properties might live 🤔 one thing to keep in mind is that bedtime mode is indeed linked to DND (at least as a zen_mode global setting)

2022-10-20 11:14:25.668   542-542   BedtimeModeController   pid-542                              D  Bedtime mode changed: enabled
2022-10-20 11:14:25.669   542-542   BedtimeModeController   pid-542                              D  Saved system states for properties: [Name=notification_filter|Current-value=1|Bedtime-on-value=2|Bedtime-off-value=1|Cached-value=1, Name=notification_policy|Current-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=none,suppressedVisualEffects=SUPPRESSED_EFFECT_SCREEN_OFF,SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,SUPPRESSED_EFFECT_LIGHTS,SUPPRESSED_EFFECT_PEEK,SUPPRESSED_EFFECT_AMBIENT,areChannelsBypassingDnd=false]|Bedtime-on-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_ANY,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]|Bedtime-off-value=null|Cached-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]]
2022-10-20 11:14:25.680   542-542   BedtimeModeController   pid-542                              D  configureBedtimeMode: enabled=true onBoot=false (properties state: [Name=notification_filter|Current-value=1|Bedtime-on-value=2|Bedtime-off-value=1|Cached-value=1, Name=notification_policy|Current-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=none,suppressedVisualEffects=SUPPRESSED_EFFECT_SCREEN_OFF,SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,SUPPRESSED_EFFECT_LIGHTS,SUPPRESSED_EFFECT_PEEK,SUPPRESSED_EFFECT_AMBIENT,areChannelsBypassingDnd=false]|Bedtime-on-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_ANY,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]|Bedtime-off-value=null|Cached-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]])
2022-10-20 11:14:25.691   542-542   BedtimeModeController   pid-542                              D  Property notification_filter is set to 2 due to entering bedtime mode.
2022-10-20 11:14:25.706   542-542   BedtimeModeController   pid-542                              D  Property notification_policy is set to NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_ANY,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true] due to entering bedtime mode.
2022-10-20 11:14:25.706   542-542   BedtimeMod...ontroller2 pid-542                              D  startEntryAnimation
2022-10-20 11:14:25.706   542-542   BedtimeModeUiHandler    pid-542                              D  setEntryViewVisible: visible=true; mEntryViewVisible=false
2022-10-20 11:14:27.808   542-542   BedtimeModeController   pid-542                              D  finalizeBedtimeEntry
2022-10-20 11:14:27.810   542-542   ContextImpl             pid-542                              W  Calling a method in the system process without a qualified user: android.app.ContextImpl.startActivity:993 com.google.android.clockwork.healthservices.BedtimeModeController.showHome:461 com.google.android.clockwork.healthservices.BedtimeModeController.finalizeBedtimeEntry:205 com.google.android.clockwork.healthservices.BedtimeModeController.lambda$pmtafElUcNPKMGV9wVfOPXW_FvQ:0 com.google.android.clockwork.healthservices.-$$Lambda$BedtimeModeController$pmtafElUcNPKMGV9wVfOPXW_FvQ.run:2 
2022-10-20 11:14:27.811   542-542   ContextImpl             pid-542                              W  Calling a method in the system process without a qualified user: android.app.ContextImpl.startActivity:1005 android.app.ContextImpl.startActivity:994 com.google.android.clockwork.healthservices.BedtimeModeController.showHome:461 com.google.android.clockwork.healthservices.BedtimeModeController.finalizeBedtimeEntry:205 com.google.android.clockwork.healthservices.BedtimeModeController.lambda$pmtafElUcNPKMGV9wVfOPXW_FvQ:0 
2022-10-20 11:14:28.313   542-542   BedtimeModeUiHandler    pid-542                              D  setEntryViewVisible: visible=false; mEntryViewVisible=true
2022-10-20 11:14:47.204   542-542   BedtimeModeController   pid-542                              D  Bedtime mode changed: disabled
2022-10-20 11:14:47.205   542-542   BedtimeModeController   pid-542                              D  configureBedtimeMode: enabled=false onBoot=false (properties state: [Name=notification_filter|Current-value=2|Bedtime-on-value=2|Bedtime-off-value=1|Cached-value=1, Name=notification_policy|Current-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_ANY,priorityConvSenders=none,suppressedVisualEffects=SUPPRESSED_EFFECT_SCREEN_OFF,SUPPRESSED_EFFECT_FULL_SCREEN_INTENT,SUPPRESSED_EFFECT_LIGHTS,SUPPRESSED_EFFECT_PEEK,SUPPRESSED_EFFECT_AMBIENT,areChannelsBypassingDnd=false]|Bedtime-on-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_ANY,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]|Bedtime-off-value=null|Cached-value=NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true]])
2022-10-20 11:14:47.205   542-542   BedtimeMod...ontroller2 pid-542                              D  stopEntryAnimation
2022-10-20 11:14:47.205   542-542   BedtimeMod...Controller pid-542                              D  haltExit
2022-10-20 11:14:47.219   542-542   BedtimeModeController   pid-542                              D  Property notification_filter is set to 1 due to exiting bedtime mode.
2022-10-20 11:14:47.225   542-542   BedtimeModeController   pid-542                              D  Property notification_policy is restored to NotificationManager.Policy[priorityCategories=PRIORITY_CATEGORY_ALARMS,PRIORITY_CATEGORY_MEDIA,PRIORITY_CATEGORY_MESSAGES,PRIORITY_CATEGORY_CALLS,PRIORITY_CATEGORY_REPEAT_CALLERS,PRIORITY_CATEGORY_CONVERSATIONS,priorityCallSenders=PRIORITY_SENDERS_STARRED,priorityMessageSenders=PRIORITY_SENDERS_STARRED,priorityConvSenders=unset,suppressedVisualEffects=,areChannelsBypassingDnd=true] due to exiting bedtime mode.
dshokouhi commented 1 year ago

ok tried to look in source but did not find it, took a shot in the dark at the Settings.Global key and found it as bedtime_mode

Will add this once we get DND sensor added because that sensor will help make this one update immediately, good news is theater mode will also be accessible. I know its not requested here but all this research got me to find that state too :)

Ulrar commented 1 year ago

That's great news, thanks for your work!