-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathContactStateIntent.swift
More file actions
72 lines (60 loc) · 2.29 KB
/
ContactStateIntent.swift
File metadata and controls
72 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2010-2026 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0
import AppIntents
import OpenHABCore
enum ContactStateError: Error, CustomLocalizedStringResourceConvertible {
case itemNotInHome(String, String)
case commandFailed(String)
var localizedStringResource: LocalizedStringResource {
switch self {
case let .itemNotInHome(itemName, homeName):
"Item '\(itemName)' is not in home '\(homeName)'"
case let .commandFailed(message):
"Command failed: \(message)"
}
}
}
@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
struct ContactStateIntent: AppIntent {
static var allowedItemTypes: [OpenHABItem.ItemType] { [.contact] }
static var parameterSummary: some ParameterSummary {
Summary("Set the state of \(\.$itemEntity) to \(\.$state)") {
\.$home
}
}
static let title: LocalizedStringResource = "Set Contact State Value"
static let description = IntentDescription("Set the state of a contact open or closed")
@Parameter(title: "Home")
var home: Home
@Parameter(
title: "Item",
requestValueDialog: IntentDialog("Search for an item")
)
var itemEntity: ContactItemEntity
@Parameter(title: "State")
var state: ContactState
func perform() async throws -> some IntentResult & ProvidesDialog {
// Validate that the item belongs to the selected home
guard let homeId = UUID(uuidString: home.id), homeId == itemEntity.homeId else {
throw ContactStateError.itemNotInHome(itemEntity.label, home.displayString)
}
do {
try await OpenHABItemCache.instance.sendCommand(
to: itemEntity.item,
home: itemEntity.homeId,
command: state.rawValue
)
} catch {
throw ContactStateError.commandFailed(error.localizedDescription)
}
return .result(dialog: "The state of \(itemEntity.label) was set to \(state.rawValue)")
}
}