Files
iOS/Sources/Extensions/Widgets/Controls/Cover/ControlCoverValueProvider.swift
Bruno Pantaleão Gonçalves c028a943da Added widget builder (#3354)
<!-- 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. -->
2025-01-27 16:16:24 +01:00

91 lines
3.3 KiB
Swift

import AppIntents
import Foundation
import SFSafeSymbols
import Shared
import WidgetKit
@available(iOS 18, *)
struct ControlCoverValueProvider: AppIntentControlValueProvider {
func currentValue(configuration: ControlCoverConfiguration) async throws -> ControlEntityItem {
try await ControlRefreshDelay.wait()
guard let serverId = configuration.entity?.serverId,
let lightId = configuration.entity?.entityId,
let state: String = try await ControlEntityProvider(domains: [.cover]).currentState(
serverId: serverId,
entityId: lightId
) else {
throw AppIntentError.restartPerform
}
let isOpen = [
ControlEntityProvider.States.open.rawValue,
ControlEntityProvider.States.opening.rawValue,
].contains(state)
let icon = isOpen ? configuration.openIcon : configuration.closedIcon
return item(entity: configuration.entity, value: isOpen, iconName: icon)
}
func placeholder(for configuration: ControlCoverConfiguration) -> ControlEntityItem {
item(entity: configuration.entity, value: nil, iconName: configuration.openIcon)
}
func previewValue(configuration: ControlCoverConfiguration) -> ControlEntityItem {
item(entity: configuration.entity, value: nil, iconName: configuration.openIcon)
}
private func item(entity: IntentCoverEntity?, 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?) -> IntentCoverEntity {
.init(
id: UUID().uuidString,
entityId: "",
serverId: "",
displayString: L10n.Widgets.Controls.Cover.placeholderTitle,
iconName: (value ?? false) ? SFSymbol.blindsVerticalOpen.rawValue : SFSymbol.blindsVerticalClosed.rawValue
)
}
}
@available(iOS 18.0, *)
struct ControlCoverConfiguration: ControlConfigurationIntent {
static var title: LocalizedStringResource = .init(
"widgets.controls.cover.description",
defaultValue: "Toggle cover"
)
@Parameter(
title: .init("widgets.controls.cover.title", defaultValue: "Cover")
)
var entity: IntentCoverEntity?
@Parameter(
title: .init("app_intents.open_state_icon.title", defaultValue: "Icon for open state"),
default: SFSymbolEntity(id: SFSymbol.curtainsOpen.rawValue)
)
var openIcon: SFSymbolEntity?
@Parameter(
title: .init("app_intents.closed_state_icon.title", defaultValue: "Icon for closed state"),
default: SFSymbolEntity(id: SFSymbol.curtainsClosed.rawValue)
)
var closedIcon: SFSymbolEntity?
}