diff --git a/__init__.py b/__init__.py index 7c6e1d5..31d5968 100644 --- a/__init__.py +++ b/__init__.py @@ -22,7 +22,6 @@ configurationFileName = "homeassistant_config.json" configuration_directory = os.path.join(configLocation()) configuration_file = os.path.join(configuration_directory, configurationFileName) config = {} -#config["sort_order"] = [["light", "switch"], ["scene", "group"], ["automation"]] config["sort_order"] = {"light": 1, "switch": 1, "scene": 2, "group": 2, "automation": 3} icon_files = { @@ -116,9 +115,7 @@ def showEntities(query): subtext="Enter a query to control your Home Assistant", actions=[UrlAction("Open in Browser", config["hass_url"])]) - action_word = query.string.split()[0].lower().strip() - #is_action_word = action_word in action_words - entity_query_list = query.string.split()#[1:] if is_action_word else query.string.split() + entity_query_list = query.string.split() if not entity_query_list: return Item(id=__title__, @@ -144,9 +141,14 @@ def showEntities(query): text="Error while getting entity states from Home Assistant", subtext=str(error)) - # Sort entries + # Sort entries by class entities = sorted(response.json(), key=lambda e: config["sort_order"].get_matching(e["entity_id"].split(".")[0])) + # Sort entries by query matching + entities = sorted(entities, key=lambda e: + RegexDict({query.string.lower() + ".": 0, ".": 1}) + .get_matching(e["attributes"]["friendly_name"].lower() if "friendly_name" in e["attributes"] else e["entity_id"])) + # Parse all entities and states for entity in entities: