Skip to content

Commit 93eb694

Browse files
committed
Added call to service
1 parent 5ae1147 commit 93eb694

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

cmd/api/flags.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ func (flags *Flags) Parse(args []string) (*Fn, []string, error) {
9797
// If the name of the command is the same as the name of the application
9898
flags.cmd = cmd
9999
flags.root = cmd.Name
100-
flags.fn = ""
101-
flags.args = flags.Args()
100+
if len(flags.Args()) > 0 {
101+
flags.fn = flags.Arg(0)
102+
if len(flags.Args()) > 1 {
103+
flags.args = flags.Args()[1:]
104+
}
105+
}
102106
} else if flags.NArg() > 0 {
103107
if cmd := flags.getCommandSet(flags.Arg(0)); cmd != nil {
104108
flags.cmd = cmd

cmd/api/homeassistant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +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>"},
56+
{Name: "call", Call: haCall, Description: "Call a service for an entity", MinArgs: 2, MaxArgs: 2, Syntax: "<call> <entity>"},
5757
},
5858
})
5959
}

pkg/homeassistant/services.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
// TYPES
1717

1818
type Domain struct {
19-
Domain string `json:"domain"`
20-
Services map[string]Service `json:"services,omitempty"`
19+
Domain string `json:"domain"`
20+
Services map[string]*Service `json:"services,omitempty"`
2121
}
2222

2323
type Service struct {
24-
Name string `json:"name"`
24+
Call string `json:"call,omitempty"`
25+
Name string `json:"name,omitempty"`
2526
Description string `json:"description,omitempty,wrap"`
2627
Fields map[string]Field `json:"fields,omitempty,wrap"`
2728
}
@@ -59,7 +60,7 @@ func (c *Client) Domains() ([]Domain, error) {
5960
}
6061

6162
// Return callable services for a domain
62-
func (c *Client) Services(domain string) ([]Service, error) {
63+
func (c *Client) Services(domain string) ([]*Service, error) {
6364
var response []Domain
6465
if err := c.Do(nil, &response, client.OptPath("services")); err != nil {
6566
return nil, err
@@ -69,11 +70,13 @@ func (c *Client) Services(domain string) ([]Service, error) {
6970
continue
7071
}
7172
if len(v.Services) == 0 {
72-
// No services found
73-
return []Service{}, nil
74-
} else {
75-
return maps.Values(v.Services), nil
73+
return nil, nil
7674
}
75+
// Populate the Id field
76+
for k, v := range v.Services {
77+
v.Call = k
78+
}
79+
return maps.Values(v.Services), nil
7780
}
7881
// Return not found
7982
return nil, ErrNotFound.Withf("domain not found: %q", domain)

0 commit comments

Comments
 (0)