iOS/Sources/Extensions/AppIntents/WidgetActionsAppIntent.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

53 lines
1.8 KiB
Swift

import AppIntents
import Foundation
import Shared
@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
struct WidgetActionsAppIntent: AppIntent, WidgetConfigurationIntent, CustomIntentMigratedAppIntent {
static let intentClassName = "WidgetActionsIntent"
static let title: LocalizedStringResource = .init("widgets.actions.title", defaultValue: "Actions")
static let description = IntentDescription(
.init("widgets.actions.description", defaultValue: "Perform Home Assistant actions.")
)
// ATTENTION: Unfortunately these sizes below can't be retrieved dynamically from widget family sizes.
// Check ``WidgetFamilySizes.swift`` as source of truth
@Parameter(
title: .init("widgets.actions.parameters.action", defaultValue: "Action"),
size: [
.systemSmall: 3,
.systemMedium: 6,
.systemLarge: 12,
.systemExtraLarge: 20,
.accessoryInline: 1,
.accessoryCorner: 1,
.accessoryCircular: 1,
.accessoryRectangular: 2,
]
)
var actions: [IntentActionAppEntity]?
static var parameterSummary: some ParameterSummary {
Summary()
}
func perform() async throws -> some IntentResult {
await Current.connectivity.syncNetworkInformation()
guard let actions else { return .result() }
for action in actions {
let intent = PerformAction()
intent.action = action
let _ = try await intent.perform()
}
return .result()
}
}
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
private extension IntentDialog {
static var actionsParameterConfiguration: Self {
.init(stringLiteral: L10n.AppIntents.WidgetAction.actionsParameterConfiguration)
}
}