forked from georg90/pimatic-max-thermostat
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmaxthermostat.coffee
executable file
·280 lines (234 loc) · 9.14 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
module.exports = (env) ->
Promise = env.require 'bluebird'
MaxCubeConnection = require 'max-control'
Promise.promisifyAll(MaxCubeConnection.prototype)
settled = (promise) -> Promise.settle([promise])
class MaxThermostat extends env.plugins.Plugin
init: (app, @framework, @config) =>
# Promise that is resolved when the connection is established
@_lastAction = new Promise( (resolve, reject) =>
@mc = new MaxCubeConnection(@config.host, @config.port)
@mc.once("connected", =>
if @config.debug
env.logger.debug "Connected, waiting for first update from cube"
@mc.once("update", resolve)
)
@mc.once('error', reject)
return
).catch( (error) ->
env.logger.error "Error on connecting to max cube: #{error.message}"
env.logger.debug error.stack
return
)
@mc.on('response', (res) =>
if @config.debug
env.logger.debug "Response: ", res
)
@mc.on("update", (data) =>
if @config.debug
env.logger.debug "got update", data
@_lastAction = settled(@_lastAction)
)
lastError = null
@mc.on('error', (error) =>
if not lastError? or lastError isnt error.message
env.logger.error "connection error: #{error}"
env.logger.debug error.stack
lastError = error.message
)
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("MaxHeatingThermostat", {
configDef: deviceConfigDef.MaxHeatingThermostat,
createCallback: (config, lastState) -> new MaxHeatingThermostat(config, lastState)
})
@framework.deviceManager.registerDeviceClass("MaxWallThermostat", {
configDef: deviceConfigDef.MaxWallThermostat,
createCallback: (config, lastState) -> new MaxWallThermostat(config, lastState)
})
@framework.deviceManager.registerDeviceClass("MaxContactSensor", {
configDef: deviceConfigDef.MaxContactSensor,
createCallback: (config, lastState) -> new MaxContactSensor(config, lastState)
})
@framework.deviceManager.registerDeviceClass("MaxCube", {
configDef: deviceConfigDef.MaxCube,
createCallback: (config, lastState) -> new MaxCube(config, lastState)
})
isWindowOpen: (rfAddress) ->
@_lastAction = settled(@_lastAction).then( =>
device = @mc.devices[rfAddress]
unless device?
return null
return [email protected](device.roomId)
)
return @_lastAction
setTemperatureSetpoint: (rfAddress, mode, value) ->
@_lastAction = settled(@_lastAction).then( =>
@mc.setTemperatureAsync(rfAddress, mode, value)
)
return @_lastAction
plugin = new MaxThermostat
class MaxHeatingThermostat extends env.devices.HeatingThermostat
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_temperatureSetpoint = lastState?.temperatureSetpoint?.value
@_mode = lastState?.mode?.value or "auto"
@_battery = lastState?.battery?.value or "ok"
@_lastSendTime = 0
@updateEventHandler = ((data) =>
data = data[@config.rfAddress]
if data?
now = new Date().getTime()
unless @_tempToSetIfWindowClosed?
###
Give the cube some time to handle the changes. If we send new values to the cube
we set _lastSendTime to the current time. We consider the values as successfully set, when
the command was not rejected. But the updates received from the cube in the next 30
seconds do not always reflect the updated values, therefore we ignoring the old values
we got by the update message for 30 seconds.
In the case that the cube did not react to our the send commands, the values will be
overwritten with the internal state (old ones) of the cube after 30 seconds, because
the update event is emitted by max-control periodically.
###
if now - @_lastSendTime < 30*1000
# only if values match, we are synced
if data.setpoint is @_temperatureSetpoint and data.mode is @_mode
@_setSynced(true)
else
# more then 30 seconds passed, set the values anyway
@_setSetpoint(data.setpoint)
@_setMode(data.mode)
@_setSynced(true)
else
plugin.isWindowOpen(@config.rfAddress).then( (windowOpen) =>
if windowOpen
@_setSetpoint(data.setpoint)
@_setMode(data.mode)
else
if plugin.config.debug
env.logger.debug("All windows closed, setting saved temperature")
tempToSet = @_tempToSetIfWindowClosed
@_tempToSetIfWindowClosed = undefined
return plugin.setTemperatureSetpoint(
@config.rfAddress, @_mode, tempToSet
).then( () =>
@_lastSendTime = new Date().getTime()
@_setSynced(false)
@_setSetpoint(tempToSet)
)
).catch( (err) =>
env.logger.error("Error setting temp after window was closed: #{err.message}")
env.logger.debug(err)
)
@_setValve(data.valve)
@_setBattery(data.battery)
return
)
plugin.mc.on("update", @updateEventHandler)
super()
destroy: () ->
plugin.mc.removeListener("update", @updateEventHandler)
super()
changeModeTo: (mode) ->
temp = @_temperatureSetpoint
if mode is "auto"
temp = null
return plugin.setTemperatureSetpoint(@config.rfAddress, mode, temp).then( =>
@_lastSendTime = new Date().getTime()
@_setSynced(false)
@_setMode(mode)
)
changeTemperatureTo: (temperatureSetpoint) ->
if @_temperatureSetpoint is temperatureSetpoint then return Promise.resolve()
return plugin.isWindowOpen(@config.rfAddress).then( (windowOpen) =>
if windowOpen
env.logger.debug("A window is open waiting till window is closed") if plugin.config.debug
@_setSynced(false)
@_tempToSetIfWindowClosed = temperatureSetpoint
@_lastSendTime = new Date().getTime()
return
else
return plugin.setTemperatureSetpoint(
@config.rfAddress, @_mode, temperatureSetpoint
).then( =>
@_lastSendTime = new Date().getTime()
@_setSynced(false)
@_setSetpoint(temperatureSetpoint)
)
)
class MaxWallThermostat extends env.devices.TemperatureSensor
_temperature: null
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_temperature = lastState?.temperature?.value
@updateEventHandler = (data) =>
data = data[@config.rfAddress]
if data?.actualTemperature?
@_temperature = data.actualTemperature
@emit 'temperature', @_temperature
plugin.mc.on("update", @updateEventHandler)
super()
destroy: () ->
plugin.mc.removeListener("update", @updateEventHandler)
super()
getTemperature: -> Promise.resolve(@_temperature)
class MaxContactSensor extends env.devices.ContactSensor
attributes:
contact:
description: "State of the contact"
type: t.boolean
labels: ['closed', 'opened']
battery:
description: "Battery status"
type: "string"
enum: ["ok", "low"]
_battery: null
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_battery = lastState?.battery?.value or "ok"
@_contact = lastState?.contact?.value
@updateEventHandler = (data) =>
data = data[@config.rfAddress]
if data?
@_setContact(data.state is 'closed')
@_setBattery(data.battery)
return
plugin.mc.on("update", @updateEventHandler)
super()
destroy: () ->
plugin.mc.removeListener("update", @updateEventHandler)
super()
_setBattery: (battery) ->
if battery is @_battery then return
@_battery = battery
@emit "battery", @_battery
getBattery: () -> Promise.resolve(@_battery)
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
@statusEventHandler = (info) =>
@emit 'dutycycle', info.dutyCycle
@emit 'memoryslots', info.memorySlots
plugin.mc.on("status", @statusEventHandler)
super()
destroy: () ->
plugin.mc.removeListener("status", @statusEventHandler)
super()
getDutycycle: -> Promise.resolve(@_dutycycle)
getMemoryslots: -> Promise.resolve(@_memoryslots)
return plugin