Files
iOS/Sources/Shared/Intents/RenderTemplateIntentHandler.swift
Bruno Pantaleão Gonçalves 2896f4a454 Assist improvements and revert optional connection access (#3201)
<!-- 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. -->

## 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-11-27 15:42:33 +01:00

77 lines
2.7 KiB
Swift

import Foundation
import Intents
import PromiseKit
import UIKit
class RenderTemplateIntentHandler: NSObject, RenderTemplateIntentHandling {
typealias Intent = RenderTemplateIntent
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)
}
func resolveTemplate(
for intent: Intent,
with completion: @escaping (INStringResolutionResult) -> Void
) {
if let templateStr = intent.template, templateStr.isEmpty == false {
Current.Log.info("using provided '\(templateStr)'")
completion(.success(with: templateStr))
} else {
Current.Log.info("requesting a value")
completion(.needsValue())
}
}
func handle(intent: Intent, completion: @escaping (RenderTemplateIntentResponse) -> Void) {
guard let templateStr = intent.template else {
Current.Log.error("Unable to unwrap intent.template")
let resp = RenderTemplateIntentResponse(code: .failure, userActivity: nil)
resp.error = "Unable to unwrap intent.template"
completion(resp)
return
}
guard let server = Current.servers.server(for: intent) else {
completion(.failure(error: "no server selected"))
return
}
Current.Log.verbose("Rendering template \(templateStr)")
Current.api(for: server)?.connection.subscribe(
to: .renderTemplate(templateStr),
initiated: { result in
if case let .failure(error) = result {
Current.Log.error("Error when rendering template in intent \(error)")
let resp = RenderTemplateIntentResponse(code: .failure, userActivity: nil)
completion(resp)
}
}, handler: { token, data in
token.cancel()
Current.Log.verbose("Successfully renderedTemplate")
let resp = RenderTemplateIntentResponse(code: .success, userActivity: nil)
resp.renderedTemplate = String(describing: data.result)
completion(resp)
}
)
}
}