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
71 lines (62 loc) · 1.59 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
package cmd
import (
"fmt"
"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(_ *cobra.Command, _ []string) error {
c := homeAutoClient()
devs, err := c.List()
assertNoErr(err, "cannot obtain data for smart home switches")
logger.Success("Device data:")
table := switchTable()
appendSwitches(devs, table)
table.Print(os.Stdout)
return nil
}
func switchTable() *console.Table {
return console.NewTable(console.Headers(
"NAME",
"PRODUCT",
"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,
fmt.Sprintf("%s %s", 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(),
}
}