-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathGetItemStateIntent.swift
More file actions
59 lines (49 loc) · 1.82 KB
/
GetItemStateIntent.swift
File metadata and controls
59 lines (49 loc) · 1.82 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
// 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 ItemStateError: Error, CustomLocalizedStringResourceConvertible {
case itemNotInHome(String, String)
var localizedStringResource: LocalizedStringResource {
switch self {
case let .itemNotInHome(itemName, homeName):
"Item '\(itemName)' is not in home '\(homeName)'"
}
}
}
@available(iOS 17.0, macOS 14.0, watchOS 10.0, *)
struct GetItemStateIntent: AppIntent {
static var parameterSummary: some ParameterSummary {
Summary("Get \(\.$itemEntity) State") {
\.$home
}
}
static let title: LocalizedStringResource = "Get Item State"
static let description = IntentDescription("Retrieve the current state of an item")
@Parameter(title: "Home")
var home: Home
@Parameter(
title: "Item",
requestValueDialog: IntentDialog("Search for an item")
)
var itemEntity: GenericItemEntity
func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
// Validate that the item belongs to the selected home
guard let homeId = UUID(uuidString: home.id), homeId == itemEntity.homeId else {
throw ItemStateError.itemNotInHome(itemEntity.label, home.displayString)
}
let state = itemEntity.item.state ?? "Unknown state"
return .result(
value: state,
dialog: "The state of \(itemEntity.label) is \(state)"
)
}
}