This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
list_thermostats.go
120 lines (105 loc) · 3.26 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
120
package cmd
import (
"fmt"
"os"
"time"
"github.com/bpicode/fritzctl/cmd/jsonapi"
"github.com/bpicode/fritzctl/cmd/printer"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/internal/console"
"github.com/bpicode/fritzctl/logger"
"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
fritzctl list thermostats --output=json`,
RunE: listThermostats,
}
func init() {
listThermostatsCmd.Flags().StringP("output", "o", "", "specify output format")
listCmd.AddCommand(listThermostatsCmd)
}
func listThermostats(cmd *cobra.Command, _ []string) error {
c := homeAutoClient()
devs, err := c.List()
assertNoErr(err, "cannot obtain thermostats device data")
data := remapThermostats(cmd, devs.Thermostats())
logger.Success("Device data:")
printer.Print(data, os.Stdout)
return nil
}
func remapThermostats(cmd *cobra.Command, ds []fritz.Device) interface{} {
switch cmd.Flag("output").Value.String() {
case "json":
return jsonapi.NewMapper().Convert(ds)
default:
return thermostatsTable(ds)
}
}
func thermostatsTable(devs []fritz.Device) *console.Table {
table := console.NewTable(console.Headers(
"NAME",
"PRODUCT",
"PRESENT",
"LOCK (BOX/DEV)",
"MEASURED",
"OFFSET",
"WANT",
"SAVING",
"COMFORT",
"NEXT",
"STATE",
"BATTERY",
))
appendThermostats(devs, table)
return table
}
func appendThermostats(devs []fritz.Device, table *console.Table) {
for _, dev := range devs {
columns := thermostatColumns(dev)
table.Append(columns)
}
}
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, fmt.Sprintf("%s %s", 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,
fmtUnit(dev.Thermostat.FmtMeasuredTemperature, "°C"),
fmtUnit(dev.Temperature.FmtOffset, "°C"),
fmtUnit(dev.Thermostat.FmtGoalTemperature, "°C"),
fmtUnit(dev.Thermostat.FmtSavingTemperature, "°C"),
fmtUnit(dev.Thermostat.FmtComfortTemperature, "°C"),
fmtNextChange(dev.Thermostat.NextChange))
}
func fmtNextChange(n fritz.NextChange) string {
ts := n.FmtTimestamp(time.Now())
if ts == "" {
return "?"
}
return ts + " -> " + fmtUnit(n.FmtGoalTemperature, "°C")
}
func errorCode(ec string) string {
checkMark := console.Stoc(ec).Inverse()
return checkMark.String() + fritz.HkrErrorDescriptions[ec]
}