From 8f125d7f5b5b90ad54fcaa46ec888329f19bb1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Pantale=C3=A3o=20Gon=C3=A7alves?= Date: Tue, 7 May 2024 11:08:56 +0200 Subject: [PATCH] Define initial Assist widget configuration to use preferred pipeline (#2763) ## Summary Instead of leaving the widget in a "configuration pending" mode, this PR sets the preferred pipeline and first server available as the initial widget config ## Screenshots ## Link to pull request in Documentation repository Documentation: home-assistant/companion.home-assistant# ## Any other notes --- .../Resources/en.lproj/Localizable.strings | 3 ++- .../Widgets/Assist/WidgetAssistProvider.swift | 24 ++++++++++++++++--- .../AssistInAppIntentHandler.swift | 15 ++++++++---- .../Shared/Resources/Swiftgen/Strings.swift | 6 +++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Sources/App/Resources/en.lproj/Localizable.strings b/Sources/App/Resources/en.lproj/Localizable.strings index f461958ed..457e5b662 100644 --- a/Sources/App/Resources/en.lproj/Localizable.strings +++ b/Sources/App/Resources/en.lproj/Localizable.strings @@ -58,6 +58,7 @@ "app_intents.perform_action.response_failure" = "Failed: %@"; "app_intents.perform_action.response_success" = "Done"; "app_intents.widget_action.actions_parameter_configuration" = "Which actions?"; +"app_intents.assist.preferred_pipeline.title" = "Preferred"; "assist.pipelines_picker.title" = "Assist Pipelines"; "cancel_label" = "Cancel"; "carPlay.action.intro.item.body" = "Tap to continue on your iPhone"; @@ -852,4 +853,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"; \ No newline at end of file +"yes_label" = "Yes"; diff --git a/Sources/Extensions/Widgets/Assist/WidgetAssistProvider.swift b/Sources/Extensions/Widgets/Assist/WidgetAssistProvider.swift index 89df46685..db02266c6 100644 --- a/Sources/Extensions/Widgets/Assist/WidgetAssistProvider.swift +++ b/Sources/Extensions/Widgets/Assist/WidgetAssistProvider.swift @@ -14,13 +14,31 @@ struct WidgetAssistProvider: IntentTimelineProvider { typealias Intent = AssistInAppIntent typealias Entry = WidgetAssistEntry + private let defaultEntry = { + var intentServer: IntentServer? = { + if let server = Current.servers.all.first { + return IntentServer(server: server) + } else { + return nil + } + }() + return WidgetAssistEntry( + server: intentServer, + pipeline: IntentAssistPipeline( + identifier: "0", + display: L10n.AppIntents.Assist.PreferredPipeline.title + ), + withVoice: .init(true) + ) + }() + func placeholder(in context: Context) -> WidgetAssistEntry { .init() } func getSnapshot(for configuration: Intent, in context: Context, completion: @escaping (Entry) -> Void) { guard let server = configuration.server, let pipeline = configuration.pipeline else { - completion(.init()) + completion(defaultEntry) return } let entry = WidgetAssistEntry( @@ -34,8 +52,8 @@ struct WidgetAssistProvider: IntentTimelineProvider { func getTimeline(for configuration: Intent, in context: Context, completion: @escaping (Timeline) -> Void) { completion(.init(entries: [ WidgetAssistEntry( - server: configuration.server, - pipeline: configuration.pipeline, + server: configuration.server ?? defaultEntry.server, + pipeline: configuration.pipeline ?? defaultEntry.pipeline, withVoice: Bool(truncating: configuration.withVoice ?? 1) ), ], policy: .never)) diff --git a/Sources/Shared/Intents/AssistInApp/AssistInAppIntentHandler.swift b/Sources/Shared/Intents/AssistInApp/AssistInAppIntentHandler.swift index 272a7c58d..761d644ae 100644 --- a/Sources/Shared/Intents/AssistInApp/AssistInAppIntentHandler.swift +++ b/Sources/Shared/Intents/AssistInApp/AssistInAppIntentHandler.swift @@ -56,8 +56,15 @@ class AssistInAppIntentHandler: NSObject, AssistInAppIntentHandling { for intent: AssistInAppIntent, with completion: @escaping (INObjectCollection?, (any Error)?) -> Void ) { + var outputPipelines = [ + IntentAssistPipeline( + identifier: "0", + display: L10n.AppIntents.Assist.PreferredPipeline.title + ), + ] + guard let server = Current.servers.server(for: intent) else { - completion(nil, nil) + completion(.init(items: outputPipelines), nil) return } @@ -66,10 +73,10 @@ class AssistInAppIntentHandler: NSObject, AssistInAppIntentHandling { completion(nil, nil) return } - let result: [IntentAssistPipeline] = pipelines.map { pipeline in - IntentAssistPipeline(identifier: pipeline.id, display: pipeline.name) + for pipeline in pipelines { + outputPipelines.append(IntentAssistPipeline(identifier: pipeline.id, display: pipeline.name)) } - completion(.init(items: result), nil) + completion(.init(items: outputPipelines), nil) } } diff --git a/Sources/Shared/Resources/Swiftgen/Strings.swift b/Sources/Shared/Resources/Swiftgen/Strings.swift index d53b8c122..7384e6604 100644 --- a/Sources/Shared/Resources/Swiftgen/Strings.swift +++ b/Sources/Shared/Resources/Swiftgen/Strings.swift @@ -252,6 +252,12 @@ public enum L10n { } public enum AppIntents { + public enum Assist { + public enum PreferredPipeline { + /// Preferred + public static var title: String { return L10n.tr("Localizable", "app_intents.assist.preferred_pipeline.title") } + } + } public enum PerformAction { /// Which action? public static var actionParameterConfiguration: String { return L10n.tr("Localizable", "app_intents.perform_action.action_parameter_configuration") }