-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatterTools.GetAllAttributesandEvents.groovy
110 lines (92 loc) · 4.77 KB
/
matterTools.GetAllAttributesandEvents.groovy
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
metadata {
definition (name: "Get All Attributes and Events", namespace: "matterTools", author: "jvm33") {
capability "Refresh"
capability "Configuration"
command "unsubscribeAll"
command "showStoredAttributeData"
// command "eventPaths"
}
preferences {
input(name:'txtEnable', type:'bool', title:'Enable additional description text logging', defaultValue:true)
input(name:'logEnable', type:'bool', title:'Enable debug logging', defaultValue:true)
input name: 'advancedOptions', type: 'bool', title: '<b>Advanced Options</b>', description: '<i>These advanced options should be already automatically set in an optimal way for your device...</i>', defaultValue: false
}
}
import hubitat.matter.DataType
import java.util.concurrent.*
import groovy.transform.Field
import groovy.json.JsonBuilder
@Field static ConcurrentHashMap globalDataStorage = new ConcurrentHashMap(32, 0.75, 1) // Intended to Store info. that does not change. Default is static
// Stores attribute values in nested ConcurrentHashMaps. Because this code retrieves many attributes at once, use ConcurrentHashMaps to ensure thread safety.
void storeRetrievedData(Map descMap){
String netId = device?.getDeviceNetworkId()
globalDataStorage.get(netId, new ConcurrentHashMap<String,ConcurrentHashMap>(8, 0.75, 1))
.get(descMap.endpoint, new ConcurrentHashMap<String,ConcurrentHashMap>(8, 0.75, 1))
.get(descMap.cluster, new ConcurrentHashMap<String,ConcurrentHashMap>(8, 0.75, 1))
.put(descMap.attrId, descMap.value)
}
// Retrieves a particular attribute from those previously received.
Object getStoredAttributeData(Map params = [:]){
Map inputs = [endpoint:null, cluster:null, attrId:null] << params
try {
assert inputs.keySet().containsAll(userInputs.keySet()) // check that all keys in userInputs are found in the inputs map, meaning the function was called with expected inputs.
assert inputs.endpoint instanceof String && inputs.endpoint.matches("[0-9A-F]+") // String must be a hex value.
assert inputs.cluster instanceof String && inputs.cluster.matches("[0-9A-F]+") // String must be a hex value.
assert inputs.attrId instanceof String && inputs.attrId.matches("[0-9A-F]+") // String must be a hex value.
} catch(AssertionError e) {
log.error "<pre>${e}"
throw(e)
}
String netId = device?.getDeviceNetworkId()
globalDataStorage.get(netId, new ConcurrentHashMap<String,ConcurrentHashMap>(8, 0.75, 1))
?.get(descMap.endpoint)
?.get(descMap.cluster)
?.get(descMap.attrId)
}
void showStoredAttributeData(){
String netId = device?.getDeviceNetworkId()
log.info "<pre> ${new JsonBuilder(globalDataStorage.get(netId)).toPrettyString()}"
// log.info globalDataStorage.get(netId)
}
def configure(){
subscribeAll()
}
def refresh(){
log.info "Refreshing all endpoints, all clusters, all attributes: "
String cmd = 'he rattrs [{"ep":"0xFFFF","cluster":"0xFFFFFFFF","attr":"0xFFFFFFFF"}]'
sendHubCommand(new hubitat.device.HubAction(cmd, hubitat.device.Protocol.MATTER))
}
void unsubscribeAll(){
String cmd = matter.unsubscribe()
log.info "Sending command to Unsubscribe from all attribute reports: " + cmd
sendHubCommand(new hubitat.device.HubAction(cmd, hubitat.device.Protocol.MATTER))
}
void subscribeAll(){
// This is a wildcard subscribe. Subscribes to all endpoints, all clusters, all attributes
String cmd = 'he subscribe 0x00 0xFF [{"ep":"0xFFFF","cluster":"0xFFFFFFFF","attr":"0xFFFFFFFF"}]'
log.info "Sending command to Subscribe for all attributes with a 0 second minimum time: " + cmd
sendHubCommand(new hubitat.device.HubAction(cmd, hubitat.device.Protocol.MATTER))
/*
cmd = 'he subscribe 0x00 0xFF [{"ep":"0xFFFF","cluster":"0xFFFFFFFF","evt":"0xFFFFFFFF"}]'
log.info "Sending command to Subscribe for all events with a 0 second minimum time: " + cmd
sendHubCommand(new hubitat.device.HubAction(cmd, hubitat.device.Protocol.MATTER))
*/
}
void eventPaths(){ // not currently used
List eventPaths = []
eventPaths.add(matter.eventPath("FFFF", 0xFFFF, 0xFFFF))
log.debug matter.subscribe(0, 0x00FF, eventPaths)
}
def parse(String description) {
Map descMap
try {
if (txtEnable) log.info "Parsing description string: <br>${description}"
descMap = matter.parseDescriptionAsMap(description)
descMap.put("endpointInt", (Integer.parseInt(descMap.endpoint, 16)))
if (txtEnable) log.info "Parsed description string as Map is: <br>${descMap}"
storeRetrievedData(descMap)
} catch (e) {
log.error "Caught error ${e} trying to parse descripiton string:<br>${description}"
throw(e)
}
}