Skip to content

Commit

Permalink
camera status, support number control, add enabled option, replace py…
Browse files Browse the repository at this point in the history
…nput with xdotool key, discovery fix
  • Loading branch information
bkbilly committed Jan 31, 2023
1 parent 01bcac5 commit 38a07af
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 53 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ It's very usefull for remote controling a linux PC, receiving notifications and
# Installation
Install or update:
```shell
sudo apt install patchelf meson libdbus-glib-1-dev libglib2.0-dev libasound2-dev python3-pip
sudo apt install patchelf meson libdbus-glib-1-dev libglib2.0-dev libasound2-dev python3-pip xdotool xprintidle xdg-utils
pip3 install -U lnxlink
# When asked, it's recommended to install as a user service.
lnxlink -c config.yaml
Expand Down Expand Up @@ -66,20 +66,20 @@ data:
"iconUrl": "http://hass.local:8123/local/myimage.jpg" }
```
### Send a series of keys:
### Send a command:
```yaml
service: mqtt.publish
data:
topic: {prefix}/{clientId}/commands/send_keys
payload: "<CTRL>+t"
topic: {prefix}/{clientId}/commands/bash
payload: "ctrl+shift+t"
```
### Send a command:
### Send a series of keys:
```yaml
service: mqtt.publish
data:
topic: {prefix}/{clientId}/commands/bash
payload: "xdotool key ctrl+t"
topic: {prefix}/{clientId}/commands/send_keys
payload: "ctrl+f H e l l o space W o r l d"
```
### Open a URL or a File
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fi
if [ system != 'debian/ubuntu' ]; then
echo -e "\n\n\e[31mSystem dependencies might not be correct...\e[0m"
fi
sudo $installcommand patchelf meson libdbus-glib-1-dev libglib2.0-dev libasound2-dev
sudo $installcommand patchelf meson libdbus-glib-1-dev libglib2.0-dev libasound2-dev xdotool xprintidle xdg-utils


# Install Python requirements
Expand Down
39 changes: 21 additions & 18 deletions lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ def on_message(self, client, userdata, msg):

def setup_discovery_monitoring(self, addon, service, discovery_template):
subtopic = addon.name.lower().replace(' ', '/')
topic = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}"
state_topic = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}"

discovery = discovery_template.copy()
discovery['name'] = addon.name.lower().replace(' ', '_')
discovery['unique_id'] = f"{self.config['mqtt']['clientId']}_{service}"
discovery['state_topic'] = topic
discovery['state_topic'] = state_topic
if addon.getInfo.__annotations__.get('return') == dict:
discovery['json_attributes_topic'] = topic
discovery['json_attributes_topic'] = state_topic
if hasattr(addon, 'icon'):
discovery['icon'] = addon.icon
if hasattr(addon, 'unit'):
Expand All @@ -174,35 +174,38 @@ def setup_discovery_monitoring(self, addon, service, discovery_template):
if hasattr(addon, 'state_class'):
discovery['state_class'] = addon.state_class


if hasattr(addon, 'sensor_type'):
sensor_type = getattr(addon, 'sensor_type', 'sensor')
if sensor_type in ['binary_sensor', 'sensor', 'update']:
self.client.publish(
f"homeassistant/{sensor_type}/lnxlink/{discovery['unique_id']}/config",
payload=json.dumps(discovery),
retain=self.config['mqtt']['lwt']['retain'])
sensor_type = getattr(addon, 'sensor_type', 'sensor')
self.client.publish(
f"homeassistant/{sensor_type}/lnxlink/{discovery['unique_id']}/config",
payload=json.dumps(discovery),
retain=self.config['mqtt']['lwt']['retain'])

def setup_discovery_control(self, addon, service, control_name, options, discovery_template):
subtopic = addon.name.lower().replace(' ', '/')
state_topic = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}"
discovery = discovery_template.copy()
discovery['name'] = control_name.lower().replace(' ', '_')
discovery['unique_id'] = f"{self.config['mqtt']['clientId']}_{control_name}"
discovery['icon'] = options.get('icon', '')
discovery['enabled_by_default'] = options.get('enabled', True)
discovery["command_topic"] = f"{self.pref_topic}/commands/{service}/{control_name}/"
if 'value_template' in options:
discovery["value_template"] = options['value_template']
if 'icon' in options:
discovery['icon'] = options.get('icon', '')


if options['type'] == 'button':
discovery['state_topic'] = f"{self.pref_topic}/lwt"
discovery["command_topic"] = f"{self.pref_topic}/commands/{service}/{control_name}/"
elif options['type'] == 'switch':
discovery["state_topic"] = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}"
discovery["command_topic"] = f"{self.pref_topic}/commands/{service}/{control_name}/"
discovery["state_topic"] = state_topic
discovery["payload_off"] = "OFF"
discovery["payload_on"] = "ON"
elif options['type'] == 'text':
discovery["state_topic"] = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}"
discovery["command_topic"] = f"{self.pref_topic}/commands/{service}/{control_name}/"
discovery["state_topic"] = state_topic
elif options['type'] == 'number':
return
discovery["state_topic"] = state_topic
discovery["min"] = options.get('min', 1)
discovery["max"] = options.get('max', 100)
else:
return
self.client.publish(
Expand Down
19 changes: 19 additions & 0 deletions lnxlink/modules/camera_used.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import subprocess


class Addon():
name = 'Camera used'
icon = 'mdi:webcam'
sensor_type = 'binary_sensor'

def getInfo(self):
stdout = subprocess.run(
"lsmod | grep '^uvcvideo ' | awk '{print $3}'",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.decode("UTF-8")

cam_used = bool(int(stdout.strip()))
if cam_used:
return "ON"
return "OFF"
1 change: 1 addition & 0 deletions lnxlink/modules/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Addon():
name = 'CPU Usage'
icon = 'mdi:speedometer'
unit = '%'
state_class = 'measurement'

def getInfo(self):
return psutil.cpu_percent()
7 changes: 6 additions & 1 deletion lnxlink/modules/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ def exposedControls(self):
"playpause": {
"type": "button",
"icon": "mdi:play-pause",
"enabled": False,
},
"previous": {
"type": "button",
"icon": "mdi:skip-previous",
"enabled": False,
},
"next": {
"type": "button",
"icon": "mdi:skip-next",
"enabled": False,
},
"volume_set": {
"type": "number",
"icon": "mdi:volume",
"icon": "mdi:volume-high",
"min": 0,
"max": 100,
"enabled": False,
"value_template": "{{ value_json.volume }}",
}
}

Expand Down
1 change: 1 addition & 0 deletions lnxlink/modules/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Addon():
name = 'Memory Usage'
icon = 'mdi:memory'
unit = '%'
state_class = 'measurement'

def getInfo(self):
return psutil.virtual_memory().percent
File renamed without changes.
35 changes: 11 additions & 24 deletions lnxlink/modules/send_keys.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
# from pynput.keyboard import Key, Controller
from pynput import keyboard
import subprocess


class Addon():
name = 'Send Keys'
icon = None
unit = None

def startControl(self, topic, data):
if type(data) == str:
self.__presskeys(data)
else:
for keys in data:
self.__presskeys(keys)

def __presskeys(self, keys):
contr = keyboard.Controller()
hotkeys = keyboard.HotKey.parse(keys)
for hotkey in hotkeys:
contr.press(hotkey)
hotkeys.reverse()
for hotkey in hotkeys:
contr.release(hotkey)
def exposedControls(self):
return {
"Send_Keys": {
"type": "text",
"icon": "mdi:keyboard-outline",
}
}


if __name__ == '__main__':
myaddon = Addon()
# myaddon.startControl('send-keys', ['<alt>+<F4>', '<alt>+<ctrl>+<delete>', '<ctrl>+C'])
# myaddon.startControl('send-keys', ['<shift>+C', 'c'])
def startControl(self, topic, data):
subprocess.call(f"xdotool key {data}", shell=True)
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "lnxlink"
version = "2023.1.5"
version = "2023.2.0"
description = "Internet Of Things (IOT) integration with Linux using MQTT"
readme = "README.md"
keywords = ["lnxlink"]
Expand All @@ -23,7 +23,6 @@ dependencies = [
"notify2>=0.3.1",
"psutil>=5.8.0",
"mpris2>=1.0.2",
"pynput>=1.7.3",
"pyalsaaudio>=0.9.2",
"dbus-python>=1.3.2",
"pgi>=0.0.11.2",
Expand Down

0 comments on commit 38a07af

Please sign in to comment.