@@ -2,8 +2,6 @@ package main
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "slices"
7
5
"strings"
8
6
"time"
9
7
@@ -16,16 +14,19 @@ import (
16
14
// TYPES
17
15
18
16
type haEntity struct {
19
- Id string `json:"entity_id"`
17
+ Id string `json:"entity_id,width:40 "`
20
18
Name string `json:"name,omitempty"`
21
19
Class string `json:"class,omitempty"`
20
+ Domain string `json:"domain,omitempty"`
22
21
State string `json:"state,omitempty"`
23
22
Attributes map [string ]interface {} `json:"attributes,omitempty,wrap"`
24
- UpdatedAt time.Time `json:"last_updated,omitempty"`
23
+ UpdatedAt time.Time `json:"last_updated,omitempty,width:34"`
24
+ ChangedAt time.Time `json:"last_changed,omitempty,width:34"`
25
25
}
26
26
27
- type haClass struct {
28
- Class string `json:"class,omitempty"`
27
+ type haDomain struct {
28
+ Name string `json:"domain"`
29
+ Services string `json:"services,omitempty"`
29
30
}
30
31
31
32
///////////////////////////////////////////////////////////////////////////////
@@ -49,8 +50,10 @@ func haRegister(flags *Flags) {
49
50
Description : "Information from home assistant" ,
50
51
Parse : haParse ,
51
52
Fn : []Fn {
52
- {Name : "classes" , Call : haClasses , Description : "Return entity classes" },
53
- {Name : "states" , Call : haStates , Description : "Return entity states" },
53
+ {Name : "domains" , Call : haDomains , Description : "Enumerate entity domains" },
54
+ {Name : "states" , Call : haStates , Description : "Show current entity states" , MaxArgs : 1 , Syntax : "(<name>)" },
55
+ {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 : "<call> <entity>" },
54
57
},
55
58
})
56
59
}
@@ -71,14 +74,25 @@ func haParse(flags *Flags, opts ...client.ClientOpt) error {
71
74
// METHODS
72
75
73
76
func haStates (_ context.Context , w * tablewriter.Writer , args []string ) error {
74
- if states , err := haGetStates (args ); err != nil {
77
+ var result []haEntity
78
+ states , err := haGetStates (nil )
79
+ if err != nil {
75
80
return err
76
- } else {
77
- return w .Write (states )
78
81
}
82
+
83
+ for _ , state := range states {
84
+ if len (args ) == 1 {
85
+ if ! haMatchString (args [0 ], state .Name , state .Id ) {
86
+ continue
87
+ }
88
+
89
+ }
90
+ result = append (result , state )
91
+ }
92
+ return w .Write (result )
79
93
}
80
94
81
- func haClasses (_ context.Context , w * tablewriter.Writer , args []string ) error {
95
+ func haDomains (_ context.Context , w * tablewriter.Writer , args []string ) error {
82
96
states , err := haGetStates (nil )
83
97
if err != nil {
84
98
return err
@@ -89,17 +103,52 @@ func haClasses(_ context.Context, w *tablewriter.Writer, args []string) error {
89
103
classes [state .Class ] = true
90
104
}
91
105
92
- result := []haClass {}
106
+ result := []haDomain {}
93
107
for c := range classes {
94
- result = append (result , haClass {Class : c })
108
+ result = append (result , haDomain {
109
+ Name : c ,
110
+ })
95
111
}
96
112
return w .Write (result )
97
113
}
98
114
115
+ func haServices (_ context.Context , w * tablewriter.Writer , args []string ) error {
116
+ service , err := haClient .State (args [0 ])
117
+ if err != nil {
118
+ return err
119
+ }
120
+ services , err := haClient .Services (service .Domain ())
121
+ if err != nil {
122
+ return err
123
+ }
124
+ return w .Write (services )
125
+ }
126
+
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
+
99
137
///////////////////////////////////////////////////////////////////////////////
100
138
// PRIVATE METHODS
101
139
102
- func haGetStates (classes []string ) ([]haEntity , error ) {
140
+ func haMatchString (q string , values ... string ) bool {
141
+ q = strings .ToLower (q )
142
+ for _ , v := range values {
143
+ v = strings .ToLower (v )
144
+ if strings .Contains (v , q ) {
145
+ return true
146
+ }
147
+ }
148
+ return false
149
+ }
150
+
151
+ func haGetStates (domains []string ) ([]haEntity , error ) {
103
152
var result []haEntity
104
153
105
154
// Get states from the remote service
@@ -112,37 +161,29 @@ func haGetStates(classes []string) ([]haEntity, error) {
112
161
for _ , state := range states {
113
162
entity := haEntity {
114
163
Id : state .Entity ,
115
- State : state .State ,
164
+ Name : state .Name (),
165
+ Domain : state .Domain (),
166
+ Class : state .Class (),
167
+ State : state .Value (),
116
168
Attributes : state .Attributes ,
117
- UpdatedAt : state .LastChanged ,
169
+ UpdatedAt : state .LastUpdated ,
170
+ ChangedAt : state .LastChanged ,
118
171
}
119
172
120
- // Ignore entities without state
121
- if entity .State == "" || entity . State == "unknown" || entity . State == "unavailable" {
173
+ // Ignore any fields where the state is empty
174
+ if entity .State == "" {
122
175
continue
123
176
}
124
177
125
- // Set entity type and name from entity id
126
- parts := strings .SplitN (entity .Id , "." , 2 )
127
- if len (parts ) >= 2 {
128
- entity .Class = strings .ToLower (parts [0 ])
129
- entity .Name = parts [1 ]
130
- }
131
-
132
- // Set entity type from device class
133
- if t , exists := state .Attributes ["device_class" ]; exists {
134
- entity .Class = fmt .Sprint (t )
178
+ // Add unit of measurement
179
+ if unit := state .UnitOfMeasurement (); unit != "" {
180
+ entity .State += " " + unit
135
181
}
136
182
137
- // Filter classes
138
- if len (classes ) > 0 && ! slices .Contains (classes , entity .Class ) {
139
- continue
140
- }
141
-
142
- // Set entity name from attributes
143
- if name , exists := state .Attributes ["friendly_name" ]; exists {
144
- entity .Name = fmt .Sprint (name )
145
- }
183
+ // Filter domains
184
+ //if len(domains) > 0 && !slices.Contains(domains, entity.Domain) {
185
+ // continue
186
+ //}
146
187
147
188
// Append results
148
189
result = append (result , entity )
0 commit comments