4
4
from pathlib import Path
5
5
6
6
7
- from flox import Flox
7
+ from flox import Flox , utils
8
8
from flox .clipboard import Clipboard
9
9
from homeassistant import Client
10
10
from requests .exceptions import ReadTimeout , ConnectionError , HTTPError
@@ -25,7 +25,14 @@ def match(query, entity, friendly_name):
25
25
26
26
27
27
class Commander (Flox , Clipboard ):
28
+
29
+ def __init__ (self ):
30
+ super ().__init__ ()
31
+ self .font_family = "#Material Design Icons Desktop"
32
+
33
+
28
34
def init_hass (self ):
35
+ self .logger .debug ("Initializing Home Assistant Client" )
29
36
self .client = Client (
30
37
self .settings .get ("url" ),
31
38
self .settings .get ("token" ),
@@ -35,6 +42,7 @@ def init_hass(self):
35
42
def query (self , query ):
36
43
try :
37
44
self .init_hass ()
45
+ self .client .api ()
38
46
except (ReadTimeout , ConnectionError , HTTPError ):
39
47
self .add_item (
40
48
title = f"Could not connect to Home Assistant!" ,
@@ -56,11 +64,6 @@ def query(self, query):
56
64
parameters = [f"{ self .user_keyword } { domain } ." ],
57
65
dont_hide = True ,
58
66
glyph = self .client .grab_icon (domain ),
59
- font_family = str (
60
- Path (self .plugindir ).joinpath (
61
- "#Material Design Icons Desktop"
62
- )
63
- ),
64
67
)
65
68
return
66
69
# logbook
@@ -72,11 +75,6 @@ def query(self, query):
72
75
method = self .change_query ,
73
76
parameters = [f'{ self .user_keyword } { entry .get ("entity_id" )} ' ],
74
77
glyph = self .client .grab_icon ("history" ),
75
- font_family = str (
76
- Path (self .plugindir ).joinpath (
77
- "#Material Design Icons Desktop"
78
- )
79
- ),
80
78
dont_hide = True ,
81
79
)
82
80
return
@@ -108,11 +106,6 @@ def query(self, query):
108
106
method = "action" ,
109
107
parameters = [entity ._entity , q ],
110
108
glyph = entity ._icon (),
111
- font_family = str (
112
- Path (self .plugindir ).joinpath (
113
- "#Material Design Icons Desktop"
114
- )
115
- ),
116
109
)
117
110
118
111
if len (self ._results ) > MAX_ITEMS :
@@ -121,7 +114,7 @@ def query(self, query):
121
114
if len (self ._results ) == 0 :
122
115
self .add_item (title = "No Results Found!" )
123
116
124
- def context_menu (self , data ):
117
+ def create_context (self , data ):
125
118
self .init_hass ()
126
119
entity = self .client .create_entity (data [0 ])
127
120
for attr in dir (entity ):
@@ -135,11 +128,6 @@ def context_menu(self, data):
135
128
glyph = self .client .grab_icon (
136
129
getattr (getattr (entity , attr ), "icon" , "image_broken" )
137
130
),
138
- font_family = str (
139
- Path (self .plugindir ).joinpath (
140
- "#Material Design Icons Desktop"
141
- )
142
- ),
143
131
)
144
132
if getattr (getattr (entity , attr ), "_service" , False ):
145
133
self ._results .insert (0 , self ._results .pop (- 1 ))
@@ -148,11 +136,6 @@ def context_menu(self, data):
148
136
title = str (getattr (entity , attr )),
149
137
subtitle = str (attr .replace ("_" , " " ).title ()),
150
138
glyph = self .client .grab_icon ("information" ),
151
- font_family = str (
152
- Path (self .plugindir ).joinpath (
153
- "#Material Design Icons Desktop"
154
- )
155
- ),
156
139
method = self .put ,
157
140
parameters = [str (getattr (entity , attr ))],
158
141
)
@@ -163,10 +146,19 @@ def context_menu(self, data):
163
146
method = self .hide_entity ,
164
147
parameters = [entity .entity_id ],
165
148
)
149
+ return self ._results
150
+
151
+
152
+ def context_menu (self , data ):
153
+ entity = data [0 ]
154
+ cache_age = 60
155
+ self ._results = utils .cache (entity ['entity_id' ], max_age = cache_age )(self .create_context )(data )
156
+
166
157
167
158
def action (self , entity_id , query = "" , service = "_default_action" ):
168
159
self .init_hass ()
169
160
entity = self .client .create_entity (entity_id )
161
+ utils .remove_cache (entity .entity_id )
170
162
try :
171
163
if (
172
164
self .client .domain (entity .entity_id , "light" )
0 commit comments