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_switches.go
72 lines (63 loc) · 1.61 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
package cmd
import (
"os"
"github.com/bpicode/fritzctl/console"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"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 := homeAutoClient()
devs, err := c.List()
assertNoError(err, "cannot obtain data for smart home switches:", err)
logger.Success("Obtained device data:")
table := switchTable()
appendSwitches(devs, table)
table.Print(os.Stdout)
return nil
}
func switchTable() *console.Table {
return console.NewTable(console.Headers(
"NAME",
"MANUFACTURER",
"PRODUCTNAME",
"PRESENT",
"STATE",
"LOCK (BOX/DEV)",
"MODE",
"POWER [W]",
"ENERGY [Wh]",
"TEMP [°C]",
"OFFSET [°C]",
))
}
func appendSwitches(devs *fritz.Devicelist, table *console.Table) {
for _, dev := range devs.Switches() {
table.Append(switchColumns(dev))
}
}
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(),
}
}