-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add temperature command targetting climate entities (#63)
* feat: add temperature command targetting climate entities --------- Co-authored-by: Fabian Sylvester <[email protected]>
- Loading branch information
Showing
10 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/xx4h/hctl/pkg" | ||
o "github.com/xx4h/hctl/pkg/output" | ||
) | ||
|
||
func newTemperatureCmd(h *pkg.Hctl, out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "temperature", | ||
Short: "Set the temperature of a climate entity", | ||
Aliases: []string{"te", "temp"}, | ||
Args: cobra.MatchAll(cobra.ExactArgs(2)), | ||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) == 0 { | ||
return compListStates(toComplete, args, []string{"set_temperature"}, nil, "", h) | ||
} | ||
return nil, cobra.ShellCompDirectiveDefault | ||
}, | ||
Run: func(_ *cobra.Command, args []string) { | ||
obj, state, err := h.TemperatureSet(args[0], args[1]) | ||
if err != nil { | ||
o.FprintError(out, err) | ||
} | ||
o.FprintSuccessAction(out, obj, state) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/xx4h/hctl/pkg/hctltest" | ||
) | ||
|
||
func Test_newCmdTemperature(t *testing.T) { | ||
ms := hctltest.MockServer(t) | ||
h := newTestingHctl(t) | ||
if err := h.SetConfigValue("hub.url", ms.URL); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
var tests = map[string]cmdTest{ | ||
"set volume": { | ||
"temperature climate.heating 21.5", | ||
"(?m)^.*heating temperature set to 21.5", | ||
"", | ||
}, | ||
} | ||
|
||
testCmd(t, h, tests) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
pkg/hctltest/testdata/heating_climate_set_temperature_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ import ( | |
) | ||
|
||
const ( | ||
statesCount = 10 | ||
statesCount = 11 | ||
) | ||
|
||
func Test_GetStates(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rest | ||
|
||
import "fmt" | ||
|
||
func (h *Hass) TemperatureSet(obj string, temp float64) (string, string, string, error) { | ||
svc := "set_temperature" | ||
sub, obj, err := h.entityArgHandler([]string{obj}, svc) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
payload := map[string]any{ | ||
"entity_id": fmt.Sprintf("%s.%s", sub, obj), | ||
"temperature": fmt.Sprintf("%.1f", temp), | ||
} | ||
res, err := h.api("POST", fmt.Sprintf("/services/%s/%s", sub, svc), payload) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
|
||
if err := h.getResult(res); err != nil { | ||
return "", "", "", err | ||
} | ||
return obj, fmt.Sprintf("temperature set to %.1f", temp), sub, nil | ||
|
||
} |