HomeAssistant: dispatch CallSwitchService by entity domain (#29700) (#29706)

This commit is contained in:
andig 2026-05-06 15:41:07 +02:00 • committed by GitHub
parent 3b90182597
commit bd651da8ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 83 additions and 4 deletions

View file

@ -233,16 +233,28 @@ func domain(entity string) (string, error) {
return domain, nil
}
// CallSwitchService is a convenience method for switch services
// CallSwitchService is a convenience method for switch-like services. The
// service name depends on the entity domain: stateless button domains expose
// only `press`, while switch-style domains use `turn_on` / `turn_off`.
func (c *Connection) CallSwitchService(entity string, turnOn bool) error {
domain, err := domain(entity)
if err != nil {
return err
}
service := "turn_off"
if turnOn {
service = "turn_on"
var service string
switch domain {
case "button", "input_button":
// Buttons are stateless — they only have a press action.
if !turnOn {
return fmt.Errorf("entity %s (domain %s) has no off action", entity, domain)
}
service = "press"
default:
service = "turn_off"
if turnOn {
service = "turn_on"
}
}
data := map[string]any{

View file

@ -0,0 +1,67 @@
package homeassistant
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestConnection(baseURL string) *Connection {
return &Connection{
Helper: request.NewHelper(util.NewLogger("test")),
instance: &proxyInstance{uri: baseURL},
}
}
// TestCallSwitchService_DomainDispatch verifies that CallSwitchService picks
// the correct service per Home Assistant domain — switches use turn_on /
// turn_off, but the stateless button / input_button domains expose only
// `press`. Regression test for evcc-io/evcc#29700.
func TestCallSwitchService_DomainDispatch(t *testing.T) {
tests := []struct {
name string
entity string
turnOn bool
wantPath string
wantErrText string
}{
{"switch turn_on", "switch.foo", true, "/api/services/switch/turn_on", ""},
{"switch turn_off", "switch.foo", false, "/api/services/switch/turn_off", ""},
{"button press", "button.tesla_model_x_wake_up", true, "/api/services/button/press", ""},
{"input_button press", "input_button.bar", true, "/api/services/input_button/press", ""},
{"button no off", "button.foo", false, "", "entity button.foo (domain button) has no off action"},
{"input_button no off", "input_button.bar", false, "", "entity input_button.bar (domain input_button) has no off action"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var gotPath, gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
body, _ := io.ReadAll(r.Body)
gotBody = string(body)
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
err := newTestConnection(srv.URL).CallSwitchService(tc.entity, tc.turnOn)
if tc.wantErrText != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantErrText)
assert.Empty(t, gotPath, "must not call HA when erroring locally")
return
}
require.NoError(t, err)
assert.Equal(t, tc.wantPath, gotPath)
assert.Contains(t, gotBody, `"entity_id":"`+tc.entity+`"`)
})
}
}