/
ESPHome-MultiSwitch.groovy
180 lines (157 loc) · 6.32 KB
/
ESPHome-MultiSwitch.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
/**
* MIT License
* Copyright 2022 Jonathan Bradshaw (jb@nrgup.net)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
metadata {
definition(
name: 'ESPHome Multi Switch',
namespace: 'esphome',
author: 'Jonathan Bradshaw',
singleThreaded: true,
importUrl: 'https://raw.githubusercontent.com/bradsjm/hubitat-drivers/main/ESPHome/ESPHome-MultiSwitch.groovy') {
capability 'Refresh'
capability 'Initialize'
capability 'SignalStrength'
// attribute populated by ESPHome API Library automatically
attribute 'networkStatus', 'enum', [ 'connecting', 'online', 'offline' ]
}
preferences {
input name: 'ipAddress', // required setting for API library
type: 'text',
title: 'Device IP Address',
required: true
input name: 'password', // optional setting for API library
type: 'text',
title: 'Device Password <i>(if required)</i>',
required: false
input name: 'logEnable', // if enabled the library will log debug details
type: 'bool',
title: 'Enable Debug Logging',
required: false,
defaultValue: false
input name: 'logTextEnable',
type: 'bool',
title: 'Enable descriptionText logging',
required: false,
defaultValue: true
}
}
import com.hubitat.app.DeviceWrapper
import com.hubitat.app.ChildDeviceWrapper
public void initialize() {
// API library command to open socket to device, it will automatically reconnect if needed
openSocket()
if (logEnable) {
runIn(1800, 'logsOff')
}
}
public void installed() {
log.info "${device} driver installed"
}
public void logsOff() {
espHomeSubscribeLogs(LOG_LEVEL_INFO, false) // disable device logging
device.updateSetting('logEnable', false)
log.info "${device} debug logging disabled"
}
// driver commands
public void componentOn(DeviceWrapper dw) {
String key = dw.getDeviceNetworkId().minus("${device.id}-")
if (dw.currentValue('switch') != 'on') {
if (logTextEnable) { log.info "${device} on" }
espHomeSwitchCommand(key: key as Long, state: true)
}
}
public void componentOff(DeviceWrapper dw) {
String key = dw.getDeviceNetworkId().minus("${device.id}-")
if (dw.currentValue('switch') != 'off') {
if (logTextEnable) { log.info "${device} off" }
espHomeSwitchCommand(key: key as Long, state: false)
}
}
public void refresh() {
log.info "${device} refresh"
state.clear()
state.requireRefresh = true
espHomeDeviceInfoRequest()
}
public void updated() {
log.info "${device} driver configuration updated"
initialize()
}
public void uninstalled() {
closeSocket('driver uninstalled') // make sure the socket is closed when uninstalling
log.info "${device} driver uninstalled"
}
// the parse method is invoked by the API library when messages are received
public void parse(Map message) {
if (logEnable) { log.debug "ESPHome received: ${message}" }
switch (message.type) {
case 'device':
// Device information
break
case 'entity':
// Discover all binary switches and create child devices for each
if (message.platform == 'switch' && !message.disabledByDefault && message.entityCategory == 'none') {
String dni = "${device.id}-${message.key}"
ChildDeviceWrapper dw = getChildDevice(dni) ?:
addChildDevice(
'hubitat',
'Generic Component Switch',
dni
)
dw.name = message.objectId
dw.label = message.name
}
if (message.platform == 'sensor') {
switch (message.deviceClass) {
case 'signal_strength':
state['signalStrength'] = message.key
break
}
}
break
case 'state':
// Signal Strength
if (state.signalStrength as Long == message.key && message.hasState) {
Integer rssi = Math.round(message.state as Float)
String unit = 'dBm'
if (device.currentValue('rssi') != rssi) {
descriptionText = "${device} rssi is ${rssi}"
sendEvent(name: 'rssi', value: rssi, unit: unit, descriptionText: descriptionText)
if (logTextEnable) { log.info descriptionText }
}
return
}
// Receives entity state updates to send to child device
if (message.platform == 'switch') {
String dni = "${device.id}-${message.key}"
String type = message.isDigital ? 'digital' : 'physical'
String value = message.state ? 'on' : 'off'
getChildDevice(dni)?.parse([
[ name: 'switch', value: value, type: type, descriptionText: "switch is ${value}" ]
])
return
}
break
}
}
// Put this line at the end of the driver to include the ESPHome API library helper
#include esphome.espHomeApiHelper