Skip to content

Commit 5ae1147

Browse files
committed
Added service call implementation
1 parent 5f551e3 commit 5ae1147

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

cmd/api/homeassistant.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func haRegister(flags *Flags) {
5353
{Name: "domains", Call: haDomains, Description: "Enumerate entity domains"},
5454
{Name: "states", Call: haStates, Description: "Show current entity states", MaxArgs: 1, Syntax: "(<name>)"},
5555
{Name: "services", Call: haServices, Description: "Show services for an entity", MinArgs: 1, MaxArgs: 1, Syntax: "<entity>"},
56+
{Name: "call", Call: haCall, Description: "Call a service for an entity", MinArgs: 2, MaxArgs: 2, Syntax: "<service> <entity>"},
5657
},
5758
})
5859
}
@@ -123,6 +124,16 @@ func haServices(_ context.Context, w *tablewriter.Writer, args []string) error {
123124
return w.Write(services)
124125
}
125126

127+
func haCall(_ context.Context, w *tablewriter.Writer, args []string) error {
128+
service := args[0]
129+
entity := args[1]
130+
states, err := haClient.Call(service, entity)
131+
if err != nil {
132+
return err
133+
}
134+
return w.Write(states)
135+
}
136+
126137
///////////////////////////////////////////////////////////////////////////////
127138
// PRIVATE METHODS
128139

pkg/homeassistant/services.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package homeassistant
22

33
import (
44
"encoding/json"
5+
"strings"
56

67
// Packages
78
"github.com/mutablelogic/go-client"
@@ -39,6 +40,10 @@ type Selector struct {
3940
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
4041
}
4142

43+
type reqCall struct {
44+
Entity string `json:"entity_id"`
45+
}
46+
4247
///////////////////////////////////////////////////////////////////////////////
4348
// API CALLS
4449

@@ -74,6 +79,30 @@ func (c *Client) Services(domain string) ([]Service, error) {
7479
return nil, ErrNotFound.Withf("domain not found: %q", domain)
7580
}
7681

82+
// Call a service for an entity. Returns a list of states that have
83+
// changed while the service was being executed.
84+
// TODO: This is a placeholder implementation, and requires fields to
85+
// be passed in the request
86+
func (c *Client) Call(service, entity string) ([]State, error) {
87+
domain := domainForEntity(entity)
88+
if domain == "" {
89+
return nil, ErrBadParameter.Withf("Invalid entity: %q", entity)
90+
}
91+
92+
// Call the service
93+
var response []State
94+
if payload, err := client.NewJSONRequest(reqCall{
95+
Entity: entity,
96+
}); err != nil {
97+
return nil, err
98+
} else if err := c.Do(payload, &response, client.OptPath("services", domain, service)); err != nil {
99+
return nil, err
100+
}
101+
102+
// Return success
103+
return response, nil
104+
}
105+
77106
///////////////////////////////////////////////////////////////////////////////
78107
// STRINGIFY
79108

@@ -91,3 +120,15 @@ func (v Field) String() string {
91120
data, _ := json.MarshalIndent(v, "", " ")
92121
return string(data)
93122
}
123+
124+
///////////////////////////////////////////////////////////////////////////////
125+
// PRIVATE METHODS
126+
127+
func domainForEntity(entity string) string {
128+
parts := strings.SplitN(entity, ".", 2)
129+
if len(parts) == 2 {
130+
return parts[0]
131+
} else {
132+
return ""
133+
}
134+
}

0 commit comments

Comments
 (0)