iOS/Sources/Extensions/Widgets/Controls/Automation/AutomationAppIntent.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

87 lines
3.0 KiB
Swift

import AppIntents
import Foundation
import PromiseKit
import Shared
import SwiftUI
@available(iOS 16.4, *)
final class AutomationAppIntent: AppIntent {
static let title: LocalizedStringResource = .init(
"widgets.automation.trigger.title",
defaultValue: "Trigger automation"
)
@Parameter(title: LocalizedStringResource(
"app_intents.automations.parameter.automation.title",
defaultValue: "Automation"
))
var automation: IntentAutomationEntity
@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.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 == automation.serverId }),
let api = Current.api(for: server) else {
continuation.resume(returning: false)
return
}
api.CallService(
domain: Domain.automation.rawValue,
service: Service.trigger.rawValue,
serviceData: ["entity_id": automation.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 automation from AutomationAppIntent, name: \(String(describing: self?.automation.displayString)), error: \(error.localizedDescription)"
)
continuation.resume(returning: false)
}
}
}
if showConfirmationNotification {
Current.notificationDispatcher.send(.init(
id: .automationAppIntentRun,
title: success ? L10n.AppIntents.Automations.SuccessMessage.content(automation.displayString) : L10n
.AppIntents
.Automations.FailureMessage.content(automation.displayString)
))
}
DataWidgetsUpdater.update()
return .result(value: success)
}
}