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

Enable persistent connection only when at home and disconnected from the internet #4167

Open
zhimsel opened this issue Jan 28, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@zhimsel
Copy link

zhimsel commented Jan 28, 2024

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

I'd like to save battery under normal circumstances by keeping the websocket connection disabled (relying on Firebase for notifications). However, one key feature that I like of the websocket connection (when on the same local network as my HA server) is the ability to get notifications from HA locally, without relying on any internet connections. This is very useful, for example, when the internet is down; I have an automation in HA to notify me when the internet is down.

The problem is that to do this, websocket connection must be enabled for this to work; but this uses more battery. Since I spend the majority of my time at home, it effectively stays on almost all the time, negating any battery savings from the "enable only when at home" setting.

Describe the solution you'd like

Would it be possible to add an option to enable the websocket when at home, but only when there's no external internet access available? I noticed this line in the Websocket Manager code; is this possible information to get from Android's API?

Describe alternatives you've considered, if any

If this is not possible, an alternative solution would be to use something like Tasker to do the "home, but no internet" detection. However, we'd need a way to inform the HA app to change the websocket setting EDIT: without relying on the HA server sending a message, which would not reach the device without an internet connection. Could we expose that setting through an intent receiver, like the io.homeassistant.companion.android.UPDATE_SENSORS intent?

In fact, even if the primary request above is fulfilled, adding this could a useful feature, anyway.

Additional context

@zhimsel zhimsel added the enhancement New feature or request label Jan 28, 2024
@dshokouhi
Copy link
Member

Would it be possible to add an option to enable the websocket when at home, but only when there's no external internet access available?

That's exactly what the setting does today. Have you tested it?

@dshokouhi
Copy link
Member

However, we'd need a way to inform the HA app to change the websocket setting.

https://companion.home-assistant.io/docs/notifications/notification-commands#persistent

@zhimsel
Copy link
Author

zhimsel commented Jan 28, 2024

Please re-read my OP carefully:

Would it be possible to add an option to enable the websocket when at home, but only when there's no external internet access available?

That's exactly what the setting does today. Have you tested it?

What I'm requesting is not the current behavior. Please note the emphasis in my quote above. And yes, I have tested it: up until very recently, I've had it set to "when at home", but I changed it to "never" to save battery. I'm very aware of the behavior of that setting.

However, we'd need a way to inform the HA app to change the websocket setting.

https://companion.home-assistant.io/docs/notifications/notification-commands#persistent

Sorry, I could've been more clear in my OP, but I figured the implications were obvious (I've edited my OP): this relies on the ability for the HA server to send a message to the device, which does not work without an internet connection or local websocket. Since the whole point of this message is to turn on the local websocket, this workflow is not really feasible.

I'm looking for a local (to the Android device) method to toggle the local websocket setting from another app (e.g. Tasker).

@dshokouhi
Copy link
Member

Please re-read my OP carefully:

Would it be possible to add an option to enable the websocket when at home, but only when there's no external internet access available?

That's exactly what the setting does today. Have you tested it?

What I'm requesting is not the current behavior. Please note the emphasis in my quote above. And yes, I have tested it: up until very recently, I've had it set to "when at home", but I changed it to "never" to save battery. I'm very aware of the behavior of that setting.

So one thing to consider is that the device itself may opt not to use any data on WiFi when it has no data but the device has cellular data. We print this data to the logs under server connection info if Wifi is actually being used for data. While you may see your device connected to it the OS will dictate where the data will travel to when Wifi has no active data.

I'm looking for a local (to the Android device) method to toggle the local websocket setting from another app (e.g. Tasker).

more than likely we will need to implement this for the request, handling the intent will be a bit tricky here as this setting is on a per server level and users do not need to be aware of the server ID. Handling this from notifications is a lot easier because we get the server ID from the received notification. Will need to think of a way for this to work however I am not sure if this option will work for you if the device does not use wifi for data.

@zhimsel
Copy link
Author

zhimsel commented Feb 5, 2024

So one thing to consider is that the device itself may opt not to use any data on WiFi when it has no data but the device has cellular data. We print this data to the logs under server connection info if Wifi is actually being used for data.

So if we already have access to this information (Wi-Fi on and connected, but not being used for data), we can action on it, no? The parameters of the decision to enable the websocket would be based on the intent of the user (as best we can guess, at least). In other words: the websocket will only be enabled (with my proposed setting "on") when the device is connected to Wi-Fi (and using it for data), but has no Internet connection. That could be exposed to the user as an additional option.

handling the intent will be a bit tricky here as this setting is on a per server level and users do not need to be aware of the server ID

Hmm, that's a good point. I didn't think about multi-server users. I'm only cursorally familiar with Android intents, but is it possible to pass additional data along with them?

I assume the websocket connection setting is per-connection and not global. This would make sense (and it's how I would design it), and the settings UI would indicate this (the setting is in the server config page).

@jpelgrom
Copy link
Member

jpelgrom commented Feb 5, 2024

So if we already have access to this information (Wi-Fi on and connected, but not being used for data), we can action on it, no?

It looks like #3961 will need to be implemented first for this to be useful and work like you expect.

I assume the websocket connection setting is per-connection and not global

That's mostly correct, it is managed per server.

is it possible to pass additional data along with them?

Yes, but server identifiers aren't user visible (and are not intended to be), so the app would need to do something like matching the name or server url to know which server you're trying to update. Another consideration here is security: exposing an intent system on the device like this should be restricted like how push notifications are restricted.

@zhimsel
Copy link
Author

zhimsel commented Feb 20, 2024

Another consideration here is security: exposing an intent system on the device like this should be restricted like how push notifications are restricted.

Hmm, that's a good point.

Well, hopefully a solution to this issue can be figured out. I'd love the functionality.

If I had the spare time and more knowledge about the Android SDK (or time to learn), I wouldn't mind submitting a PR for this. Unfortunately I'm already stretched thin as it is. Maybe one day, if I find myself with lots of free time 😬 In the mean time, this issue and my hope will have to suffice.

My thanks to anyone who works on this!

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

No branches or pull requests

3 participants