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
/
temperature.go
92 lines (83 loc) · 2.73 KB
/
temperature.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
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"github.com/spf13/cobra"
)
var temperatureCmd = &cobra.Command{
Use: "temperature [value in °C, on, off, sav, comf] [device/group names]",
Short: "Set the temperature of HKR devices/groups or turn them on/off",
Long: "Change the temperature of HKR devices/groups by supplying the desired value in °C. " +
"When turning HKR devices on/off, replace the value by 'on'/'off' respectively." +
"To reset each devices to its comfort/saving temperature, replace the value by 'comf'/'sav'.",
Example: `fritzctl temperature 21.0 HKR_1 HKR_2
fritzctl temperature off HKR_1
fritzctl temperature on HKR_2
fritzctl temperature comf HK1 HKR_2
fritzctl temperature sav HK1 HKR_2
`,
RunE: changeTemperature,
}
func init() {
RootCmd.AddCommand(temperatureCmd)
}
func changeTemperature(cmd *cobra.Command, args []string) error {
assertMinLen(args, 2, "insufficient input: at least two parameters expected.\n\n", cmd.UsageString())
val := args[0]
if strings.EqualFold(val, "sav") || strings.EqualFold(val, "saving") {
changeByCallback(func(t fritz.Thermostat) string {
return t.FmtSavingTemperature()
}, args[1:]...)
return nil
}
if strings.EqualFold(val, "comf") || strings.EqualFold(val, "comfort") {
changeByCallback(func(t fritz.Thermostat) string {
return t.FmtComfortTemperature()
}, args[1:]...)
return nil
}
changeByValue(nil, val, args[1:]...)
logger.Info("It may take a few minutes until the changes propagate to the end device(s)")
return nil
}
func changeByCallback(supplier func(t fritz.Thermostat) string, names ...string) {
c := homeAutoClient(fritz.Caching(true))
devices, err := c.List()
assertNoErr(err, "cannot list available devices")
for _, name := range names {
device := deviceWithName(name, devices.Thermostats())
assertTrue(device != nil, fmt.Sprintf("device with name '%s' not found", name))
changeByValue(c, supplier(device.Thermostat), name)
}
logger.Info("It may take a few minutes until the changes propagate to the end device(s)")
}
func deviceWithName(name string, list []fritz.Device) *fritz.Device {
for _, d := range list {
if d.Name == name {
return &d
}
}
return nil
}
func changeByValue(c fritz.HomeAuto, val string, names ...string) {
temp, err := parseTemperature(val)
assertNoErr(err, "cannot parse temperature value")
if c == nil {
c = homeAutoClient()
}
err = c.Temp(temp, names...)
assertNoErr(err, "error setting temperature")
}
func parseTemperature(s string) (float64, error) {
if strings.EqualFold(s, "off") {
return 126.5, nil
}
if strings.EqualFold(s, "on") {
return 127.0, nil
}
temp, errorParse := strconv.ParseFloat(s, 64)
return temp, errorParse
}