-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnilan.go
332 lines (286 loc) · 10.9 KB
/
nilan.go
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
package main
import (
"log"
"os"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/service"
"github.com/pjuzeliunas/nilan"
)
// Nilan CTS700 accessory
type Nilan struct {
*accessory.Accessory
CentralHeatingSwitch *service.Switch
VentilationThermostat *NilanFanThermostat
OutdoorTemp *service.TemperatureSensor
Fan *NilanFan
HotWaterSwitch *service.Switch
HotWater *service.Thermostat
SupplyFlow *service.Thermostat
}
// NilanFanThermostat service
type NilanFanThermostat struct {
*service.Thermostat
CurrentRelativeHumidity *characteristic.CurrentRelativeHumidity
}
// NewNilanFanThermostat instantiates Nilan Central Heating service
func NewNilanFanThermostat() *NilanFanThermostat {
svc := NilanFanThermostat{}
svc.Thermostat = service.NewThermostat()
svc.CurrentRelativeHumidity = characteristic.NewCurrentRelativeHumidity()
svc.AddCharacteristic(svc.CurrentRelativeHumidity.Characteristic)
return &svc
}
// NilanFan service
type NilanFan struct {
*service.FanV2
RotationSpeed *characteristic.RotationSpeed
}
// NewNilanFan instantiates Nilan Fan service
func NewNilanFan() *NilanFan {
svc := NilanFan{}
svc.FanV2 = service.NewFanV2()
svc.RotationSpeed = characteristic.NewRotationSpeed()
svc.RotationSpeed.SetMinValue(25)
svc.RotationSpeed.SetMaxValue(100)
svc.RotationSpeed.SetStepValue(25)
svc.AddCharacteristic(svc.RotationSpeed.Characteristic)
return &svc
}
// NewNilan sets Nilan accessory instance up
func NewNilan(info accessory.Info) *Nilan {
acc := Nilan{}
acc.Accessory = accessory.New(info, accessory.TypeHeater)
acc.CentralHeatingSwitch = service.NewSwitch()
acc.CentralHeatingSwitch.AddCharacteristic(newName("Central Heating"))
acc.CentralHeatingSwitch.On.OnValueRemoteUpdate(func(on bool) {
log.Printf("Setting Central Heating active: %v\n", on)
s := nilan.Settings{}
p := !on
s.CentralHeatingPaused = &p
if !on {
s.CentralHeatingPauseDuration = new(int)
*s.CentralHeatingPauseDuration = 180
}
c := nilanController()
c.SendSettings(s)
})
acc.VentilationThermostat = NewNilanFanThermostat()
acc.VentilationThermostat.Primary = true
acc.VentilationThermostat.AddCharacteristic(newName("Room Temperature"))
acc.VentilationThermostat.TargetHeatingCoolingState.OnValueRemoteUpdate(func(state int) {
c := nilanController()
switch state {
case characteristic.TargetHeatingCoolingStateOff:
p := true
s := nilan.Settings{VentilationOnPause: &p}
c.SendSettings(s)
case characteristic.TargetHeatingCoolingStateHeat:
p := false
m := 2 // heating
s := nilan.Settings{VentilationMode: &m, VentilationOnPause: &p}
c.SendSettings(s)
case characteristic.TargetHeatingCoolingStateCool:
p := false
m := 1 // cooling
s := nilan.Settings{VentilationMode: &m, VentilationOnPause: &p}
c.SendSettings(s)
case characteristic.TargetHeatingCoolingStateAuto:
p := false
m := 0 // auto
s := nilan.Settings{VentilationMode: &m, VentilationOnPause: &p}
c.SendSettings(s)
}
})
acc.VentilationThermostat.TemperatureDisplayUnits.SetValue(characteristic.TemperatureDisplayUnitsCelsius)
acc.VentilationThermostat.TargetTemperature.SetMinValue(5.0)
acc.VentilationThermostat.TargetTemperature.SetMaxValue(40.0)
acc.VentilationThermostat.TargetTemperature.SetStepValue(1.0)
acc.VentilationThermostat.TargetTemperature.OnValueRemoteUpdate(func(tFloat float64) {
log.Printf("Setting new Room target temperature: %v\n", tFloat)
t := int(tFloat * 10.0)
if !(t >= 50 && t <= 400) {
log.Println("Invalid Room temperature setting. Ignoring change request.")
return
}
s := nilan.Settings{DesiredRoomTemperature: &t}
c := nilanController()
c.SendSettings(s)
})
acc.Fan = NewNilanFan()
acc.Fan.AddCharacteristic(newName("Fan"))
acc.Fan.Active.Perms = []string{characteristic.PermRead, characteristic.PermEvents}
acc.Fan.RotationSpeed.OnValueRemoteUpdate(func(newSpeed float64) {
log.Printf("Setting new Fan speed: %v\n", newSpeed)
speed := nilan.FanSpeed(100 + int(newSpeed)/25)
if !(speed >= 101 && speed <= 104) {
log.Println("Invalid Fan speed. Ignoring change request.")
return
}
s := nilan.Settings{FanSpeed: &speed}
c := nilanController()
c.SendSettings(s)
})
acc.HotWaterSwitch = service.NewSwitch()
acc.HotWaterSwitch.AddCharacteristic(newName("Hot Water Production"))
acc.HotWaterSwitch.On.OnValueRemoteUpdate(func(on bool) {
log.Printf("Setting DHW active: %v\n", on)
s := nilan.Settings{}
p := !on
s.DHWProductionPaused = &p
if !on {
s.DHWProductionPauseDuration = new(int)
*s.DHWProductionPauseDuration = 180
}
c := nilanController()
c.SendSettings(s)
})
acc.HotWater = service.NewThermostat()
acc.HotWater.AddCharacteristic(newName("Hot Water"))
acc.HotWater.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateHeat)
acc.HotWater.TargetHeatingCoolingState.Perms = []string{characteristic.PermRead, characteristic.PermEvents}
acc.HotWater.TemperatureDisplayUnits.SetValue(characteristic.TemperatureDisplayUnitsCelsius)
acc.HotWater.TargetTemperature.SetMinValue(10.0)
acc.HotWater.TargetTemperature.SetMaxValue(60.0)
acc.HotWater.TargetTemperature.SetStepValue(1.0)
acc.HotWater.TargetTemperature.OnValueRemoteUpdate(func(tFloat float64) {
log.Printf("Setting new DHW target temperature: %v\n", tFloat)
t := int(tFloat * 10.0)
if !(t >= 100 && t <= 600) {
log.Println("Invalid DHW temperature setting. Ignoring change request.")
return
}
s := nilan.Settings{DesiredDHWTemperature: &t}
c := nilanController()
c.SendSettings(s)
})
acc.SupplyFlow = service.NewThermostat()
acc.SupplyFlow.AddCharacteristic(newName("Central Heating"))
acc.SupplyFlow.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateHeat)
acc.SupplyFlow.TargetHeatingCoolingState.Perms = []string{characteristic.PermRead, characteristic.PermEvents}
acc.SupplyFlow.TemperatureDisplayUnits.SetValue(characteristic.TemperatureDisplayUnitsCelsius)
acc.SupplyFlow.TargetTemperature.SetMinValue(5.0)
acc.SupplyFlow.TargetTemperature.SetMaxValue(50.0)
acc.SupplyFlow.TargetTemperature.SetStepValue(1.0)
acc.SupplyFlow.TargetTemperature.OnValueRemoteUpdate(func(tFloat float64) {
log.Printf("Setting new Supply Flow target temperature: %v\n", tFloat)
t := int(tFloat * 10.0)
if !(t >= 50 && t <= 500) {
log.Println("Invalid Supply Flow temperature setting. Ignoring change request.")
return
}
s := nilan.Settings{SetpointSupplyTemperature: &t}
c := nilanController()
c.SendSettings(s)
})
acc.OutdoorTemp = service.NewTemperatureSensor()
acc.OutdoorTemp.AddCharacteristic(newName("Outdoor Temperature"))
acc.OutdoorTemp.CurrentTemperature.SetMinValue(-40)
acc.OutdoorTemp.CurrentTemperature.SetMaxValue(160)
acc.AddService(acc.CentralHeatingSwitch.Service)
acc.AddService(acc.VentilationThermostat.Service)
acc.AddService(acc.OutdoorTemp.Service)
acc.AddService(acc.Fan.Service)
acc.AddService(acc.HotWaterSwitch.Service)
acc.AddService(acc.HotWater.Service)
acc.AddService(acc.SupplyFlow.Service)
return &acc
}
func newName(n string) *characteristic.Characteristic {
char := characteristic.NewName()
char.String.SetValue(n)
return char.Characteristic
}
func nilanController() nilan.Controller {
conf := nilan.CurrentConfig()
return nilan.Controller{Config: conf}
}
func updateReadings(acc *Nilan) {
c := nilanController()
r := c.FetchReadings()
s := c.FetchSettings()
if *s.CentralHeatingIsOn && !*s.CentralHeatingPaused {
acc.CentralHeatingSwitch.On.SetValue(true)
acc.SupplyFlow.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateHeat)
} else {
acc.CentralHeatingSwitch.On.SetValue(false)
acc.SupplyFlow.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateOff)
}
acc.VentilationThermostat.CurrentTemperature.SetValue(float64(r.RoomTemperature) / 10.0)
acc.VentilationThermostat.TargetTemperature.SetValue(float64(*s.DesiredRoomTemperature) / 10.0)
acc.VentilationThermostat.CurrentRelativeHumidity.SetValue(float64(r.ActualHumidity))
if *s.VentilationOnPause {
acc.VentilationThermostat.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateOff)
acc.VentilationThermostat.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateOff)
} else {
switch *s.VentilationMode {
case 0: // auto
acc.VentilationThermostat.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateAuto)
acc.VentilationThermostat.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateOff)
case 1: // cooling
acc.VentilationThermostat.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateCool)
acc.VentilationThermostat.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateCool)
case 2: // heating
acc.VentilationThermostat.TargetHeatingCoolingState.SetValue(characteristic.TargetHeatingCoolingStateHeat)
acc.VentilationThermostat.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateHeat)
}
}
if *s.VentilationOnPause {
acc.Fan.Active.SetValue(characteristic.ActiveInactive)
} else {
acc.Fan.Active.SetValue(characteristic.ActiveActive)
}
acc.Fan.RotationSpeed.SetValue((float64(*s.FanSpeed) - 100) * 25.0)
acc.HotWater.CurrentTemperature.SetValue(float64(r.DHWTankTopTemperature) / 10.0)
acc.HotWater.TargetTemperature.SetValue(float64(*s.DesiredDHWTemperature) / 10.0)
if *s.DHWProductionPaused {
acc.HotWater.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateOff)
acc.HotWaterSwitch.On.SetValue(false)
} else {
acc.HotWater.CurrentHeatingCoolingState.SetValue(characteristic.CurrentHeatingCoolingStateHeat)
acc.HotWaterSwitch.On.SetValue(true)
}
acc.SupplyFlow.CurrentTemperature.SetValue(float64(r.SupplyFlowTemperature) / 10.0)
acc.SupplyFlow.TargetTemperature.SetValue(float64(*s.SetpointSupplyTemperature) / 10.0)
acc.OutdoorTemp.CurrentTemperature.SetValue(float64(r.OutdoorTemperature) / 10.0)
}
func startUpdatingReadings(ac *Nilan, freq time.Duration) {
defer func() {
if r := recover(); r != nil {
// In case of failure: waiting and trying again
log.Printf("Sync with Nilan did fail: %v\n", r)
time.Sleep(freq)
startUpdatingReadings(ac, freq)
}
}()
for {
updateReadings(ac)
time.Sleep(freq) // 3 sec delay
}
}
func main() {
// create an accessory
info := accessory.Info{Name: "Nilan"}
ac := NewNilan(info)
go startUpdatingReadings(ac, 5*time.Second)
pin, pinDefined := os.LookupEnv("HK_PIN")
if !pinDefined {
log.Panic("HK_PIN environment variable with 8 digit PIN code must be present")
}
port, portDefined := os.LookupEnv("HK_PORT")
// configure the ip transport
config := hc.Config{Pin: pin}
if portDefined {
config.Port = port
}
t, err := hc.NewIPTransport(config, ac.Accessory)
if err != nil {
log.Panic(err)
}
hc.OnTermination(func() {
<-t.Stop()
})
t.Start()
}