mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-18 21:51:51 -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 --> First iteration of the widget builder, user is able to choose what entities to display, choose a display text for them, color customization and "on tap" action. ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> https://github.com/user-attachments/assets/5949322a-a8df-4b3d-aaab-d4ba3853cc84 ## 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. -->
83 lines
2.9 KiB
Swift
83 lines
2.9 KiB
Swift
import AppIntents
|
|
import Foundation
|
|
import SFSafeSymbols
|
|
import Shared
|
|
import WidgetKit
|
|
|
|
@available(iOS 18, *)
|
|
struct ControlSwitchValueProvider: AppIntentControlValueProvider {
|
|
func currentValue(configuration: ControlSwitchConfiguration) async throws -> ControlEntityItem {
|
|
try await ControlRefreshDelay.wait()
|
|
guard let serverId = configuration.entity?.serverId,
|
|
let switchId = configuration.entity?.entityId,
|
|
let state = try await ControlEntityProvider(domains: [.switch]).currentState(
|
|
serverId: serverId,
|
|
entityId: switchId
|
|
) else {
|
|
throw AppIntentError.restartPerform
|
|
}
|
|
|
|
let isOn = state == ControlEntityProvider.States.on.rawValue
|
|
|
|
return item(entity: configuration.entity, value: isOn, iconName: configuration.icon)
|
|
}
|
|
|
|
func placeholder(for configuration: ControlSwitchConfiguration) -> ControlEntityItem {
|
|
item(entity: configuration.entity, value: nil, iconName: configuration.icon)
|
|
}
|
|
|
|
func previewValue(configuration: ControlSwitchConfiguration) -> ControlEntityItem {
|
|
item(entity: configuration.entity, value: nil, iconName: configuration.icon)
|
|
}
|
|
|
|
private func item(entity: IntentSwitchEntity?, value: Bool?, iconName: SFSymbolEntity?) -> ControlEntityItem {
|
|
let placeholder = placeholder(value: value)
|
|
if let entity {
|
|
return .init(
|
|
id: entity.id,
|
|
entityId: entity.entityId,
|
|
serverId: entity.serverId,
|
|
name: entity.displayString,
|
|
icon: iconName ?? .init(id: placeholder.iconName),
|
|
value: value ?? false
|
|
)
|
|
} else {
|
|
return .init(
|
|
id: placeholder.id,
|
|
entityId: placeholder.entityId,
|
|
serverId: placeholder.serverId,
|
|
name: placeholder.displayString,
|
|
icon: .init(id: placeholder.iconName),
|
|
value: false
|
|
)
|
|
}
|
|
}
|
|
|
|
private func placeholder(value: Bool?) -> IntentLightEntity {
|
|
.init(
|
|
id: UUID().uuidString,
|
|
entityId: "",
|
|
serverId: "",
|
|
displayString: L10n.Widgets.Controls.Switch.placeholderTitle,
|
|
iconName: (value ?? false) ? SFSymbol.lightswitchOnFill.rawValue : SFSymbol.lightswitchOffFill.rawValue
|
|
)
|
|
}
|
|
}
|
|
|
|
@available(iOS 18.0, *)
|
|
struct ControlSwitchConfiguration: ControlConfigurationIntent {
|
|
static var title: LocalizedStringResource = .init(
|
|
"widgets.controls.switch.description",
|
|
defaultValue: "Turn on/off switch"
|
|
)
|
|
|
|
@Parameter(
|
|
title: .init("widgets.controls.switch.title", defaultValue: "Switch")
|
|
)
|
|
var entity: IntentSwitchEntity?
|
|
@Parameter(
|
|
title: .init("app_intents.scripts.icon.title", defaultValue: "Icon")
|
|
)
|
|
var icon: SFSymbolEntity?
|
|
}
|