Repository: bpicode/fritzctl (go)

Issue:
fritzctl temperature on/off
Is there a way to turn on/off the thermostats like direct in the Fritz!Box or at the device? In the help it says only: fritzctl temperature [value in °C] [device names] [flags]

Pull request description:
Issue #58: add support for temperature on/off
Closes #58 

Patch:
diff --git a/cmd/temperature.go b/cmd/temperature.go
index 3bdaf08..d984c6b 100644
--- a/cmd/temperature.go
+++ b/cmd/temperature.go
@@ -2,16 +2,21 @@ package cmd
 
 import (
 	"strconv"
+	"strings"
 
 	"github.com/spf13/cobra"
 )
 
 var temperatureCmd = &cobra.Command{
-	Use:     "temperature [value in °C] [device names]",
-	Short:   "Set the temperature of HKR devices",
-	Long:    "Change the temperature of one or more HKR devices.",
-	Example: "fritzctl temperature 21.0 HKR_1 HKR_2",
-	RunE:    changeTemperature,
+	Use:   "temperature [value in °C, on, off] [device names]",
+	Short: "Set the temperature of HKR devices or turn them on/off",
+	Long: "Change the temperature of one or more HKR devices 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() {
@@ -19,11 +24,22 @@ func init() {
 }
 
 func changeTemperature(cmd *cobra.Command, args []string) error {
-	assertStringSliceHasAtLeast(args, 2, "insufficient input: at least two parameters expected.")
-	temp, errorParse := strconv.ParseFloat(args[0], 64)
+	assertStringSliceHasAtLeast(args, 2, "insufficient input: at least two parameters expected.\n\n", cmd.UsageString())
+	temp, errorParse := parseTemperature(args[0])
 	assertNoError(errorParse, "cannot parse temperature value:", errorParse)
 	c := homeAutoClient()
 	err := c.Temp(temp, args[1:]...)
 	assertNoError(err, "error setting temperature:", err)
 	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
+}


Test patch:
diff --git a/cmd/temperature_test.go b/cmd/temperature_test.go
new file mode 100644
index 0000000..a042747
--- /dev/null
+++ b/cmd/temperature_test.go
@@ -0,0 +1,24 @@
+package cmd
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+// TestParseTemperature tests the user-supplied temperature interpretation.
+func TestParseTemperature(t *testing.T) {
+	assertions := assert.New(t)
+
+	twelve, err := parseTemperature("12")
+	assertions.NoError(err)
+	assertions.Equal(float64(12), twelve)
+
+	on, err := parseTemperature("on")
+	assertions.NoError(err)
+	assertions.Equal(float64(127), on)
+
+	off, err := parseTemperature("off")
+	assertions.NoError(err)
+	assertions.Equal(float64(126.5), off)
+}