44from pathlib import Path
55
66
7- from flox import Flox
7+ from flox import Flox , utils
88from flox .clipboard import Clipboard
99from homeassistant import Client
1010from requests .exceptions import ReadTimeout , ConnectionError , HTTPError
@@ -25,7 +25,14 @@ def match(query, entity, friendly_name):
2525
2626
2727class Commander (Flox , Clipboard ):
28+
29+ def __init__ (self ):
30+ super ().__init__ ()
31+ self .font_family = "#Material Design Icons Desktop"
32+
33+
2834 def init_hass (self ):
35+ self .logger .debug ("Initializing Home Assistant Client" )
2936 self .client = Client (
3037 self .settings .get ("url" ),
3138 self .settings .get ("token" ),
@@ -35,6 +42,7 @@ def init_hass(self):
3542 def query (self , query ):
3643 try :
3744 self .init_hass ()
45+ self .client .api ()
3846 except (ReadTimeout , ConnectionError , HTTPError ):
3947 self .add_item (
4048 title = f"Could not connect to Home Assistant!" ,
@@ -56,11 +64,6 @@ def query(self, query):
5664 parameters = [f"{ self .user_keyword } { domain } ." ],
5765 dont_hide = True ,
5866 glyph = self .client .grab_icon (domain ),
59- font_family = str (
60- Path (self .plugindir ).joinpath (
61- "#Material Design Icons Desktop"
62- )
63- ),
6467 )
6568 return
6669 # logbook
@@ -72,11 +75,6 @@ def query(self, query):
7275 method = self .change_query ,
7376 parameters = [f'{ self .user_keyword } { entry .get ("entity_id" )} ' ],
7477 glyph = self .client .grab_icon ("history" ),
75- font_family = str (
76- Path (self .plugindir ).joinpath (
77- "#Material Design Icons Desktop"
78- )
79- ),
8078 dont_hide = True ,
8179 )
8280 return
@@ -108,11 +106,6 @@ def query(self, query):
108106 method = "action" ,
109107 parameters = [entity ._entity , q ],
110108 glyph = entity ._icon (),
111- font_family = str (
112- Path (self .plugindir ).joinpath (
113- "#Material Design Icons Desktop"
114- )
115- ),
116109 )
117110
118111 if len (self ._results ) > MAX_ITEMS :
@@ -121,7 +114,7 @@ def query(self, query):
121114 if len (self ._results ) == 0 :
122115 self .add_item (title = "No Results Found!" )
123116
124- def context_menu (self , data ):
117+ def create_context (self , data ):
125118 self .init_hass ()
126119 entity = self .client .create_entity (data [0 ])
127120 for attr in dir (entity ):
@@ -135,11 +128,6 @@ def context_menu(self, data):
135128 glyph = self .client .grab_icon (
136129 getattr (getattr (entity , attr ), "icon" , "image_broken" )
137130 ),
138- font_family = str (
139- Path (self .plugindir ).joinpath (
140- "#Material Design Icons Desktop"
141- )
142- ),
143131 )
144132 if getattr (getattr (entity , attr ), "_service" , False ):
145133 self ._results .insert (0 , self ._results .pop (- 1 ))
@@ -148,11 +136,6 @@ def context_menu(self, data):
148136 title = str (getattr (entity , attr )),
149137 subtitle = str (attr .replace ("_" , " " ).title ()),
150138 glyph = self .client .grab_icon ("information" ),
151- font_family = str (
152- Path (self .plugindir ).joinpath (
153- "#Material Design Icons Desktop"
154- )
155- ),
156139 method = self .put ,
157140 parameters = [str (getattr (entity , attr ))],
158141 )
@@ -163,10 +146,19 @@ def context_menu(self, data):
163146 method = self .hide_entity ,
164147 parameters = [entity .entity_id ],
165148 )
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+
166157
167158 def action (self , entity_id , query = "" , service = "_default_action" ):
168159 self .init_hass ()
169160 entity = self .client .create_entity (entity_id )
161+ utils .remove_cache (entity .entity_id )
170162 try :
171163 if (
172164 self .client .domain (entity .entity_id , "light" )
0 commit comments