Skip to content

Commit

Permalink
Shutdown/Restart button Gracefully stop lnxlink service
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Feb 3, 2023
1 parent 38a07af commit cf0632b
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 116 deletions.
30 changes: 0 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,33 +166,3 @@ Make sure you have these packages on your system:
- xdg-open
- upower
- xset

<details><summary>Technical Notes (click to expand)</summary>

# Creating new senosr
To expand the supported features, create a new python file on **modules** folder and use this template:
```python
class Addon():
name = 'Example'
icon = 'mdi:home-assistant'
unit = ''
def startControl(self, topic, data):
''' When a command is sent, it will run this method '''
print(topic, data)
def getInfo(self):
''' Returns any type that can be converted to JSON '''
return 15
def exposedControls(self):
''' Optional method which exposes an entity '''
return {
"mybutton": {
"type": "button",
"icon": "mdi:button-cursor",
}
}
```

</details>
14 changes: 8 additions & 6 deletions lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, config_path):

self.Addons = {}
for service, addon in modules.parse_modules(self.config['modules']).items():
self.Addons[addon.service] = addon()
self.Addons[addon.service] = addon(self)

self.setup_mqtt()

Expand Down Expand Up @@ -174,11 +174,12 @@ def setup_discovery_monitoring(self, addon, service, discovery_template):
if hasattr(addon, 'state_class'):
discovery['state_class'] = addon.state_class

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'])
sensor_type = getattr(addon, 'sensor_type', None)
if sensor_type is not None:
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(' ', '/')
Expand Down Expand Up @@ -238,6 +239,7 @@ def setup_discovery(self):
if hasattr(addon, 'exposedControls'):
for control_name, options in addon.exposedControls().items():
try:
control_name = control_name.lower().replace(' ', '_')
self.setup_discovery_control(addon, service, control_name, options, discovery_template)
except Exception as e:
traceback.print_exc()
Expand Down
4 changes: 3 additions & 1 deletion lnxlink/modules/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class Addon():
name = 'bash'

def __init__(self, lnxlink):
self.name = 'bash'

def exposedControls(self):
return {
Expand Down
10 changes: 6 additions & 4 deletions lnxlink/modules/bluetooth_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@


class Addon():
name = 'Bluetooth Battery'
icon = 'mdi:battery'
device_class = 'battery'
unit = '%'
def __init__(self, lnxlink):
self.name = 'Bluetooth Battery'
self.sensor_type = 'sensor'
self.icon = 'mdi:battery'
self.device_class = 'battery'
self.unit = '%'

def getInfo(self):
stdout = subprocess.run(
Expand Down
8 changes: 5 additions & 3 deletions lnxlink/modules/camera_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


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

def __init__(self, lnxlink):
self.name = 'Camera used'
self.icon = 'mdi:webcam'
self.sensor_type = 'binary_sensor'

def getInfo(self):
stdout = subprocess.run(
Expand Down
12 changes: 8 additions & 4 deletions lnxlink/modules/cpu.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import psutil


class Addon():
name = 'CPU Usage'
icon = 'mdi:speedometer'
unit = '%'
state_class = 'measurement'

def __init__(self, lnxlink):
self.name = 'CPU Usage'
self.sensor_type = 'sensor'
self.icon = 'mdi:speedometer'
self.unit = '%'
self.state_class = 'measurement'

def getInfo(self):
return psutil.cpu_percent()
9 changes: 6 additions & 3 deletions lnxlink/modules/disk_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@


class Addon():
name = 'Disk Usage'
icon = 'mdi:harddisk'
unit = 'json'

def __init__(self, lnxlink):
self.name = 'Disk Usage'
self.sensor_type = 'sensor'
self.icon = 'mdi:harddisk'
self.unit = 'json'

def getInfo(self) -> dict:
disks = {"status": False}
Expand Down
9 changes: 6 additions & 3 deletions lnxlink/modules/idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@


class Addon():
name = 'Idle'
icon = 'mdi:timer-sand'
unit = 'sec'

def __init__(self, lnxlink):
self.name = 'Idle'
self.sensor_type = 'sensor'
self.icon = 'mdi:timer-sand'
self.unit = 'sec'

def getInfo(self):
stdout = subprocess.run(
Expand Down
9 changes: 4 additions & 5 deletions lnxlink/modules/keep_alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@


class Addon():
name = 'Keep Alive'
icon = 'mdi:mouse-variant'
unit = None
sensor_type = 'switch'
keepalive = 'OFF'

def __init__(self, lnxlink):
self.name = 'Keep Alive'
self.keepalive = 'OFF'

def exposedControls(self):
return {
Expand Down
9 changes: 5 additions & 4 deletions lnxlink/modules/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@


class Addon():
name = 'Media Info'
icon = 'mdi:music'
unit = 'json'

def __init__(self):
def __init__(self, lnxlink):
self.name = 'Media Info'
self.sensor_type = 'sensor'
self.icon = 'mdi:music'
self.unit = 'json'
self.players = []

def exposedControls(self):
Expand Down
12 changes: 8 additions & 4 deletions lnxlink/modules/memory.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import psutil


class Addon():
name = 'Memory Usage'
icon = 'mdi:memory'
unit = '%'
state_class = 'measurement'

def __init__(self, lnxlink):
self.name = 'Memory Usage'
self.sensor_type = 'sensor'
self.icon = 'mdi:memory'
self.unit = '%'
self.state_class = 'measurement'

def getInfo(self):
return psutil.virtual_memory().percent
9 changes: 5 additions & 4 deletions lnxlink/modules/microphone_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


class Addon():
name = 'Microphone used'
icon = 'mdi:microphone'
sensor_type = 'binary_sensor'
unit = ''

def __init__(self, lnxlink):
self.name = 'Microphone used'
self.icon = 'mdi:microphone'
self.sensor_type = 'binary_sensor'

def getInfo(self):
mics = glob.glob('/proc/asound/**/*c/sub*/status', recursive=True)
Expand Down
11 changes: 6 additions & 5 deletions lnxlink/modules/network_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


class Addon():
name = 'Network Download'
icon = 'mdi:access-point-network'
unit = 'Mbit/s'
state_class = 'measurement'

def __init__(self):
def __init__(self, lnxlink):
self.name = 'Network Download'
self.sensor_type = 'sensor'
self.icon = 'mdi:access-point-network'
self.unit = 'Mbit/s'
self.state_class = 'measurement'
self.timeOld = datetime.now()
self.recvOld = psutil.net_io_counters().bytes_recv

Expand Down
11 changes: 6 additions & 5 deletions lnxlink/modules/network_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


class Addon():
name = 'Network Upload'
icon = 'mdi:access-point-network'
unit = 'Mbit/s'
state_class = 'measurement'

def __init__(self):
def __init__(self, lnxlink):
self.name = 'Network Upload'
self.sensor_type = 'sensor'
self.icon = 'mdi:access-point-network'
self.unit = 'Mbit/s'
self.state_class = 'measurement'
self.timeOld = datetime.now()
self.sentOld = psutil.net_io_counters().bytes_sent

Expand Down
8 changes: 4 additions & 4 deletions lnxlink/modules/notify.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import notify2
import requests


class Addon():
name = 'Notify OSD'
icon = None
unit = None

def __init__(self, lnxlink):
self.name = 'Notify OSD'

def startControl(self, topic, data):
iconUrl = data.get('iconUrl', '')
Expand All @@ -27,4 +28,3 @@ def startControl(self, topic, data):
).show()

print(topic, data)

9 changes: 6 additions & 3 deletions lnxlink/modules/nvidia_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@


class Addon():
name = 'Nvidia GPU'
icon = 'mdi:expansion-card-variant'
unit = 'json'

def __init__(self, lnxlink):
self.name = 'Nvidia GPU'
self.sensor_type = 'sensor'
self.icon = 'mdi:expansion-card-variant'
self.unit = 'json'

def getInfo(self) -> dict:
stdout = subprocess.run(
Expand Down
9 changes: 5 additions & 4 deletions lnxlink/modules/required_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


class Addon():
name = 'Required Restart'
icon = 'mdi:alert-octagon-outline'
sensor_type = 'binary_sensor'
unit = ''

def __init__(self, lnxlink):
self.name = 'Required Restart'
self.sensor_type = 'binary_sensor'
self.icon = 'mdi:alert-octagon-outline'

def getInfo(self):
if os.path.exists('/var/run/reboot-required'):
Expand Down
9 changes: 6 additions & 3 deletions lnxlink/modules/restart.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import subprocess


class Addon():
name = 'Restart'
icon = 'mdi:restart'
unit = 'button'

def __init__(self, lnxlink):
self.name = 'Restart'
self.lnxlink = lnxlink

def startControl(self, topic, data):
self.lnxlink.temp_connection_callback(True)
subprocess.call(["shutdown", "-r", "now"])

def exposedControls(self):
Expand Down
7 changes: 3 additions & 4 deletions lnxlink/modules/screen_onoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@


class Addon():
name = 'Screen OnOff'
icon = 'mdi:monitor'
unit = None
sensor_type = 'switch'

def __init__(self, lnxlink):
self.name = 'Screen OnOff'

def exposedControls(self):
return {
Expand Down
4 changes: 3 additions & 1 deletion lnxlink/modules/send_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class Addon():
name = 'Send Keys'

def __init__(self, lnxlink):
self.name = 'Send Keys'

def exposedControls(self):
return {
Expand Down
9 changes: 6 additions & 3 deletions lnxlink/modules/shutdown.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import subprocess


class Addon():
name = 'Shutdown'
icon = 'mdi:power'
unit = 'button'

def __init__(self, lnxlink):
self.name = 'Shutdown'
self.lnxlink = lnxlink

def startControl(self, topic, data):
self.lnxlink.temp_connection_callback(True)
subprocess.call(["shutdown", "now"])

def exposedControls(self):
Expand Down
7 changes: 4 additions & 3 deletions lnxlink/modules/suspend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import subprocess


class Addon():
name = 'Suspend'
icon = 'mdi:progress-clock'
unit = 'button'

def __init__(self, lnxlink):
self.name = 'Suspend'

def startControl(self, topic, data):
subprocess.call(["systemctl", "suspend"])
Expand Down
Loading

0 comments on commit cf0632b

Please sign in to comment.