@@ -2,6 +2,7 @@ package homeassistant
2
2
3
3
import (
4
4
"encoding/json"
5
+ "strings"
5
6
6
7
// Packages
7
8
"github.com/mutablelogic/go-client"
@@ -39,6 +40,10 @@ type Selector struct {
39
40
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
40
41
}
41
42
43
+ type reqCall struct {
44
+ Entity string `json:"entity_id"`
45
+ }
46
+
42
47
///////////////////////////////////////////////////////////////////////////////
43
48
// API CALLS
44
49
@@ -74,6 +79,30 @@ func (c *Client) Services(domain string) ([]Service, error) {
74
79
return nil , ErrNotFound .Withf ("domain not found: %q" , domain )
75
80
}
76
81
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
+
77
106
///////////////////////////////////////////////////////////////////////////////
78
107
// STRINGIFY
79
108
@@ -91,3 +120,15 @@ func (v Field) String() string {
91
120
data , _ := json .MarshalIndent (v , "" , " " )
92
121
return string (data )
93
122
}
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