iOS/Sources/Extensions/AppIntents/Script/ScriptAppIntent.swift
Bruno Pantaleão Gonçalves 1da8113507
Sync network info before performing intents (#4147)
Added calls to syncNetworkInformation() at the start of various
perform() methods for AppIntents and widget controls to ensure
up-to-date connectivity data before executing actions. Also refactored
ConnectivityWrapper to support async network info synchronization.

<!-- 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. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-29 12:50:47 +00:00

155 lines
5.4 KiB
Swift

import AppIntents
import Foundation
import PromiseKit
import SFSafeSymbols
import Shared
import SwiftUI
@available(iOS 16.4, *)
final class ScriptAppIntent: AppIntent {
static let title: LocalizedStringResource = .init("widgets.script.description.title", defaultValue: "Run Script")
@Parameter(title: LocalizedStringResource("app_intents.scripts.script.title", defaultValue: "Run Script"))
var script: IntentScriptEntity
@Parameter(
title: LocalizedStringResource(
"app_intents.notify_when_run.title",
defaultValue: "Notify when run"
),
description: LocalizedStringResource(
"app_intents.notify_when_run.description",
defaultValue: "Shows notification after executed"
),
default: true
)
var showConfirmationNotification: Bool
@Parameter(
title: LocalizedStringResource(
"app_intents.scripts.haptic_confirmation.title",
defaultValue: "Haptic confirmation"
),
default: false
)
var hapticConfirmation: Bool
func perform() async throws -> some IntentResult & ReturnsValue<Bool> {
await Current.connectivity.syncNetworkInformation()
if hapticConfirmation {
AppIntentHaptics.notify()
}
let success: Bool = try await withCheckedThrowingContinuation { continuation in
guard let server = Current.servers.all.first(where: { $0.identifier.rawValue == script.serverId }),
let api = Current.api(for: server) else {
continuation.resume(returning: false)
return
}
api.turnOnScript(scriptEntityId: script.entityId, triggerSource: .AppIntent)
.pipe { [weak self] result in
switch result {
case .fulfilled:
continuation.resume(returning: true)
case let .rejected(error):
Current.Log
.error(
"Failed to execute script from ScriptAppIntent, name: \(String(describing: self?.script.displayString)), error: \(error.localizedDescription)"
)
continuation.resume(returning: false)
}
}
}
if showConfirmationNotification {
Current.notificationDispatcher.send(.init(
id: .scriptAppIntentRun,
title: success ? L10n.AppIntents.Scripts.SuccessMessage.content(script.displayString) : L10n.AppIntents
.Scripts.FailureMessage.content(script.displayString)
))
}
DataWidgetsUpdater.update()
return .result(value: success)
}
}
@available(iOS 16.4, macOS 13.0, watchOS 9.0, *)
struct IntentScriptEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Script")
static let defaultQuery = IntentScriptAppEntityQuery()
var id: String
var entityId: String
var serverId: String
var serverName: String
var displayString: String
var iconName: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(displayString)")
}
init(
id: String,
entityId: String,
serverId: String,
serverName: String,
displayString: String,
iconName: String
) {
self.id = id
self.entityId = entityId
self.serverId = serverId
self.serverName = serverName
self.displayString = displayString
self.iconName = iconName
}
}
@available(iOS 16.4, macOS 13.0, watchOS 9.0, *)
struct IntentScriptAppEntityQuery: EntityQuery, EntityStringQuery {
func entities(for identifiers: [String]) async throws -> [IntentScriptEntity] {
getScriptEntities().flatMap(\.1).filter { identifiers.contains($0.id) }
}
func entities(matching string: String) async throws -> IntentItemCollection<IntentScriptEntity> {
let scriptsPerServer = getScriptEntities()
return .init(sections: scriptsPerServer.map { (key: Server, value: [IntentScriptEntity]) in
.init(
.init(stringLiteral: key.info.name),
items: value.filter({ $0.displayString.lowercased().contains(string.lowercased()) })
)
})
}
func suggestedEntities() async throws -> IntentItemCollection<IntentScriptEntity> {
let scriptsPerServer = getScriptEntities()
return .init(sections: scriptsPerServer.map { (key: Server, value: [IntentScriptEntity]) in
.init(.init(stringLiteral: key.info.name), items: value)
})
}
private func getScriptEntities(matching string: String? = nil) -> [(Server, [IntentScriptEntity])] {
var scriptEntities: [(Server, [IntentScriptEntity])] = []
let entities = ControlEntityProvider(domains: [.script]).getEntities(matching: string)
for (server, values) in entities {
scriptEntities.append((server, values.map({ entity in
IntentScriptEntity(
id: entity.id,
entityId: entity.entityId,
serverId: entity.serverId,
serverName: server.info.name,
displayString: entity.name,
iconName: entity.icon ?? SFSymbol.applescriptFill.rawValue
)
})))
}
return scriptEntities
}
}