forked from xAPPO/MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT Text beta 3e (test)
243 lines (215 loc) · 7.12 KB
/
MQTT Text beta 3e (test)
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
// beta 3d
metadata
{
definition (name: "MQTT Text", namespace: "ukusa", author: "Kevin Hawkins",importUrl: "https://raw.githubusercontent.com/xAPPO/MQTT/beta3/MQTT%20Text%20driver")
{
capability "Sensor"
capability "Refresh"
capability "Telnet" // temporary kludge
capability "Notification"
capability "Actuator"
capability "Battery"
//capability "Switch"
attribute "text", "string"
attribute "battery", "number"
attribute "sensor","string"
attribute "mqtt","string"
attribute "prefix","string"
attribute "suffix","string"
/*
attribute "stateOn","string"
attribute "stateOff","string"
attribute "stateTopic","string" //onoff
attribute "textTopic","string"
attribute "cmdTopic","string"
attribute "textCmdTopic","string"
*/
command "setSuffix",["String"]
command "setPrefix",["String"]
command "initialize"
command "setBattery", ["Integer"]
command "setText", ["String"]
command "forceText", ["String"]
/*
command "updateText", ["String"]
command "setStateTopic", ["String"]
command "setTextTopic", ["String"]
command "setTextCmdTopic", ["String"]
command "getStateTopics", ["String"]
command "setStateCmdTopic", ["String"]
command "on"
command "off"
command "toON"
command "toOFF"
command "setValue", ["String"]
command "forceValue", ["String"] // updates text value directly as well should be boolean or string option
*/
}
}
/*
preferences{
section ("... not required for automatically discovered devices ")
{
//input name: "TextTopic", type: "text", title: "Text topic", description: "", required: false, displayDuringSetup: true
//input name: "TextCmdTopic", type: "text", title: "Text Command topic", description: "", required: false, displayDuringSetup: true
//input name: "TextPrefix", type: "text", title: "Text prefix", description: "prefix inserted before display text", required: false, displayDuringSetup: true
//input name: "TextSuffix", type: "text", title: "Text suffix", description: "suffix added after display text", required: false, displayDuringSetup: true
}
}
*/
def installed()
{
initialize()
}
def updated()
{
if (state.type==null||state.type=="manual")
if (settings?.TextTopic != null) {
state.type = "manual"
sendEvent(name: "textTopic", value: settings?.TextTopic,isStateChange: true)
device.deviceNetworkId = 'MQTT:Internal '+ settings?.TextTopic
if (settings?.TextPrefix != null){
sendEvent(name: "prefix", value: settings?.TextPrefix,isStateChange: true)
//sendEvent(name: "stateON", value: settings?.StateON,isStateChange: true)
}
if (settings?.TextSuffix != null){
sendEvent(name: "suffix", value: settings?.TextSuffix,isStateChange: true)
//sendEvent(name: "stateOFF", value: settings?.StateOFF,isStateChange: true)
}
}
initialize()
}
def initialize()
{
//refresh()
sendEvent(name: "mqtt", value: "text",isStateChange: true)
sendEvent(name: "text", value: " ",isStateChange: true)
}
def parse(String description) // shouldn't get called
{
log.warn "Msg: Description is $description"
}
def refresh() {
return
}
def deviceNotification(notify)
{
//log.warn "TODO: Need to add notification [$notify]"
setText (notify)
}
def setSuffix(suffix)
{
if (suffix==null) suffix = ' '
sendEvent(name: "suffix", value: suffix,isStateChange: true)
if (state.rich) setText(state.textVal)
}
def setPrefix(prefix)
{
if (prefix==null) prefix=' '
sendEvent(name: "prefix", value: prefix,isStateChange: true)
if (state.rich) setText(state.textVal)
}
def setBattery(level)
{
// if ((level>=0)&&(level<=100)){
sendEvent(name: "battery", value:level,isStateChange: true)
//setSuffix(" %")
//setPrefix (" ")
//forceText(level.toString())
// }
}
def forceText(String display){
state.rich=false
sendEvent(name: "text", value: display,isStateChange: true)
}
def setText (String value)
{
state.rich=true
state.textVal=value
if ((device.currentValue("prefix") !=" ")&&(device.currentValue("prefix") !=null)) value = device.latestValue("prefix") + value
if ((device.currentValue("suffix") != " ")&&(device.currentValue("suffix") !=null)) value = value + device.latestValue("suffix")
sendEvent(name: "text", value: value, isStateChange: true)
}
// Older methods
/*
def setValue (String value)
{
//log.trace "Text Device was set to ${value}"
//sendEvent(name: "Notify", value: state.statePrefix + value + state.stateSuffix,isStateChange: true)
if (device.currentValue("prefix") !=" ") value = device.latestValue("prefix") + value
if (device.currentValue("suffix") != " ") value = value + device.latestValue("suffix")
if (state.type=="manual") sendEvent(name: "changeText", value: display, data: [topic: device.currentValue("textCmdTopic")],isStateChange: true)
//sendEvent(name: "text", value: value, isStateChange: true)
//sendEvent(name: "textCmdTopic", value: "", isStateChange: true)
//if (state.type=="manual") sendEvent(name: "changeText", value: value, data: [topic: device.textCmdTopic],isStateChange: true)
//sendEvent(name: "mqtt", value: 'text',isStateChange: true)
}
*/
/*
def getStateTopics()
{
log.error ("getStateTopics() within DisplayText was called - to be deprecated","WARN")
// TODO INCOMPLETE - INPUT ONLY DEVICE NEED TO ADD MORE CODE SIMILAR TO Dimmer
// needed to create DNI of device to match MQTT state changes
//sendEvent(name: "topic", value: state.stateTopic,isStateChange: true)
}
*/
/*
def setStateTopic(String topic)
{
log.debug "State topic set to " + topic
sendEvent(name: "stateTopic", value: topic,isStateChange: true)
}
*/
/*
def setStateCmdTopic(String topic)
{
//log.debug "Command topic set to " + topic
sendEvent(name: "cmdTopic", value: topic,isStateChange: true)
}
*/
/*
def setTextTopic(String topic)
{
sendEvent(name: "textTopic", value: topic,isStateChange: true)
}
def setTextCmdTopic(String topic)
{
sendEvent(name: "textCmdTopic", value: topic,isStateChange: true)
}
*/
/*
def updateText (String value){
sendEvent(name: "text", value: value, isStateChange: true)
}
*/
/*
def on() {
//log.debug "child On " + state.commandTopic
sendEvent(name: "switch", value: "on", isStateChange: true)
//parent.sendPayload(state.commandTopic,"true")
//parent.sendPayload(device.cmdTopic, device.stateTypeON)
}
def off() {
//log.debug "child Off "+ state.commandTopic
sendEvent(name: "switch", value: "off", isStateChange: true)
//parent.sendPayload(state.commandTopic,"false")
//parent.sendPayload(device.cmdTopic,device.stateTypeOFF)
}
def toON() { // avoids publish loop
//log.debug "child > On "
sendEvent(name: "switch", value: "on", isStateChange: true)
}
def toOFF() { // avoids publish loop
//log.debug "child > Off "
sendEvent(name: "switch", value: "off", isStateChange: true)
}
*/
/*
def setStateType (stateOFF,stateON)
{
//log.trace "Setting OFF State Type to " + stateOFF
sendEvent(name: "stateOn", value: stateON,isStateChange: true)
sendEvent(name: "stateOff", value: stateOFF,isStateChange: true)
}
*/