From 84f9726b09050d32106953b83450d8d3cdc8c5f2 Mon Sep 17 00:00:00 2001 From: Jakub Zimny Date: Mon, 20 Jan 2025 23:32:12 +0100 Subject: [PATCH 1/4] Add functionality to list entities from REST endpoint --- internal/http/http.go | 8 ++++++++ state.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/internal/http/http.go b/internal/http/http.go index 8d9c9f7..71c9eaf 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -40,6 +40,14 @@ func (c *HttpClient) GetState(entityId string) ([]byte, error) { return resp, nil } +func (c *HttpClient) States() ([]byte, error) { + resp, err := get(c.url+"/states", c.token) + if err != nil { + return nil, err + } + return resp, nil +} + func get(url, token string) ([]byte, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/state.go b/state.go index 2f1e700..4b95c08 100644 --- a/state.go +++ b/state.go @@ -16,6 +16,7 @@ type State interface { BeforeSunrise(...DurationString) bool AfterSunset(...DurationString) bool BeforeSunset(...DurationString) bool + List() ([]EntityState, error) Get(entityId string) (EntityState, error) Equals(entityId, state string) (bool, error) } @@ -74,6 +75,16 @@ func (s *StateImpl) Get(entityId string) (EntityState, error) { return es, err } +func (s *StateImpl) List() ([]EntityState, error) { + resp, err := s.httpClient.States() + if err != nil { + return nil, err + } + es := []EntityState{} + err = json.Unmarshal(resp, &es) + return es, err +} + func (s *StateImpl) Equals(entityId string, expectedState string) (bool, error) { currentState, err := s.Get(entityId) if err != nil { From 5b43c79783e95062a4cabc171bb037298b12f128 Mon Sep 17 00:00:00 2001 From: Jakub Zimny Date: Mon, 20 Jan 2025 23:38:56 +0100 Subject: [PATCH 2/4] fix compilation error --- checkers_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checkers_test.go b/checkers_test.go index 22dd451..8abc678 100644 --- a/checkers_test.go +++ b/checkers_test.go @@ -13,6 +13,7 @@ type MockState struct { EqualsError bool GetReturn EntityState GetError bool + AllEntities []EntityState } func (s MockState) AfterSunrise(_ ...DurationString) bool { @@ -33,6 +34,9 @@ func (s MockState) Get(eid string) (EntityState, error) { } return s.GetReturn, nil } +func (s MockState) List() ([]EntityState, error) { + return s.AllEntities, nil +} func (s MockState) Equals(eid, state string) (bool, error) { if s.EqualsError { return false, errors.New("some error") From 98e3583c6f3eee4066010e9a9ce185d33e5a79c2 Mon Sep 17 00:00:00 2001 From: Jakub Zimny Date: Mon, 20 Jan 2025 23:39:53 +0100 Subject: [PATCH 3/4] No need to add as field struct --- checkers_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/checkers_test.go b/checkers_test.go index 8abc678..de55ddf 100644 --- a/checkers_test.go +++ b/checkers_test.go @@ -13,7 +13,6 @@ type MockState struct { EqualsError bool GetReturn EntityState GetError bool - AllEntities []EntityState } func (s MockState) AfterSunrise(_ ...DurationString) bool { @@ -35,7 +34,7 @@ func (s MockState) Get(eid string) (EntityState, error) { return s.GetReturn, nil } func (s MockState) List() ([]EntityState, error) { - return s.AllEntities, nil + return []EntityState{}, nil } func (s MockState) Equals(eid, state string) (bool, error) { if s.EqualsError { From 7f279c688c0446cf00b7baf71054a6934f9d0d84 Mon Sep 17 00:00:00 2001 From: Jakub Zimny Date: Wed, 22 Jan 2025 08:39:55 +0100 Subject: [PATCH 4/4] address PR comments --- checkers_test.go | 2 +- state.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/checkers_test.go b/checkers_test.go index de55ddf..8a659d2 100644 --- a/checkers_test.go +++ b/checkers_test.go @@ -33,7 +33,7 @@ func (s MockState) Get(eid string) (EntityState, error) { } return s.GetReturn, nil } -func (s MockState) List() ([]EntityState, error) { +func (s MockState) ListEntities() ([]EntityState, error) { return []EntityState{}, nil } func (s MockState) Equals(eid, state string) (bool, error) { diff --git a/state.go b/state.go index 4b95c08..3d7430c 100644 --- a/state.go +++ b/state.go @@ -16,7 +16,7 @@ type State interface { BeforeSunrise(...DurationString) bool AfterSunset(...DurationString) bool BeforeSunset(...DurationString) bool - List() ([]EntityState, error) + ListEntities() ([]EntityState, error) Get(entityId string) (EntityState, error) Equals(entityId, state string) (bool, error) } @@ -75,7 +75,9 @@ func (s *StateImpl) Get(entityId string) (EntityState, error) { return es, err } -func (s *StateImpl) List() ([]EntityState, error) { +// ListEntities returns a list of all entities in Home Assistant. +// see rest documentation for more details: https://developers.home-assistant.io/docs/api/rest/#actions +func (s *StateImpl) ListEntities() ([]EntityState, error) { resp, err := s.httpClient.States() if err != nil { return nil, err