Files
iOS/Sources/Shared/Intents/AppIntent/AssistInApp/AssistInAppIntentHandler.swift
Bruno Pantaleão Gonçalves aa2b4ebad3 iOS18 Assist control (#2972)
<!-- 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. -->
![CleanShot 2024-09-03 at 11 11
29@2x](https://github.com/user-attachments/assets/3551f526-a2e7-42ec-a5ee-732c3daec7e1)

## 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. -->
2024-09-03 16:51:49 +02:00

89 lines
2.9 KiB
Swift

import Intents
import ObjectMapper
import PromiseKit
class AssistInAppIntentHandler: NSObject, AssistInAppIntentHandling {
typealias Intent = AssistInAppIntent
// MARK: - Server
func resolveServer(for intent: Intent, with completion: @escaping (IntentServerResolutionResult) -> Void) {
if let server = Current.servers.server(for: intent) {
completion(.success(with: .init(server: server)))
} else {
completion(.needsValue())
}
}
func provideServerOptions(for intent: Intent, with completion: @escaping ([IntentServer]?, Error?) -> Void) {
completion(IntentServer.all, nil)
}
func provideServerOptionsCollection(
for intent: Intent,
with completion: @escaping (INObjectCollection<IntentServer>?, Error?) -> Void
) {
completion(.init(items: IntentServer.all), nil)
}
// MARK: - Pipeline
func resolvePipeline(
for intent: AssistInAppIntent,
with completion: @escaping (IntentAssistPipelineResolutionResult) -> Void
) {
guard let server = Current.servers.server(for: intent) else {
completion(.needsValue())
return
}
AssistService(server: server).fetchPipelines { response in
guard let pipelines = response?.pipelines else {
completion(.needsValue())
return
}
guard let result = pipelines.first(where: { pipeline in
pipeline.id == intent.pipeline?.identifier
}) else {
completion(.needsValue())
return
}
completion(.success(with: .init(identifier: result.id, display: result.name)))
}
}
func providePipelineOptionsCollection(
for intent: AssistInAppIntent,
with completion: @escaping (INObjectCollection<IntentAssistPipeline>?, (any Error)?) -> Void
) {
var outputPipelines = [
IntentAssistPipeline(
identifier: "0",
display: L10n.AppIntents.Assist.PreferredPipeline.title
),
]
guard let server = Current.servers.server(for: intent) else {
completion(.init(items: outputPipelines), nil)
return
}
AssistService(server: server).fetchPipelines { response in
guard let pipelines = response?.pipelines else {
completion(nil, nil)
return
}
for pipeline in pipelines {
outputPipelines.append(IntentAssistPipeline(identifier: pipeline.id, display: pipeline.name))
}
completion(.init(items: outputPipelines), nil)
}
}
// MARK: With Voice
func resolveWithVoice(for intent: AssistInAppIntent) async -> INBooleanResolutionResult {
INBooleanResolutionResult.success(with: intent.withVoice == 1 ? true : false)
}
}