This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
list_thermostats.go
119 lines (105 loc) · 3.44 KB
/
list_thermostats.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
package cmd
import (
"os"
"time"
"github.com/bpicode/fritzctl/console"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"github.com/bpicode/fritzctl/stringutils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var listThermostatsCmd = &cobra.Command{
Use: "thermostats",
Short: "List the available smart home thermostats",
Long: "List the available smart home devices [thermostats] and associated data.",
Example: "fritzctl list thermostats",
RunE: listThermostats,
}
func init() {
listCmd.AddCommand(listThermostatsCmd)
}
func listThermostats(cmd *cobra.Command, args []string) error {
c := homeAutoClient()
devs, err := c.List()
assertNoError(err, "cannot obtain thermostats device data:", err)
logger.Success("Obtained device data:")
table := thermostatsTable()
table = appendThermostats(devs, table)
table.Render()
return nil
}
var errorCodesVsDescriptions = map[string]string{
"": "",
"0": "",
"1": " Thermostat adjustment not possible. Is the device mounted correctly?",
"2": " Valve plunger cannot be driven far enough. Possilbe solutions: Open and close the plunger a couple of times by hand. Check if the battery is too weak.",
"3": " Valve plunger cannot be moved. Is it blocked?",
"4": " Preparing installation.",
"5": " Device in mode 'INSTALLATION'. It can be mounted now.",
"6": " Device is adjusting to the valve plunger.",
}
func thermostatsTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"NAME",
"MANUFACTURER",
"PRODUCTNAME",
"PRESENT",
"LOCK (BOX/DEV)",
"MEASURED [°C]",
"OFFSET [°C]",
"WANT [°C]",
"SAVING [°C]",
"COMFORT [°C]",
"NEXT",
"STATE",
"BATTERY",
})
return table
}
func appendThermostats(devs *fritz.Devicelist, table *tablewriter.Table) *tablewriter.Table {
for _, dev := range devs.Thermostats() {
table.Append(thermostatColumns(dev))
}
return table
}
func thermostatColumns(dev fritz.Device) []string {
var columnValues []string
columnValues = appendMetadata(columnValues, dev)
columnValues = appendRuntimeFlags(columnValues, dev)
columnValues = appendTemperatureValues(columnValues, dev)
columnValues = appendRuntimeWarnings(columnValues, dev)
return columnValues
}
func appendMetadata(cols []string, dev fritz.Device) []string {
return append(cols, dev.Name, dev.Manufacturer, dev.Productname)
}
func appendRuntimeFlags(cols []string, dev fritz.Device) []string {
return append(cols,
console.IntToCheckmark(dev.Present),
console.StringToCheckmark(dev.Thermostat.Lock)+"/"+console.StringToCheckmark(dev.Thermostat.DeviceLock))
}
func appendRuntimeWarnings(cols []string, dev fritz.Device) []string {
return append(cols, errorCode(dev.Thermostat.ErrorCode),
console.Stoc(dev.Thermostat.BatteryLow).Inverse().String())
}
func appendTemperatureValues(cols []string, dev fritz.Device) []string {
return append(cols,
dev.Thermostat.FmtMeasuredTemperature(),
dev.Temperature.FmtOffset(),
dev.Thermostat.FmtGoalTemperature(),
dev.Thermostat.FmtSavingTemperature(),
dev.Thermostat.FmtComfortTemperature(),
fmtNextChange(dev.Thermostat.NextChange))
}
func fmtNextChange(n fritz.NextChange) string {
return stringutils.DefaultIfEmpty(n.FmtTimestamp(time.Now()), "?") +
" -> " +
stringutils.DefaultIfEmpty(n.FmtGoalTemperature(), "?") +
"°C"
}
func errorCode(ec string) string {
checkMark := console.Stoc(ec).Inverse()
return checkMark.String() + errorCodesVsDescriptions[ec]
}