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

Would it be possible to trigger Apple Watch complication updates through automations. #1046

Closed
Doa132 opened this issue Sep 19, 2020 · 5 comments · Fixed by #1615
Closed
Labels
feature-request firebase-migration ios watch The Apple Watch app and complications
Milestone

Comments

@Doa132
Copy link

Doa132 commented Sep 19, 2020

There's another app called Complications (https://mikelyons.org/complicated/) which allows you to use webhooks to send text to your apple watch complication. It's still somewhat limited to every 5-10mins, but you can choose when it happens instead of having it update randomly, potentially just missing a sensor change.
At the moment, I use it to update a watch complication only when certain sensors change by using a shell command service to send a url encoded text. Sometimes that's multiple times an hour, other times far less. Occasionally, it won't update if there's been too many sensor changes within a short time or if I haven't opened the app on my phone in ages. But it makes my complication far more reliable than is currently available through the HA iOS app complications as I'm choosing when the update occurs.
It would be great if something similar could be implemented with the HA app complications. Would be nice to be able to cut out the middle app, especially since obviously pressing on the Complicated complication can't open the HA actions.

@zacwest
Copy link
Member

zacwest commented Sep 21, 2020

It is possible to update complications via a push notification, but one potential downside is going to be how often you can do it. The complications are limited to 50 total refreshes per day. That can be once per second for 50 seconds, total, or once every 30 minutes for 24 hours, or so. Right now the app prefers the latter.

@Doa132
Copy link
Author

Doa132 commented Sep 21, 2020

Even with the limit of 50 per day it could be more useful to have it change based on a sensor change. For example triggering a change only if your alarm system changes state. Mostly like wouldn’t happen often, but better to be able to see that it’s disarmed before you try open your door than to have it randomly update 20mins later when the state hasn’t changed.
It would just have to be made clear in the docs that there is a limit to how often it will update.

@zacwest zacwest added the watch The Apple Watch app and complications label Oct 11, 2020
@zacwest
Copy link
Member

zacwest commented Oct 11, 2020

Looks like this will require setting up a notify service specifically for the watch for watchOS 6+. In my reading of the docs the 50/day limit for push notifications may be separate from the periodic background uploading we do.

@zacwest
Copy link
Member

zacwest commented Oct 12, 2020

OK, so I've got this working locally, but it's going to be relatively complicated:

  1. This requires using PushKit, which is a separate (but compatible) push system to the normal APNs we use to deliver alerts.
  2. PushKit actually generates new device identifiers for just complication notifications.
  3. Firebase doesn't support PushKit notifications. That is, they don't allow you to just push to arbitrary Apple tokens and they have not done the effort to wrap PushKit.

A good amount of infrastructure work is going to be necessary to get this working, unfortunately.

@zacwest
Copy link
Member

zacwest commented Oct 12, 2020

for later, this registers with PushKit. the registration request also has credentials with the token in it as Data which is the device token to push to

diff --git a/Sources/Extensions/Watch/ExtensionDelegate.swift b/Sources/Extensions/Watch/ExtensionDelegate.swift
index 3c909ade..d69dccb7 100644
--- a/Sources/Extensions/Watch/ExtensionDelegate.swift
+++ b/Sources/Extensions/Watch/ExtensionDelegate.swift
@@ -14,6 +14,7 @@ import UserNotifications
 import XCGLogger
 import Shared
 import PromiseKit
+import PushKit
 
 class ExtensionDelegate: NSObject, WKExtensionDelegate {
     // MARK: Fileprivate
@@ -24,6 +25,8 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate {
         }
     }
 
+    var registry: AnyObject?
+
     // MARK: - WKExtensionDelegate -
 
     func applicationDidFinishLaunching() {
@@ -48,6 +51,13 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate {
 
         setupWatchCommunicator()
 
+        if #available(watchOSApplicationExtension 6.0, *) {
+            registry = with(PKPushRegistry(queue: nil)) {
+                $0.desiredPushTypes = [.complication]
+                $0.delegate = self
+            }
+        }
+
         // schedule the next background refresh
         Current.backgroundRefreshScheduler.schedule().cauterize()
     }
@@ -249,3 +269,17 @@ extension ExtensionDelegate: UNUserNotificationCenterDelegate {
         completionHandler([.alert, .badge, .sound])
     }
 }
+
+@available(watchOSApplicationExtension 6.0, *)
+extension ExtensionDelegate: PKPushRegistryDelegate {
+    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
+        Current.Log.info("push credentials \(pushCredentials) updated for type \(type)")
+    }
+
+    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
+        Current.Log.info("received incoming push: \(payload) for \(type)")
+        updateComplications().done {
+            completion()
+        }
+    }
+}

@zacwest zacwest added this to the 2021.6 milestone May 4, 2021
zacwest added a commit that referenced this issue May 11, 2021
Depends on home-assistant/mobile-apps-fcm-push#52. Fixes #1046, though not in the best way. However, this at least leaves it open to doing it better later.

## Summary
Adds support for triggering complication updates indirectly via background notifications to the iOS app, and adds a manual update button to the complication list.

## Screenshots
![Image](https://user-images.githubusercontent.com/74188/116802546-6c2cf980-aac8-11eb-913a-6b9df9153ede.png)

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#511

## Any other notes
This indirect method appears to be a bit slower than the direct method, but seems to mostly work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request firebase-migration ios watch The Apple Watch app and complications
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants