-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoomScenes.groovy
271 lines (255 loc) · 8.94 KB
/
RoomScenes.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import com.hubitat.app.DeviceWrapper as DeviceWrapper
import com.hubitat.app.DeviceWrapperList as DeviceWrapperList
import com.hubitat.hub.domain.Event as Event
import com.hubitat.hub.domain.Hub as Hub
#include wesmc.UtilsLibrary
definition(
name: "RoomScenes",
namespace: 'wesmc',
author: 'Wesley M. Conner',
description: "Define and Execute RA2-aware Scenes for a Hubitat Room",
category: "", // Not supported as of Q3'23
iconUrl: "", // Not supported as of Q3'23
iconX2Url: "", // Not supported as of Q3'23
documentationLink: 'https://github.com/WesleyMConner/Hubitat-RoomScenes/README.adoc',
importUrl: 'https://github.com/WesleyMConner/Hubitat-RoomScenes.git'
)
preferences {
page(name: "monoPage", title: "", install: true, uninstall: true)
}
void addRoomObjToSettings(String heading) {
//-----------------------------------------------------------------------
// ABSTRACT
// Ask client to select a single room and save the whole room object
// as "state.roomObj". This method must be called from a Hubitat App
// page's section.
//
// DESIGN NOTES
// There may not be an import for defining a RoomWrapper or a
// RoomWrapperList.
//-----------------------------------------------------------------------
paragraph emphasis(heading)
ArrayList<LinkedHashMap> rooms = app.getRooms()
List<Map<String, String>> roomPicklist = rooms
.sort{ it.name }
.collect{ [(it.id.toString()): it.name] }
input(
name: 'roomId',
type: 'enum',
title: 'Select the Room',
submitOnChange: true,
required: true,
multiple: false,
options: roomPicklist
)
if (settings.roomId) {
state.roomObj = rooms.find{it.id.toString() == settings.roomId}
}
}
void addScenesToSettings (String heading) {
paragraph emphasis(heading)
paragraph emphasis2('Use Hubitat Modes to name Room Scenes <em>..(optional)</em>')
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! UNKNOWN IMPORT FOR ModeWrapper or ModeWrapperList !!!
// !!! Mode appears to have mode.id, mode.name, ... !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ArrayList<LinkedHashMap> modes = location.modes
state.modes = modes
List<String> locationNamePicklist = state.modes.collect{it.name}
input(
name: 'modesAsScenes',
type: 'enum',
title: 'Select the Modes',
submitOnChange: true,
required: false,
multiple: true,
options: locationNamePicklist
)
paragraph emphasis2('Create Custom Room Scene Name <em>..(optional)</em>')
for (int i = 1; i<9; i++) {
input(
name: "cust${i}",
type: 'text',
title: "Custom Name",
width: 3,
submitOnChange: true,
required: false,
defaultValue: 'n/a'
)
}
List<String> scenes = (
modesAsScenes.findAll{it}
+ [settings.cust1, settings.cust2, settings.cust3,
settings.cust4, settings.cust5, settings.cust6,
settings.cust7, settings.cust8].findAll{it && it != 'n/a'}
).sort{}
state.scenes = scenes
}
void addModeIdToSceneToSettings (String heading) {
// Abstract
// Ask client to select a scene for each current Hub mode and persist
// the results as "state.modeIdToScene" (a Map<String, String>).
//
// Design Notes
// - The mapping facilitates state.currentScene == 'AUTO'
// - Refresh the mapping if/when site modes are changed.
paragraph emphasis(heading)
Map<String, String> modeIdToRoomScene
ArrayList<LinkedHashMap> modes = location.modes
Map<String, String> modeIdToScene = [:]
modes.each{mode ->
Boolean modeNameIsSceneName = state.scenes.find{it == mode.name} ? true : false
input(
name: "${mode.id}ToScene",
type: 'enum',
title: "Scene for ${mode.name}",
width: 2,
submitOnChange: true,
required: true,
multiple: false,
options: state.scenes,
defaultValue: modeNameIsSceneName ? mode.name : ''
)
modeIdToScene[mode.id] = settings["${mode.id}ToScene"]
}
// Only promote mappings to state if there are zero remaining nulls.
Map<String, String> nullMappings = modeIdToScene.findAll{it.value == null}
state.modeIdToScene = nullMappings.size() == 0 ? modeIdToScene : [:]
}
Map monoPage() {
return dynamicPage(
name: "monoPage",
title: "",
install: true,
uninstall: true
) {
section {
app.updateLabel("${state?.roomObj?.name ?: 'TBD'} Room Scenes Instance")
// paragraph heading("${state.roomObj.name ?: 'TBD'} Room Scenes Instance") \
paragraph important('<br/>Tab to register field changes!')
addRoomObjToSettings('<b>Step 1:</b> Identify the Hubitat Room this instance controls')
if (state.roomObj) {
addScenesToSettings ("<b>Step 2:</b> Identify <b>${state.roomObj.name}</b> Room Scenes")
}
paragraph "<b>Current Scenes:</b> ${state.scenes.join(', ') ?: '...none...'}"
//---->if (state.scenes.size() < 2) {
//----> paragraph comment('At least two Room Scenes must be defined in order to proceed.')
//---->} else {
addModeIdToSceneToSettings("<b>Step 3:</b> Map Hub modes to ${state.roomObj.name} Scenes (for automation)")
//---->}
if (state.modeIdToScene) {
//--paragraph "state.modeIdToScene: ${state.modeIdToScene}"
// READY TO IDENTIFY DEVICES
paragraph emphasis("<b>Step 4:</b> Identify Devices for ${state.roomObj.name}")
paragraph emphasis2('Identify Lutron AND <b>Non-Lutron</b>, <b>Non-VSW</b> Devices')
input (
name: 'nonLutronDevices',
type: 'capability.switch', // Enums in the future
title: 'Select Non-Lutron Switches',
submitOnChange: true,
required: true,
multiple: true
)
paragraph emphasis2('Identify <b>Lutron</b>, <b>Non-VSW</b> Devices')
input (
name: 'lutronRepeaters',
type: 'device.LutronKeypad', // Enums in the future
title: 'Select Lutron Main Repeaters',
submitOnChange: true,
required: true,
multiple: true
)
}
if (settings.nonLutronDevices && settings.lutronRepeaters) {
paragraph emphasis("<b>Step 5:</b> Configure Scenes for ${state.roomObj.name}")
state.scenes.each{scene ->
paragraph emphasis2("SCENE: ${scene}")
settings.nonLutronDevices.each{device ->
input(
name: "${scene}.${device.id}",
type: 'number',
title: "<b>${device.displayName}</b><br/>Level 0..100",
width: 2,
submitOnChange: true,
required: true,
multiple: false,
defaultValue: 0
)
}
settings.lutronRepeaters.each{device ->
input(
name: "${scene}.${device.id}",
type: 'number',
title: "<b>${device.displayName}</b><br/>Button #",
width: 2,
submitOnChange: true,
required: true,
multiple: false,
defaultValue: 0
)
}
}
}
////
//// EXIT
////
input(
name: 'exit',
type: 'bool',
title: comment('Exit?'),
submitOnChange: true,
required: true,
defaultValue: false
)
}
}
}
/*
void deviceSceneInputs(DeviceWrapper d, List<String> scenes) {
scenes.collect{scene, index ->
input(
name: "${d.id}:${scene}",
type: 'integer',
width: 1,
title: ${index == 1 ? d.displayName : ''},
defaultValue: 0
)
}
}
*/
// String addRoomHtml(LinkedHashMap room) {
// return """<table>
// ${roomHeadingsHtml(room.scenes)}
// ${roomRowHtml(settings.devices[0], room.scenes)}
// </table>"""
// return """<table>
// <td>Device Name</td>${roomHeadingsHtml(room.scenes)}
// ${room.nonLutronId.collect{ d -> roomRowHtml(d, room.scenes) }}
// ${room.mainRepId.collect{ d -> roomRowHtml(d, room.scenes) }}
// </table>"""
// }
// if (settings.roomObj) {
// paragraph "roomObj: ${settings.roomObj}"
// }
/*
paragraph "settings keys are >${settings.keySet()}<"
if (settings.devices && settings.repeaters) {
// Leverag types for coding.
List<String> scenes = ['alpha', 'beta', 'gamma']
Map<Integer, String> devices = settings.devices
Map<Integer, String> repeaters = settings.repeaters
Map<String, Integer> deviceValues = [:]
Map<String, Integer> repeaterValues = [:]
// Preserve room state
state.room = [:]
state.room.scenes = scenes
state.room.devices = devices
state.room.repeaters = repeaters
state.room.deviceValues = deviceValues
state.room.repeaterValues = repeaterValues
// Isolation test of roomCellHtml(...)
${roomCellHtml(settings.devices[0], "BIRDY")}
// Isolation test of roomRowHtml(...)
${roomHeadingsHtml(room.scenes)}
${roomRowHtml(settings.devices[0], room.scenes)}
*/