-
Notifications
You must be signed in to change notification settings - Fork 15
/
maxthermostat.coffee
executable file
·371 lines (306 loc) · 11 KB
/
maxthermostat.coffee
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
_ = env.require 'lodash'
MaxCubeConnection = require 'max-control'
Promise.promisifyAll(MaxCubeConnection.prototype)
M = env.matcher
settled = (promise) -> Promise.settle([promise])
class MaxThermostat extends env.plugins.Plugin
init: (app, @framework, @config) =>
@framework.ruleManager.addActionProvider(new MaxModeActionProvider(@framework))
@framework.ruleManager.addActionProvider(new MaxTempActionProvider(@framework))
# Promise that is resolved when the connection is established
@_lastAction = new Promise( (resolve, reject) =>
@mc = new MaxCubeConnection(@config.host, @config.port)
@mc.once("connected", resolve)
@mc.client.once('error', reject)
return
).timeout(60000).catch( (error) ->
env.logger.error "Error on connecting to max cube: #{error.message}"
env.logger.debug error.stack
return
)
@mc.on('response', (res) =>
env.logger.debug "Response: ", res
)
@mc.on("update", (data) =>
env.logger.debug "got update", data
)
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("MaxThermostatDevice", {
configDef: deviceConfigDef.MaxThermostatDevice,
createCallback: (config) -> new MaxThermostatDevice(config)
})
@framework.deviceManager.registerDeviceClass("MaxContactSensor", {
configDef: deviceConfigDef.MaxContactSensor,
createCallback: (config) -> new MaxContactSensor(config)
})
@framework.deviceManager.registerDeviceClass("MaxCube", {
configDef: deviceConfigDef.MaxCube,
createCallback: (config, lastState) -> new MaxCube(config, lastState)
})
# wait till all plugins are loaded
@framework.on "after init", =>
# Check if the mobile-frontent was loaded and get a instance
mobileFrontend = @framework.pluginManager.getPlugin 'mobile-frontend'
if mobileFrontend?
mobileFrontend.registerAssetFile 'js', "pimatic-max-thermostat/app/jqm-spinbox.js"
mobileFrontend.registerAssetFile 'js', "pimatic-max-thermostat/app/js.coffee"
mobileFrontend.registerAssetFile 'css', "pimatic-max-thermostat/app/css/css.css"
mobileFrontend.registerAssetFile 'html', "pimatic-max-thermostat/app/template.html"
else
env.logger.warn(
"MaxThermostat could not find the mobile-frontend. No gui will be available"
)
setTemperature: (deviceNo, mode, value) ->
@_lastAction = settled(@_lastAction).then( =>
@mc.setTemperatureAsync(deviceNo, mode, value)
)
return @_lastAction
plugin = new MaxThermostat
class MaxThermostatDevice extends env.devices.Device
attributes:
settemperature:
description: "the temp that should be set"
type: "number"
unit: "°C"
mode:
description: "the current mode"
type: "string"
enum: ["auto", "manu", "boost"]
actions:
changeModeTo:
params:
mode:
type: "string"
changeTemperatureTo:
params:
settemperature:
type: "number"
template: "MaxThermostatDevice"
_mode: "auto"
_settemperature: null
constructor: (@config) ->
@id = @config.id
@name = @config.name
@_settemperature = @config.actTemp
plugin.mc.on("update", (data) =>
data = data[@config.deviceNo]
if data?
@config.actTemp = data.setpoint
@config.mode = data.mode
# @config.comfyTemp = data.comfortTemperature // Doesn't make sense
# @config.ecoTemp = data.ecoTemperature // you'll define these in the pimatic config
@config.battery = data.battery
@_setTemp(@config.actTemp)
@_setMode(@config.mode)
return
)
super()
getMode: () -> Promise.resolve(@_mode)
getSettemperature: () -> Promise.resolve(@_settemperature)
_setMode: (mode) ->
if mode is @_mode then return
@_mode = mode
@emit "mode", @_mode
_setTemp: (settemperature) ->
if settemperature is @_settemperature then return
@_settemperature = settemperature
@emit "settemperature", @_settemperature
changeModeTo: (mode) ->
return plugin.setTemperature(@config.deviceNo, mode, @config.actTemp).then( =>
@_setMode(mode)
)
changeTemperatureTo: (temperature) ->
if @settemperature is temperature then return
return plugin.setTemperature(@config.deviceNo, @config.mode, temperature)
class MaxContactSensor extends env.devices.ContactSensor
constructor: (@config) ->
@id = @config.id
@name = @config.name
plugin.mc.on("update", (data) =>
data = data[@config.deviceNo]
if data?
@_setContact(data.state is 'closed')
return
)
super()
class MaxCube extends env.devices.Sensor
attributes:
dutycycle:
description: "Percentage of max rf limit reached"
type: "number"
unit: "%"
memoryslots:
description: "Available memory slots for commands"
type: "number"
_dutycycle: 0
_memoryslots: 50
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_dutycycle = plugin.mc.dutyCycle
@_memoryslots = plugin.mc.memorySlots
plugin.mc.on("status", (info) =>
@emit 'dutycycle', info.dutyCycle
@emit 'memoryslots', info.memorySlots
)
super()
getDutycycle: -> Promise.resolve(@_dutycycle)
getMemoryslots: -> Promise.resolve(@_memoryslots)
class MaxModeActionProvider extends env.actions.ActionProvider
constructor: (@framework) ->
# ### parseAction()
###
Parses the above actions.
###
parseAction: (input, context) =>
# The result the function will return:
retVar = null
thermostats = _(@framework.deviceManager.devices).values().filter(
(device) => device.hasAction("changeModeTo")
).value()
if thermostats.length is 0 then return
device = null
valueTokens = null
match = null
# Try to match the input string with:
M(input, context)
.match('set mode of ')
.matchDevice(thermostats, (next, d) =>
next.match(' to ')
.matchStringWithVars( (next, ts) =>
m = next.match(' mode', optional: yes)
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
valueTokens = ts
match = m.getFullMatch()
)
)
if match?
if valueTokens.length is 1 and not isNaN(valueTokens[0])
value = valueTokens[0]
assert(not isNaN(value))
modes = ["eco", "boost", "auto", "manu", "comfy"]
# TODO: Implement eco & comfy in changeModeTo method!
if modes.indexOf(value) < -1
context?.addError("Allowed modes: eco,boost,auto,manu,comfy")
return
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new MaxModeActionHandler(@framework, device, valueTokens)
}
else
return null
class MaxModeActionHandler extends env.actions.ActionHandler
constructor: (@framework, @device, @valueTokens) ->
assert @device?
assert @valueTokens?
###
Handles the above actions.
###
_doExecuteAction: (simulate, value) =>
return (
if simulate
__("would set mode %s to %s%%", @device.name, value)
else
@device.changeModeTo(value).then( => __("set mode %s to %s%%", @device.name, value) )
)
# ### executeAction()
executeAction: (simulate) =>
@framework.variableManager.evaluateStringExpression(@valueTokens).then( (value) =>
@lastValue = value
return @_doExecuteAction(simulate, value)
)
# ### hasRestoreAction()
hasRestoreAction: -> yes
# ### executeRestoreAction()
executeRestoreAction: (simulate) => Promise.resolve(@_doExecuteAction(simulate, @lastValue))
class MaxTempActionProvider extends env.actions.ActionProvider
constructor: (@framework) ->
# ### parseAction()
###
Parses the above actions.
###
parseAction: (input, context) =>
# The result the function will return:
retVar = null
thermostats = _(@framework.deviceManager.devices).values().filter(
(device) => device.hasAction("changeTemperatureTo")
).value()
if thermostats.length is 0 then return
device = null
valueTokens = null
match = null
# Try to match the input string with:
M(input, context)
.match('set temp of ')
.matchDevice(thermostats, (next, d) =>
next.match(' to ')
.matchNumericExpression( (next, ts) =>
m = next.match('°C', optional: yes)
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
valueTokens = ts
match = m.getFullMatch()
)
)
if match?
if valueTokens.length is 1 and not isNaN(valueTokens[0])
value = valueTokens[0]
assert(not isNaN(value))
value = parseFloat(value)
if value < 0.0
context?.addError("Can't set temp to a negativ value.")
return
if value > 32.0
context?.addError("Can't set temp higher than 32°C.")
return
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new MaxTempActionHandler(@framework, device, valueTokens)
}
else
return null
class MaxTempActionHandler extends env.actions.ActionHandler
constructor: (@framework, @device, @valueTokens) ->
assert @device?
assert @valueTokens?
# _clampVal: (value) ->
# assert(not isNaN(value))
# return (switch
# when value > 32 then 32
# when value < 0 then 0
# else value
# )
###
Handles the above actions.
###
_doExecuteAction: (simulate, value) =>
return (
if simulate
__("would set temp of %s to %s%%", @device.name, value)
else
@device.changeTemperatureTo(value).then( =>
__("set temp of %s to %s%%", @device.name, value)
)
)
# ### executeAction()
executeAction: (simulate) =>
@framework.variableManager.evaluateNumericExpression(@valueTokens).then( (value) =>
# value = @_clampVal value
@lastValue = value
return @_doExecuteAction(simulate, value)
)
# ### hasRestoreAction()
hasRestoreAction: -> yes
# ### executeRestoreAction()
executeRestoreAction: (simulate) => Promise.resolve(@_doExecuteAction(simulate, @lastValue))
return plugin