Sort items by entity class
This commit is contained in:
@ -27,7 +27,8 @@ You can see the actions by pressing the `alt` key.
|
||||
### ToDo
|
||||
|
||||
- renew icons (.svg)
|
||||
- sort Items (on_off, scene/automation, other)
|
||||
- test more device classes and adjust which service to call
|
||||
- sort items: name matching first
|
||||
|
||||
## Contributing
|
||||
|
||||
|
30
__init__.py
30
__init__.py
@ -4,6 +4,7 @@
|
||||
Synopsis: <trigger> <entity filter>"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import requests
|
||||
from albert import Item, ClipAction, FuncAction, UrlAction, configLocation
|
||||
@ -22,6 +23,7 @@ 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 = {
|
||||
"logo": "icons/icon.png",
|
||||
@ -34,8 +36,7 @@ icon_files = {
|
||||
}
|
||||
|
||||
toggle_types = ["light", "switch", "automation", "group", "input_boolean", "climate", "camera"]
|
||||
on_off_types = ["scene", "media_player"] # types where toggle doesn't work
|
||||
#action_words = ["on", "off", "open", "close"]
|
||||
on_off_types = ["scene", "media_player"] # use turn_{on,off} instead of toggle
|
||||
|
||||
|
||||
def initialize():
|
||||
@ -44,7 +45,7 @@ def initialize():
|
||||
# load HASS config
|
||||
if os.path.exists(configuration_file):
|
||||
with open(configuration_file) as json_config:
|
||||
config = json.load(json_config)
|
||||
config.update(json.load(json_config))
|
||||
else:
|
||||
config["hass_url"] = "http://192.168.1.5:8123"
|
||||
config["hass_key"] = ""
|
||||
@ -57,6 +58,10 @@ def initialize():
|
||||
critical("There was an error opening the file: %s" % configuration_file)
|
||||
except OSError:
|
||||
critical("There was an error making the directory: %s" % configuration_directory)
|
||||
|
||||
config["sort_order"]["."] = 999 # Wildcard for sorting
|
||||
config["sort_order"] = RegexDict(config["sort_order"])
|
||||
|
||||
debug("Loaded config: " + str(config))
|
||||
|
||||
|
||||
@ -140,8 +145,7 @@ def showEntities(query):
|
||||
subtext=str(error))
|
||||
|
||||
# Sort entries
|
||||
#entities = sorted(response.json(), key=lambda s: s.lower())
|
||||
entities = response.json()
|
||||
entities = sorted(response.json(), key=lambda e: config["sort_order"].get_matching(e["entity_id"].split(".")[0]))
|
||||
|
||||
# Parse all entities and states
|
||||
for entity in entities:
|
||||
@ -202,12 +206,13 @@ def showEntities(query):
|
||||
if entity_class in toggle_types:
|
||||
item.addAction(FuncAction("Toggle", lambda d=data: sendCommand(d, "homeassistant/toggle")))
|
||||
|
||||
on_or_off = "on" if state != "on" else "off"
|
||||
if entity_class in on_off_types:
|
||||
on_or_off = "on" if state != "on" else "off"
|
||||
item.addAction(FuncAction("Turn %s" % (on_or_off), lambda d=data: sendCommand(d, "homeassistant/turn_%s" % (on_or_off))))
|
||||
|
||||
#if entity_class in open_close_types:
|
||||
#item.addAction(FuncAction("Open Cover", lambda d=data: sendCommand(d, "cover/open_cover")))
|
||||
if entity_class == "cover":
|
||||
open_or_close = "open" if state != "open" else "close"
|
||||
item.addAction(FuncAction("%s Cover" % (open_or_close.capitalize()), lambda d=data: sendCommand(d, "homeassistant/cover/%s_cover") % (open_or_close)))
|
||||
|
||||
item.addAction(ClipAction("Copy ID", entity["entity_id"]))
|
||||
|
||||
@ -218,8 +223,8 @@ def showEntities(query):
|
||||
results.append(
|
||||
Item(id=__title__,
|
||||
icon=os.path.dirname(__file__) + "/" + icon_files["logo"],
|
||||
text="Entity not found",
|
||||
subtext="Please specify another entity."
|
||||
text="Entity not found!",
|
||||
subtext="Please search for another entity."
|
||||
)
|
||||
)
|
||||
|
||||
@ -242,3 +247,8 @@ def sendCommand(data, service):
|
||||
except requests.exceptions.RequestException as error:
|
||||
warning("Error while sending command to Home Assistant:\n%s" % (str(error)))
|
||||
|
||||
# New class to match dict keys with regex
|
||||
class RegexDict(dict):
|
||||
def get_matching(self, event):
|
||||
return next(self[key] for key in self if re.match(key, event))
|
||||
|
||||
|
Reference in New Issue
Block a user