forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.go
45 lines (39 loc) · 1.21 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
package cmd
import (
"strconv"
"strings"
"github.com/spf13/cobra"
)
var temperatureCmd = &cobra.Command{
Use: "temperature [value in °C, on, off] [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.",
Example: `fritzctl temperature 21.0 HKR_1 HKR_2
fritzctl temperature off HKR_1
fritzctl temperature on 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())
temp, err := parseTemperature(args[0])
assertNoErr(err, "cannot parse temperature value")
c := homeAutoClient()
err = c.Temp(temp, args[1:]...)
assertNoErr(err, "error setting temperature")
return nil
}
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
}