Skip to content

Commit

Permalink
Merge pull request #32 from Garulf/dev
Browse files Browse the repository at this point in the history
Speed up
  • Loading branch information
Garulf committed Feb 18, 2022
2 parents 294f611 + 6b7fb16 commit 7c6d667
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "HA-Commander",
"Description": "Search, and interact with Home Assistant using Wox or Flow Launcher.",
"Author": "Garulf",
"Version": "3.1.0",
"Version": "3.2.0",
"Language": "python",
"Website": "https://github.com/Garulf/HA-Commander",
"IcoPath": "icons\\home-assistant.png",
Expand Down
18 changes: 9 additions & 9 deletions plugin/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

COLORS_FILE = "./plugin/colors.json"
META_FILE = "./meta.json"

with open(META_FILE, "r") as f:
ICONS = json.load(f)
with open(COLORS_FILE, "r") as _f:
COLORS = json.load(_f)

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

def lookup_icon(self, name):
with open(META_FILE, "r") as f:
for _icon in json.load(f):
if name == _icon["name"]:
return chr(int(_icon["codepoint"], 16))
for _icon in ICONS:
if name == _icon["name"]:
return chr(int(_icon["codepoint"], 16))
return None


class Client(Base):
def __init__(self, url, token, verify_ssl=True):
super().__init__(url, token, verify_ssl)
self.api()

def api(self):
return self.request("GET", "")
Expand Down Expand Up @@ -316,9 +315,10 @@ def __init__(self, client: Client, entity: dict) -> None:
getattr(self, source).name = source
getattr(self, source).__doc__ = 'Set Source to "{}"'.format(source)
getattr(self, source).icon = "radiobox-blank"
if source == self.attributes.get("source"):
getattr(self, source).icon = "radiobox-marked"
getattr(self, source).__doc__ = "Currently selected Source."
current_source = self.attributes.get("source")
if current_source:
getattr(self, current_source).icon = "radiobox-marked"
getattr(self, current_source).__doc__ = "Currently selected Source."

@service(icon="arrow-right")
def _select_source(self, source) -> None:
Expand Down
46 changes: 19 additions & 27 deletions plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path


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


class Commander(Flox, Clipboard):

def __init__(self):
super().__init__()
self.font_family = "#Material Design Icons Desktop"


def init_hass(self):
self.logger.debug("Initializing Home Assistant Client")
self.client = Client(
self.settings.get("url"),
self.settings.get("token"),
Expand All @@ -35,6 +42,7 @@ def init_hass(self):
def query(self, query):
try:
self.init_hass()
self.client.api()
except (ReadTimeout, ConnectionError, HTTPError):
self.add_item(
title=f"Could not connect to Home Assistant!",
Expand All @@ -56,11 +64,6 @@ def query(self, query):
parameters=[f"{self.user_keyword} {domain}."],
dont_hide=True,
glyph=self.client.grab_icon(domain),
font_family=str(
Path(self.plugindir).joinpath(
"#Material Design Icons Desktop"
)
),
)
return
# logbook
Expand All @@ -72,11 +75,6 @@ def query(self, query):
method=self.change_query,
parameters=[f'{self.user_keyword} {entry.get("entity_id")}'],
glyph=self.client.grab_icon("history"),
font_family=str(
Path(self.plugindir).joinpath(
"#Material Design Icons Desktop"
)
),
dont_hide=True,
)
return
Expand Down Expand Up @@ -108,11 +106,6 @@ def query(self, query):
method="action",
parameters=[entity._entity, q],
glyph=entity._icon(),
font_family=str(
Path(self.plugindir).joinpath(
"#Material Design Icons Desktop"
)
),
)

if len(self._results) > MAX_ITEMS:
Expand All @@ -121,7 +114,7 @@ def query(self, query):
if len(self._results) == 0:
self.add_item(title="No Results Found!")

def context_menu(self, data):
def create_context(self, data):
self.init_hass()
entity = self.client.create_entity(data[0])
for attr in dir(entity):
Expand All @@ -135,11 +128,6 @@ def context_menu(self, data):
glyph=self.client.grab_icon(
getattr(getattr(entity, attr), "icon", "image_broken")
),
font_family=str(
Path(self.plugindir).joinpath(
"#Material Design Icons Desktop"
)
),
)
if getattr(getattr(entity, attr), "_service", False):
self._results.insert(0, self._results.pop(-1))
Expand All @@ -148,11 +136,6 @@ def context_menu(self, data):
title=str(getattr(entity, attr)),
subtitle=str(attr.replace("_", " ").title()),
glyph=self.client.grab_icon("information"),
font_family=str(
Path(self.plugindir).joinpath(
"#Material Design Icons Desktop"
)
),
method=self.put,
parameters=[str(getattr(entity, attr))],
)
Expand All @@ -163,10 +146,19 @@ def context_menu(self, data):
method=self.hide_entity,
parameters=[entity.entity_id],
)
return self._results


def context_menu(self, data):
entity = data[0]
cache_age = 60
self._results = utils.cache(entity['entity_id'], max_age=cache_age)(self.create_context)(data)


def action(self, entity_id, query="", service="_default_action"):
self.init_hass()
entity = self.client.create_entity(entity_id)
utils.remove_cache(entity.entity_id)
try:
if (
self.client.domain(entity.entity_id, "light")
Expand Down

0 comments on commit 7c6d667

Please sign in to comment.