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

Assist for Apple Watch #2825

Merged
merged 16 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 136 additions & 6 deletions HomeAssistant.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

87 changes: 4 additions & 83 deletions Sources/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

private var watchCommunicatorService: WatchCommunicatorService?

func application(
_ application: UIApplication,
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
Expand Down Expand Up @@ -348,83 +350,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

private func setupWatchCommunicator() {
Current.servers.add(observer: self)

// This directly mutates the data structure for observations to avoid race conditions.

Communicator.State.observations.store[.init(queue: .main)] = { state in
Current.Log.verbose("Activation state changed: \(state)")
_ = HomeAssistantAPI.SyncWatchContext()
}

WatchState.observations.store[.init(queue: .main)] = { watchState in
Current.Log.verbose("Watch state changed: \(watchState)")
_ = HomeAssistantAPI.SyncWatchContext()
}

Reachability.observations.store[.init(queue: .main)] = { reachability in
Current.Log.verbose("Reachability changed: \(reachability)")
}

InteractiveImmediateMessage.observations.store[.init(queue: .main)] = { message in
Current.Log.verbose("Received message: \(message.identifier)")

// TODO: move all these to something more strongly typed

if message.identifier == "ActionRowPressed" {
Current.Log.verbose("Received ActionRowPressed \(message) \(message.content)")
let responseIdentifier = "ActionRowPressedResponse"

guard let actionID = message.content["ActionID"] as? String,
let action = Current.realm().object(ofType: Action.self, forPrimaryKey: actionID),
let server = Current.servers.server(for: action) else {
Current.Log.warning("ActionID either does not exist or is not a string in the payload")
message.reply(.init(identifier: responseIdentifier, content: ["fired": false]))
return
}

firstly {
Current.api(for: server).HandleAction(actionID: actionID, source: .Watch)
}.done {
message.reply(.init(identifier: responseIdentifier, content: ["fired": true]))
}.catch { err in
Current.Log.error("Error during action event fire: \(err)")
message.reply(.init(identifier: responseIdentifier, content: ["fired": false]))
}
} else if message.identifier == "PushAction" {
Current.Log.verbose("Received PushAction \(message) \(message.content)")
let responseIdentifier = "PushActionResponse"

if let infoJSON = message.content["PushActionInfo"] as? [String: Any],
let info = Mapper<HomeAssistantAPI.PushActionInfo>().map(JSON: infoJSON),
let serverIdentifier = message.content["Server"] as? String,
let server = Current.servers.server(forServerIdentifier: serverIdentifier) {
Current.backgroundTask(withName: "watch-push-action") { _ in
firstly {
Current.api(for: server).handlePushAction(for: info)
}.ensure {
message.reply(.init(identifier: responseIdentifier))
}
}.catch { error in
Current.Log.error("error handling push action: \(error)")
}
}
}
}

Blob.observations.store[.init(queue: .main)] = { blob in
Current.Log.verbose("Received blob: \(blob.identifier)")
}

Context.observations.store[.init(queue: .main)] = { context in
Current.Log.verbose("Received context: \(context.content.keys) \(context.content)")

if let modelIdentifier = context.content["watchModel"] as? String {
Current.crashReporter.setUserProperty(value: modelIdentifier, name: "PairedAppleWatch")
}
}

_ = Communicator.shared
watchCommunicatorService = WatchCommunicatorService()
watchCommunicatorService?.setup()
}

func setupLocalization() {
Expand Down Expand Up @@ -481,9 +408,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// swiftlint:disable:next file_length
}

extension AppDelegate: ServerObserver {
func serversDidChange(_ serverManager: ServerManager) {
_ = HomeAssistantAPI.SyncWatchContext()
}
}
14 changes: 0 additions & 14 deletions Sources/App/Assist/AssistChatItem.swift

This file was deleted.

3 changes: 2 additions & 1 deletion Sources/App/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"app_intents.widget_action.actions_parameter_configuration" = "Which actions?";
"assist.error.pipelines_response" = "Failed to obtain Assist pipelines, please check your pipelines configuration.";
"assist.pipelines_picker.title" = "Assist Pipelines";
"assist.watch.mic_button.title" = "Tap to ";
"cancel_label" = "Cancel";
"carPlay.action.intro.item.body" = "Tap to continue on your iPhone";
"carPlay.action.intro.item.title" = "Create your first action";
Expand Down Expand Up @@ -854,4 +855,4 @@ Home Assistant is free and open source home automation software with a focus on
"widgets.open_page.description" = "Open a frontend page in Home Assistant.";
"widgets.open_page.not_configured" = "No Pages Available";
"widgets.open_page.title" = "Open Page";
"yes_label" = "Yes";
"yes_label" = "Yes";
Loading
Loading