forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_switches.go
77 lines (69 loc) · 1.8 KB
/
list_switches.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
package cmd
import (
"os"
"github.com/bpicode/fritzctl/assert"
"github.com/bpicode/fritzctl/console"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var listSwitchesCmd = &cobra.Command{
Use: "switches",
Short: "List the available smart home switches",
Long: "List the available smart home devices [switches] and associated data.",
Example: "fritzctl list switches",
RunE: listSwitches,
}
func init() {
listCmd.AddCommand(listSwitchesCmd)
}
func listSwitches(cmd *cobra.Command, args []string) error {
c := clientLogin()
f := fritz.HomeAutomation(c)
devs, err := f.ListDevices()
assert.NoError(err, "cannot obtain data for smart home switches:", err)
logger.Success("Obtained device data:")
table := switchTable()
table = appendSwitches(devs, table)
table.Render()
return nil
}
func switchTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"NAME",
"MANUFACTURER",
"PRODUCTNAME",
"PRESENT",
"STATE",
"LOCK (BOX/DEV)",
"MODE",
"POWER [W]",
"ENERGY [Wh]",
"TEMP [°C]",
"OFFSET [°C]",
})
return table
}
func appendSwitches(devs *fritz.Devicelist, table *tablewriter.Table) *tablewriter.Table {
for _, dev := range devs.Switches() {
table.Append(switchColumns(dev))
}
return table
}
func switchColumns(dev fritz.Device) []string {
return []string{
dev.Name,
dev.Manufacturer,
dev.Productname,
console.IntToCheckmark(dev.Present),
console.StringToCheckmark(dev.Switch.State),
console.StringToCheckmark(dev.Switch.Lock) + "/" + console.StringToCheckmark(dev.Switch.DeviceLock),
dev.Switch.Mode,
dev.Powermeter.FmtPowerW(),
dev.Powermeter.FmtEnergyWh(),
dev.Temperature.FmtCelsius(),
dev.Temperature.FmtOffset(),
}
}