Skip to content

Commit 7c6d667

Browse files
authored
Merge pull request #32 from Garulf/dev
Speed up
2 parents 294f611 + 6b7fb16 commit 7c6d667

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

plugin.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "HA-Commander",
55
"Description": "Search, and interact with Home Assistant using Wox or Flow Launcher.",
66
"Author": "Garulf",
7-
"Version": "3.1.0",
7+
"Version": "3.2.0",
88
"Language": "python",
99
"Website": "https://github.com/Garulf/HA-Commander",
1010
"IcoPath": "icons\\home-assistant.png",

plugin/homeassistant.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
COLORS_FILE = "./plugin/colors.json"
1515
META_FILE = "./meta.json"
16-
16+
with open(META_FILE, "r") as f:
17+
ICONS = json.load(f)
1718
with open(COLORS_FILE, "r") as _f:
1819
COLORS = json.load(_f)
1920

@@ -97,17 +98,15 @@ def grab_icon(self, domain, state="on"):
9798
return None
9899

99100
def lookup_icon(self, name):
100-
with open(META_FILE, "r") as f:
101-
for _icon in json.load(f):
102-
if name == _icon["name"]:
103-
return chr(int(_icon["codepoint"], 16))
101+
for _icon in ICONS:
102+
if name == _icon["name"]:
103+
return chr(int(_icon["codepoint"], 16))
104104
return None
105105

106106

107107
class Client(Base):
108108
def __init__(self, url, token, verify_ssl=True):
109109
super().__init__(url, token, verify_ssl)
110-
self.api()
111110

112111
def api(self):
113112
return self.request("GET", "")
@@ -316,9 +315,10 @@ def __init__(self, client: Client, entity: dict) -> None:
316315
getattr(self, source).name = source
317316
getattr(self, source).__doc__ = 'Set Source to "{}"'.format(source)
318317
getattr(self, source).icon = "radiobox-blank"
319-
if source == self.attributes.get("source"):
320-
getattr(self, source).icon = "radiobox-marked"
321-
getattr(self, source).__doc__ = "Currently selected Source."
318+
current_source = self.attributes.get("source")
319+
if current_source:
320+
getattr(self, current_source).icon = "radiobox-marked"
321+
getattr(self, current_source).__doc__ = "Currently selected Source."
322322

323323
@service(icon="arrow-right")
324324
def _select_source(self, source) -> None:

plugin/main.py

+19-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pathlib import Path
55

66

7-
from flox import Flox
7+
from flox import Flox, utils
88
from flox.clipboard import Clipboard
99
from homeassistant import Client
1010
from requests.exceptions import ReadTimeout, ConnectionError, HTTPError
@@ -25,7 +25,14 @@ def match(query, entity, friendly_name):
2525

2626

2727
class 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

Comments
 (0)