This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnefit.coffee
172 lines (140 loc) · 4.96 KB
/
nefit.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
module.exports = (env) ->
Promise = env.require 'bluebird'
NefitEasyClient = require 'nefit-easy-commands'
class nefit extends env.plugins.Plugin
init: (app, @framework, @config) =>
# wait till all plugins are loaded
@framework.on "after init", =>
# Check if the mobile-frontend was loaded and get a instance
mobileFrontend = @framework.pluginManager.getPlugin 'mobile-frontend'
if mobileFrontend?
mobileFrontend.registerAssetFile 'js', "pimatic-nefit/app/nefit-page.coffee"
mobileFrontend.registerAssetFile 'css', "pimatic-nefit/app/nefit-template.css"
mobileFrontend.registerAssetFile 'html', "pimatic-nefit/app/nefit-template.jade"
else
env.logger.warn "your plugin could not find the mobile-frontend. No gui will be available"
#Register devices
deviceConfigDef = require("./device-config-schema.coffee")
deviceClasses = [
NefitThermostat
]
for Cl in deviceClasses
do (Cl) =>
@framework.deviceManager.registerDeviceClass(Cl.name, {
configDef: deviceConfigDef[Cl.name]
createCallback: (config,lastState) =>
device = new Cl(config, lastState)
return device
})
class NefitThermostat extends env.devices.HeatingThermostat
template: "nefit"
attributes:
temperatureSetpoint:
label: "Temperature Setpoint"
description: "The temp that should be set"
type: "number"
discrete: true
unit: "°C"
valve:
description: "Position of the valve"
type: "boolean"
discrete: true
mode:
description: "The current mode"
type: "string"
enum: ["clock",'manual']
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_temperatureSetpoint = lastState?.temperatureSetpoint?.value or 20
@_valve = false
@_mode = lastState?.mode?.value or "manual"
@_temperature = lastState?.temperature?.value
@_pressure = lastState?.pressure?.value
@addAttribute('temperature', {
label: "House temperature"
description: "House temperature",
type: "number"
displaySparkline: false
unit: "°C"
})
@['temperature'] = ()-> Promise.resolve(@_temperature)
@addAttribute('pressure', {
label: "Pressure"
description: "Pressure of the water",
type: "number"
displaySparkline: false
unit: "bar"
})
@['pressure'] = ()-> Promise.resolve(@_pressure)
@client = NefitEasyClient({
serialNumber : @config.SerialNumber,
accessKey : @config.AccessKey,
password : @config.Password,
})
@client.connect().then( () =>
@requestData()
).catch( (e) =>
env.logger.debug(e)
)
super()
changeTemperatureTo: (temperatureSetpoint) ->
clearTimeout @requestTimeout if @requestTimeout?
@requestTimeout = setTimeout(@requestData, @config.Polling)
@_setSetpoint(temperatureSetpoint)
@client.setTemperature(temperatureSetpoint).then( (result) =>
return Promise.resolve()
).catch( (e) =>
env.logger.debug(e)
return Promise.resolve()
)
changeModeTo: (mode) ->
@_setMode(mode)
clearTimeout @requestTimeout if @requestTimeout?
@requestTimeout = setTimeout(@requestData, @config.Polling)
@client.setUserMode(mode).then( (status) =>
return Promise.resolve()
).catch( (e) =>
env.logger.debug(e)
return Promise.resolve()
)
requestData: () =>
env.logger.debug("polling thermostat")
clearTimeout @requestTimeout if @requestTimeout?
@client.status().then( (status) =>
env.logger.debug(status)
# get the mode
@_setMode(status["user mode"])
# get temperature setpoint of thermostat
@_setSetpoint(parseFloat(status["temp setpoint"]))
# Get the heating status
if status['boiler indicator'] == 'off'
@_setValve(false)
else
@_setValve(true)
# get house temperature
temperature = parseFloat(status["in house temp"])
unless temperature is @_temperature
@_temperature = temperature
@emit "temperature", @_temperature
).catch( (e) =>
env.logger.debug(e)
)
@client.pressure().then( (result) =>
# get the pressure
pressure = parseFloat(result.pressure)
unless pressure is @_pressure
@_pressure = pressure
@emit "pressure", @_pressure
).catch( (e) =>
env.logger.debug(e)
)
@requestTimeout = setTimeout(@requestData, @config.Polling)
destroy: ->
@client.end()
clearTimeout @requestTimeout if @requestTimeout?
super()
getTemperature: () -> Promise.resolve(@_temperature)
getPressure: () -> Promise.resolve(@_pressure)
nefit = new nefit
return nefit