Files
iOS/Sources/Extensions/Widgets/Controls/Light/LightIntent.swift
Bruno Pantaleão Gonçalves 23edc7ce0a Fix scripts execution (#3731)
2025-07-16 11:28:10 +02:00

43 lines
1.4 KiB
Swift

import AppIntents
import Foundation
import Shared
@available(iOS 18, *)
struct LightIntent: SetValueIntent {
static var title: LocalizedStringResource = .init("app_intents.intent.light.title", defaultValue: "Control light")
@Parameter(title: .init("app_intents.lights.light.title", defaultValue: "Light"))
var light: IntentLightEntity
@Parameter(title: .init("app_intents.state.target", defaultValue: "Target state"))
var value: Bool
@Parameter(title: .init("app_intents.state.toggle", defaultValue: "Toggle"), default: false)
var toggle: Bool
func perform() async throws -> some IntentResult {
guard let server = Current.servers.all.first(where: { $0.identifier.rawValue == light.serverId }),
let connection = Current.api(for: server)?.connection else {
return .result()
}
var service = Service.toggle.rawValue
if !toggle {
service = value ? Service.turnOn.rawValue : Service.turnOff.rawValue
}
let _ = await withCheckedContinuation { continuation in
connection.send(.callService(
domain: .init(stringLiteral: Domain.light.rawValue),
service: .init(stringLiteral: service),
data: [
"entity_id": light.entityId,
]
)).promise.pipe { _ in
continuation.resume()
}
}
return .result()
}
}