-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
460 lines (422 loc) · 16.3 KB
/
main.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math"
"os"
"path"
"strconv"
"time"
"github.com/avast/retry-go"
wx "github.com/cdzombak/libwx"
"github.com/influxdata/influxdb-client-go/v2"
"ecobee_influx_connector/ecobee" // taken from https://github.com/rspier/go-ecobee and lightly customized
)
// Config describes the ecobee_influx_connector program's configuration.
// It is used to parse the configuration JSON file.
type Config struct {
APIKey string `json:"api_key"`
WorkDir string `json:"work_dir,omitempty"`
ThermostatID string `json:"thermostat_id"`
InfluxServer string `json:"influx_server"`
InfluxOrg string `json:"influx_org,omitempty"`
InfluxUser string `json:"influx_user,omitempty"`
InfluxPass string `json:"influx_password,omitempty"`
InfluxToken string `json:"influx_token,omitempty"`
InfluxBucket string `json:"influx_bucket"`
InfluxHealthCheckDisabled bool `json:"influx_health_check_disabled"`
WriteHeatPump1 bool `json:"write_heat_pump_1"`
WriteHeatPump2 bool `json:"write_heat_pump_2"`
WriteAuxHeat1 bool `json:"write_aux_heat_1"`
WriteAuxHeat2 bool `json:"write_aux_heat_2"`
WriteCool1 bool `json:"write_cool_1"`
WriteCool2 bool `json:"write_cool_2"`
WriteHumidifier bool `json:"write_humidifier"`
WriteDehumidifier bool `json:"write_dehumidifier"`
AlwaysWriteWeather bool `json:"always_write_weather_as_current"`
}
const (
thermostatNameTag = "thermostat_name"
source = "ecobee"
sourceTag = "data_source"
ecobeeWeatherMeasurementName = "ecobee_weather"
)
var version = "<dev>"
func main() {
configFile := flag.String("config", "", "Configuration JSON file.")
listThermostats := flag.Bool("list-thermostats", false, "List available thermostats, then exit.")
printVersion := flag.Bool("version", false, "Print version and exit.")
flag.Parse()
if *printVersion {
fmt.Println(version)
os.Exit(0)
}
if *configFile == "" {
fmt.Println("-config is required.")
os.Exit(1)
}
config := Config{}
cfgBytes, err := os.ReadFile(*configFile)
if err != nil {
log.Fatalf("Unable to read config file '%s': %s", *configFile, err)
}
if err = json.Unmarshal(cfgBytes, &config); err != nil {
log.Fatalf("Unable to parse config file '%s': %s", *configFile, err)
}
if config.APIKey == "" {
log.Fatal("api_key must be set in the config file.")
}
if config.WorkDir == "" {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Unable to get current working directory: %s", err)
}
config.WorkDir = wd
}
client := ecobee.NewClient(config.APIKey, path.Join(config.WorkDir, "ecobee-cred-cache"))
if *listThermostats {
s := ecobee.Selection{
SelectionType: "registered",
}
ts, err := client.GetThermostats(s)
if err != nil {
log.Fatal(err)
}
for _, t := range ts {
fmt.Printf("'%s': ID %s\n", t.Name, t.Identifier)
}
os.Exit(0)
}
if config.ThermostatID == "" {
log.Fatalf("thermostat_id must be set in the config file.")
}
if config.InfluxBucket == "" || config.InfluxServer == "" {
log.Fatalf("influx_server and influx_bucket must be set in the config file.")
}
const influxTimeout = 3 * time.Second
authString := ""
if config.InfluxUser != "" || config.InfluxPass != "" {
authString = fmt.Sprintf("%s:%s", config.InfluxUser, config.InfluxPass)
} else if config.InfluxToken != "" {
authString = fmt.Sprintf("%s", config.InfluxToken)
}
influxClient := influxdb2.NewClient(config.InfluxServer, authString)
if !config.InfluxHealthCheckDisabled {
ctx, cancel := context.WithTimeout(context.Background(), influxTimeout)
defer cancel()
health, err := influxClient.Health(ctx)
if err != nil {
log.Fatalf("failed to check InfluxDB health: %v", err)
}
if health.Status != "pass" {
log.Fatalf("InfluxDB did not pass health check: status %s; message '%s'", health.Status, *health.Message)
}
}
influxWriteAPI := influxClient.WriteAPIBlocking(config.InfluxOrg, config.InfluxBucket)
_ = influxWriteAPI
lastWrittenRuntimeInterval := 0
lastWrittenWeather := time.Time{}
lastWrittenSensors := time.Time{}
doUpdate := func() {
if err := retry.Do(
func() error {
t, err := client.GetThermostat(config.ThermostatID)
if err != nil {
return err
}
// Air quality related values are only in the current runtime,
// thus they need to be handled outside the extended runtime section
currentRuntimeReportTime, err := time.Parse("2006-01-02 15:04:05", t.Runtime.LastStatusModified)
if err != nil {
return err
}
if err := retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), influxTimeout)
defer cancel()
actualAQAccuracy := t.Runtime.ActualAQAccuracy
actualAQScore := t.Runtime.ActualAQScore
actualCO2 := t.Runtime.ActualCO2
actualVOC := t.Runtime.ActualVOC
fmt.Printf("Air quality at %s:\n", currentRuntimeReportTime)
fmt.Printf("\tcurrent co2: %d\n\tcurrent voc: %d\n",
actualCO2, actualVOC)
fields := map[string]interface{}{
"airquality_accuracy": actualAQAccuracy,
"airquality_score": actualAQScore,
"co2": actualCO2,
"voc": actualVOC,
}
err := influxWriteAPI.WritePoint(ctx,
influxdb2.NewPoint(
"ecobee_air_quality",
map[string]string{thermostatNameTag: t.Name}, // tags
fields,
currentRuntimeReportTime,
))
if err != nil {
return err
}
return nil
}, retry.Attempts(3), retry.Delay(1*time.Second)); err != nil {
return err
}
latestRuntimeInterval := t.ExtendedRuntime.RuntimeInterval
log.Printf("latest runtime interval available is %d\n", latestRuntimeInterval)
// In the absence of a time zone indicator, Parse returns a time in UTC.
baseReportTime, err := time.Parse("2006-01-02 15:04:05", t.ExtendedRuntime.LastReadingTimestamp)
if err != nil {
return err
}
for i := 0; i < 3; i++ {
reportTime := baseReportTime
if i == 0 {
reportTime = reportTime.Add(-5 * time.Minute)
}
if i == 2 {
reportTime = reportTime.Add(5 * time.Minute)
}
currentTemp := wx.TempF(float64(t.ExtendedRuntime.ActualTemperature[i]) / 10.0)
currentHumidity := t.ExtendedRuntime.ActualHumidity[i]
heatSetPoint := wx.TempF(float64(t.ExtendedRuntime.DesiredHeat[i]) / 10.0)
coolSetPoint := wx.TempF(float64(t.ExtendedRuntime.DesiredCool[i]) / 10.0)
humiditySetPoint := t.ExtendedRuntime.DesiredHumidity[i]
demandMgmtOffset := wx.TempF(float64(t.ExtendedRuntime.DmOffset[i]) / 10.0)
hvacMode := t.ExtendedRuntime.HvacMode[i] // string :(
heatPump1RunSec := t.ExtendedRuntime.HeatPump1[i]
heatPump2RunSec := t.ExtendedRuntime.HeatPump2[i]
auxHeat1RunSec := t.ExtendedRuntime.AuxHeat1[i]
auxHeat2RunSec := t.ExtendedRuntime.AuxHeat2[i]
cool1RunSec := t.ExtendedRuntime.Cool1[i]
cool2RunSec := t.ExtendedRuntime.Cool2[i]
fanRunSec := t.ExtendedRuntime.Fan[i]
humidifierRunSec := t.ExtendedRuntime.Humidifier[i]
dehumidifierRunSec := t.ExtendedRuntime.Dehumidifier[i]
fmt.Printf("Thermostat conditions at %s:\n", reportTime)
fmt.Printf("\tcurrent temperature: %.1f degF (%.1f degC)\n\theat set point: %.1f degF (%.1f degC)"+
"\n\tcool set point: %.1f degF (%.1f degC)\n\tdemand management offset: %.1f (%.1f degC)\n",
currentTemp, currentTemp.C(), heatSetPoint, heatSetPoint.C(),
coolSetPoint, coolSetPoint.C(), demandMgmtOffset, demandMgmtOffset.C())
fmt.Printf("\tcurrent humidity: %d%%\n\thumidity set point: %d\n\tHVAC mode: %s\n",
currentHumidity, humiditySetPoint, hvacMode)
fmt.Printf("\tfan runtime: %d seconds\n\thumidifier runtime: %d seconds\n\tdehumidifier runtime: %d seconds\n",
fanRunSec, humidifierRunSec, dehumidifierRunSec)
fmt.Printf("\theat pump 1 runtime: %d seconds\n\theat pump 2 runtime: %d seconds\n",
heatPump1RunSec, heatPump2RunSec)
fmt.Printf("\theat 1 runtime: %d seconds\n\theat 2 runtime: %d seconds\n",
auxHeat1RunSec, auxHeat2RunSec)
fmt.Printf("\tcool 1 runtime: %d seconds\n\tcool 2 runtime: %d seconds\n",
cool1RunSec, cool2RunSec)
if latestRuntimeInterval != lastWrittenRuntimeInterval {
if err := retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), influxTimeout)
defer cancel()
fields := map[string]interface{}{
"temperature": currentTemp.Unwrap(),
"temperature_f": currentTemp.Unwrap(),
"temperature_c": currentTemp.C().Unwrap(),
"humidity": currentHumidity,
"heat_set_point": heatSetPoint.Unwrap(),
"heat_set_point_f": heatSetPoint.Unwrap(),
"heat_set_point_c": heatSetPoint.C().Unwrap(),
"cool_set_point": coolSetPoint.Unwrap(),
"cool_set_point_f": coolSetPoint.Unwrap(),
"cool_set_point_c": coolSetPoint.C().Unwrap(),
"demand_mgmt_offset": demandMgmtOffset.Unwrap(),
"demand_mgmt_offset_f": demandMgmtOffset.Unwrap(),
"demand_mgmt_offset_c": demandMgmtOffset.C().Unwrap(),
"fan_run_time": fanRunSec,
}
if config.WriteHumidifier || config.WriteDehumidifier {
fields["humidity_set_point"] = humiditySetPoint
}
if config.WriteHumidifier {
fields["humidifier_run_time"] = humidifierRunSec
}
if config.WriteDehumidifier {
fields["dehumidifier_run_time"] = dehumidifierRunSec
}
if config.WriteAuxHeat1 {
fields["aux_heat_1_run_time"] = auxHeat1RunSec
}
if config.WriteAuxHeat2 {
fields["aux_heat_2_run_time"] = auxHeat2RunSec
}
if config.WriteHeatPump1 {
fields["heat_pump_1_run_time"] = heatPump1RunSec
}
if config.WriteHeatPump2 {
fields["heat_pump_2_run_time"] = heatPump2RunSec
}
if config.WriteCool1 {
fields["cool_1_run_time"] = cool1RunSec
}
if config.WriteCool2 {
fields["cool_2_run_time"] = cool2RunSec
}
err := influxWriteAPI.WritePoint(ctx,
influxdb2.NewPoint(
"ecobee_runtime",
map[string]string{thermostatNameTag: t.Name},
fields,
reportTime,
))
if err != nil {
return err
}
return nil
}, retry.Attempts(3), retry.Delay(1*time.Second)); err != nil {
return err
}
}
}
lastWrittenRuntimeInterval = latestRuntimeInterval
// assume t.LastModified for these:
sensorTime, err := time.Parse("2006-01-02 15:04:05", t.UtcTime)
if err != nil {
return err
}
for _, sensor := range t.RemoteSensors {
name := sensor.Name
var temp wx.TempF
var presence, presenceSupported bool
for _, c := range sensor.Capability {
if c.Type == "temperature" {
tempInt, err := strconv.Atoi(c.Value)
if err != nil {
log.Printf("error reading temp '%s' for sensor %s: %s", c.Value, sensor.Name, err)
} else {
temp = wx.TempF(float64(tempInt) / 10.0)
}
} else if c.Type == "occupancy" {
presenceSupported = true
presence = c.Value == "true"
}
}
fmt.Printf("Sensor '%s' at %s:\n", name, sensorTime)
fmt.Printf("\ttemperature: %.1f degF (%.1f degC)\n", temp, temp.C())
if presenceSupported {
fmt.Printf("\toccupied: %t\n", presence)
}
if temp == 0.0 {
// no temp reading from this sensor, so skip writing it to Influx
continue
}
if sensorTime != lastWrittenSensors {
if err := retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), influxTimeout)
defer cancel()
fields := map[string]interface{}{
"temperature": temp.Unwrap(),
"temperature_f": temp.Unwrap(),
"temperature_c": temp.C().Unwrap(),
}
if presenceSupported {
fields["occupied"] = presence
}
err := influxWriteAPI.WritePoint(ctx,
influxdb2.NewPoint(
"ecobee_sensor",
map[string]string{
thermostatNameTag: t.Name,
"sensor_name": sensor.Name,
"sensor_id": sensor.ID,
}, // tags
fields,
sensorTime,
))
if err != nil {
return err
}
return nil
}, retry.Attempts(3), retry.Delay(1*time.Second)); err != nil {
return err
}
}
}
lastWrittenSensors = sensorTime
weatherTime, err := time.Parse("2006-01-02 15:04:05", t.Weather.Timestamp)
if err != nil {
return err
}
outdoorTemp := wx.TempF(float64(t.Weather.Forecasts[0].Temperature) / 10.0)
pressureMillibar := wx.PressureMb(t.Weather.Forecasts[0].Pressure)
outdoorHumidity := wx.ClampedRelHumidity(t.Weather.Forecasts[0].RelativeHumidity)
dewpoint := wx.TempF(float64(t.Weather.Forecasts[0].Dewpoint) / 10.0)
windSpeedMph := wx.SpeedMph(t.Weather.Forecasts[0].WindSpeed)
windBearing := t.Weather.Forecasts[0].WindBearing
visibilityMeters := wx.Meter(t.Weather.Forecasts[0].Visibility)
visibilityMiles := visibilityMeters.Miles()
windChill := wx.WindChillF(outdoorTemp, windSpeedMph)
weatherSymbol := t.Weather.Forecasts[0].WeatherSymbol
sky := t.Weather.Forecasts[0].Sky
fmt.Printf("Weather at %s:\n", weatherTime)
fmt.Printf("\ttemperature: %.1f degF (%.1f degC)\n\tpressure: %.0f mb\n\thumidity: %d%%\n\tdew point: %.1f degF (%.1f degC)",
outdoorTemp, outdoorTemp.C(), pressureMillibar, outdoorHumidity, dewpoint, dewpoint.C())
fmt.Printf("\n\twind: %d at %.0f mph\n\twind chill: %.1f degF\n\tvisibility: %.1f miles\nweather symbol: %d\nsky: %d",
windBearing, windSpeedMph, windChill, visibilityMiles, weatherSymbol, sky)
if weatherTime != lastWrittenWeather || config.AlwaysWriteWeather {
if err := retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), influxTimeout)
defer cancel()
pointTime := weatherTime
if config.AlwaysWriteWeather {
pointTime = time.Now()
}
err := influxWriteAPI.WritePoint(ctx,
influxdb2.NewPoint(
ecobeeWeatherMeasurementName,
map[string]string{ // tags
thermostatNameTag: t.Name,
sourceTag: source,
},
map[string]interface{}{ // fields
"outdoor_temp": outdoorTemp.Unwrap(),
"outdoor_temp_f": outdoorTemp.Unwrap(),
"outdoor_temp_c": outdoorTemp.C().Unwrap(),
"outdoor_humidity": outdoorHumidity.Unwrap(),
"barometric_pressure_mb": int(math.Round(pressureMillibar.Unwrap())), // we get int precision from Ecobee, and historically this is written as int
"barometric_pressure_inHg": pressureMillibar.InHg().Unwrap(),
"dew_point": dewpoint.Unwrap(),
"dew_point_f": dewpoint.Unwrap(),
"dew_point_c": dewpoint.C().Unwrap(),
"wind_speed": int(math.Round(windSpeedMph.Unwrap())), // we get int precision from Ecobee, and historically this is written as int
"wind_speed_mph": windSpeedMph.Unwrap(),
"wind_bearing": windBearing,
"visibility_mi": visibilityMiles.Unwrap(),
"visibility_km": visibilityMiles.Km().Unwrap(),
"recommended_max_indoor_humidity": wx.IndoorHumidityRecommendationF(outdoorTemp).Unwrap(),
"wind_chill_f": windChill.Unwrap(),
"wind_chill_c": windChill.C().Unwrap(),
"weather_symbol": weatherSymbol,
"sky": sky,
},
pointTime,
))
if err != nil {
return err
}
lastWrittenWeather = weatherTime
return nil
}, retry.Attempts(3), retry.Delay(1*time.Second)); err != nil {
return err
}
}
return nil
},
retry.Attempts(3),
retry.Delay(5*time.Second),
); err != nil {
log.Fatal(err)
}
}
doUpdate()
for {
select {
case <-time.Tick(5 * time.Minute):
doUpdate()
}
}
}