Files
iOS/Sources/Extensions/Widgets/Controls/Fan/ControlFanValueProvider.swift
Bruno Pantaleão Gonçalves 12aa9c9849 Provide areas and device context for all extensions entity pickers (#4756)
<!-- 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: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 13:24:37 +02:00

116 lines
3.8 KiB
Swift

import AppIntents
import Foundation
import SFSafeSymbols
import Shared
import WidgetKit
@available(iOS 18, *)
struct ControlFanValueProvider: AppIntentControlValueProvider {
func currentValue(configuration: ControlFanConfiguration) async throws -> ControlEntityItem {
try await ControlRefreshDelay.wait()
guard let serverId = configuration.fan?.serverId,
let fanId = configuration.fan?.entityId,
let state: String = try await ControlEntityProvider(domains: [.fan]).currentState(
serverId: serverId,
entityId: fanId
) else {
throw AppIntentError.restartPerform
}
let isOn = state == ControlEntityProvider.States.on.rawValue
let icon = isOn ? configuration.icon : configuration.offStateIcon
return item(
fan: configuration.fan,
value: isOn,
iconName: icon,
displayText: configuration.displayText
)
}
func placeholder(for configuration: ControlFanConfiguration) -> ControlEntityItem {
item(
fan: configuration.fan,
value: nil,
iconName: configuration.icon,
displayText: configuration.displayText
)
}
func previewValue(configuration: ControlFanConfiguration) -> ControlEntityItem {
item(
fan: configuration.fan,
value: nil,
iconName: configuration.icon,
displayText: configuration.displayText
)
}
private func item(
fan: IntentFanEntity?,
value: Bool?,
iconName: SFSymbolEntity?,
displayText: String?
) -> ControlEntityItem {
let placeholder = placeholder(value: value)
if let fan {
return .init(
id: fan.id,
entityId: fan.entityId,
serverId: fan.serverId,
name: displayText ?? fan.displayString,
icon: iconName ?? .init(id: placeholder.iconName),
value: value ?? false
)
} else {
return .init(
id: placeholder.id,
entityId: placeholder.entityId,
serverId: placeholder.serverId,
name: displayText ?? placeholder.displayString,
icon: .init(id: placeholder.iconName),
value: false
)
}
}
private func placeholder(value: Bool?) -> IntentFanEntity {
.init(
id: UUID().uuidString,
entityId: "",
serverId: "",
displayString: L10n.Widgets.Controls.Fan.pendingConfiguration,
iconName: (value ?? false) ? SFSymbol.fanFill.rawValue : SFSymbol.fan.rawValue
)
}
}
@available(iOS 18.0, *)
struct ControlFanConfiguration: ControlConfigurationIntent {
static var title: LocalizedStringResource = .init(
"widgets.controls.fan.description",
defaultValue: "Turn on/off your fan"
)
@Parameter(
title: .init("app_intents.server.title", defaultValue: "Server")
)
var server: IntentServerAppEntity?
@Parameter(
title: .init("app_intents.fan.title", defaultValue: "Fan")
)
var fan: IntentFanEntity?
@Parameter(
title: .init("app_intents.fan.on_state_icon.title", defaultValue: "Icon for on state"),
default: SFSymbolEntity(id: SFSymbol.fanFill.rawValue)
)
var icon: SFSymbolEntity?
@Parameter(
title: .init("app_intents.fan.off_state_icon.title", defaultValue: "Icon for off state"),
default: SFSymbolEntity(id: SFSymbol.fan.rawValue)
)
var offStateIcon: SFSymbolEntity?
@Parameter(
title: .init("app_intents.display_text.title", defaultValue: "Display Text")
)
var displayText: String?
}