mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-18 11:15:36 -05:00
<!-- 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 --> This PR deprecate intents from siri intents definition and migrates them to App Intent. ## 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. -->
52 lines
1.8 KiB
Swift
52 lines
1.8 KiB
Swift
import AppIntents
|
|
import HAKit
|
|
import Shared
|
|
|
|
@available(iOS 16.4, *)
|
|
struct RenderTemplateAppIntent: AppIntent {
|
|
static var title: LocalizedStringResource = .init(
|
|
"app_intents.render_template.title",
|
|
defaultValue: "Render template"
|
|
)
|
|
|
|
static var description = IntentDescription(.init(
|
|
"app_intents.render_template.description",
|
|
defaultValue: "Render a Home Assistant template. Only users with the admin role can perform this action."
|
|
))
|
|
|
|
@Parameter(title: .init("app_intents.server.title", defaultValue: "Server"))
|
|
var server: IntentServerAppEntity
|
|
|
|
@Parameter(
|
|
title: .init("app_intents.render_template.template.title", defaultValue: "Template"),
|
|
default: "{{ now() }}"
|
|
)
|
|
var template: String
|
|
|
|
func perform() async throws -> some IntentResult & ReturnsValue<String> {
|
|
await Current.connectivity.syncNetworkInformation()
|
|
guard let server = server.getServer(),
|
|
let connection = Current.api(for: server)?.connection else {
|
|
throw ShortcutAppIntentError(L10n.AppIntents.Error.noServer)
|
|
}
|
|
|
|
let rendered = try await connection.renderTemplate(template)
|
|
return .result(value: rendered)
|
|
}
|
|
}
|
|
|
|
private extension HAConnection {
|
|
func renderTemplate(_ template: String) async throws -> String {
|
|
try await withCheckedThrowingContinuation { continuation in
|
|
subscribe(to: .renderTemplate(template), initiated: { result in
|
|
if case let .failure(error) = result {
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}, handler: { token, data in
|
|
token.cancel()
|
|
continuation.resume(returning: String(describing: data.result))
|
|
})
|
|
}
|
|
}
|
|
}
|