mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 01:15:10 -06:00
@@ -33,6 +33,9 @@ func (s MockState) Get(eid string) (EntityState, error) {
|
||||
}
|
||||
return s.GetReturn, nil
|
||||
}
|
||||
func (s MockState) ListEntities() ([]EntityState, error) {
|
||||
return []EntityState{}, nil
|
||||
}
|
||||
func (s MockState) Equals(eid, state string) (bool, error) {
|
||||
if s.EqualsError {
|
||||
return false, errors.New("some error")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
13
state.go
13
state.go
@@ -16,6 +16,7 @@ type State interface {
|
||||
BeforeSunrise(...DurationString) bool
|
||||
AfterSunset(...DurationString) bool
|
||||
BeforeSunset(...DurationString) bool
|
||||
ListEntities() ([]EntityState, error)
|
||||
Get(entityId string) (EntityState, error)
|
||||
Equals(entityId, state string) (bool, error)
|
||||
}
|
||||
@@ -74,6 +75,18 @@ func (s *StateImpl) Get(entityId string) (EntityState, error) {
|
||||
return es, err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user