it works \o/
This commit is contained in:
parent
7367fcd3b5
commit
9aaecdf9f1
@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Exit if something fails
|
||||
set -e
|
||||
set -ex
|
||||
|
||||
|
||||
if [[ -z "$XDG_DATA_HOME" ]]; then
|
||||
@ -13,8 +13,10 @@ fi
|
||||
mkdir -p $prefix/krunner/dbusplugins/
|
||||
mkdir -p $prefix/dbus-1/services/
|
||||
|
||||
PY=$(which python3)
|
||||
cp plasma-runner-krha.desktop $prefix/krunner/dbusplugins/
|
||||
sed "s|/home/qbit/projects/krha/krha.py|${PWD}/krha.py|" "org.kde.krha.service" > $prefix/dbus-1/services/org.kde.krha.service
|
||||
sed "s|/usr/bin/python3|${PY}|" "org.kde.krha.service" > $prefix/dbus-1/services/org.kde.krha.service
|
||||
|
||||
kquitapp5 krunner
|
||||
kquitapp6 krunner
|
||||
|
||||
|
56
krha.py
56
krha.py
@ -1,36 +1,76 @@
|
||||
#!/usr/bin/python3
|
||||
#! /usr/bin/env nix-shell
|
||||
#!nix-shell -i python3 -p python3 python3Packages.dbus-python python3Packages.pygobject3 python3Packages.requests
|
||||
|
||||
import dbus.service
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
from gi.repository import GLib
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
DBusGMainLoop(set_as_default=True)
|
||||
|
||||
objpath = "/krha"
|
||||
|
||||
iface = "org.kde.krunner1"
|
||||
os.environ['REQUESTS_CA_BUNDLE'] = '/etc/ssl/certs/ca-certificates.crt'
|
||||
|
||||
iface = "org.kde.krunner1"
|
||||
|
||||
class Runner(dbus.service.Object):
|
||||
def __init__(self):
|
||||
dbus.service.Object.__init__(self, dbus.service.BusName("org.kde.krha", dbus.SessionBus()), objpath)
|
||||
# Load config from ~/.config/krunnerrc or environment variables
|
||||
self.api_key = os.environ.get("HA_API_KEY", "")
|
||||
self.ha_url = os.environ.get("HA_URL", "").rstrip('/')
|
||||
|
||||
@dbus.service.method(iface, in_signature='s', out_signature='a(sssida{sv})')
|
||||
def Match(self, query: str):
|
||||
"""This method is used to get the matches and it returns a list of tupels"""
|
||||
if query == "hello":
|
||||
# data, text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category, multiline(bool) and urls)
|
||||
return [("Hello", "Hello from krha!", "document-edit", 100, 1.0, {'subtext': 'Demo Subtext'})]
|
||||
return []
|
||||
if not query.startswith("ha "):
|
||||
return []
|
||||
|
||||
command = query[3:]
|
||||
if not command:
|
||||
return [("ha", "Home Assistant Commands", "home", 100, 1.0,
|
||||
{'subtext': 'Type a command after "ha" to control Home Assistant'})]
|
||||
|
||||
return [(
|
||||
command, # data
|
||||
f"Send to Home Assistant: {command}", # text
|
||||
"home", # icon
|
||||
100, # type
|
||||
1.0, # relevance
|
||||
{'subtext': 'Press Enter to send command'} # properties
|
||||
)]
|
||||
|
||||
@dbus.service.method(iface, out_signature='a(sss)')
|
||||
def Actions(self):
|
||||
# id, text, icon
|
||||
return [("id", "Tooltip", "planetkde")]
|
||||
return [("send", "Send to Home Assistant", "home")]
|
||||
|
||||
@dbus.service.method(iface, in_signature='ss')
|
||||
def Run(self, data: str, action_id: str):
|
||||
print(data, action_id)
|
||||
if not self.api_key or not self.ha_url:
|
||||
print("Error: HA_API_KEY or HA_URL not configured")
|
||||
return
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self.ha_url}/api/conversation/process",
|
||||
headers=headers,
|
||||
json={"text": data}
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print(f"Response from Home Assistant: {json.dumps(result, indent=2)}")
|
||||
except Exception as e:
|
||||
print(f"Error sending command to Home Assistant: {e}")
|
||||
|
||||
|
||||
|
||||
runner = Runner()
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Exit if something fails
|
||||
set -e
|
||||
set -ex
|
||||
|
||||
if [[ -z "$XDG_DATA_HOME" ]]; then
|
||||
prefix=~/.local/share
|
||||
@ -11,5 +11,5 @@ fi
|
||||
|
||||
rm $prefix/krunner/dbusplugins/plasma-runner-krha.desktop
|
||||
rm $prefix/dbus-1/services/org.kde.krha.service
|
||||
kquitapp5 krunner
|
||||
kquitapp6 krunner
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user