Skip to content

Commit

Permalink
Assist for Apple Watch (#2825)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

<img width="1102" alt="Screenshot 2024-06-25 at 13 34 21"
src="https://github.com/home-assistant/iOS/assets/5808343/52343848-0d50-4d76-a8a2-8eb2de52f492">


## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
  • Loading branch information
bgoncal authored Jul 2, 2024
1 parent a10c49a commit 69b0c26
Show file tree
Hide file tree
Showing 41 changed files with 1,564 additions and 186 deletions.
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

0 comments on commit 69b0c26

Please sign in to comment.