Skip to content

Commit

Permalink
Update Sensor #25
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Jan 23, 2023
1 parent 17d0129 commit 725a2b7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ It is inspired by [IOT Link](https://iotlink.gitlab.io/).

# Features
- **System control:** Shutdown, Restart, Send Keys, Notify, Media, Screen On/Off, open URL/File, bash.
- **System monitor:** CPU, Ram, Network, Media, Microphone, Idle, Bluetooth battery.
- **Home Assistant:** Uses MQTT Autodiscovery to create entities.
- **System monitor:** CPU, Ram, Network, Media, Microphone, Idle, Bluetooth battery, Disk usage, Required restart, Nvidia GPU.
- **Home Assistant:** Uses MQTT Autodiscovery to create entities and shows if update is required.
- **No sudo required:** No need to be root user to install and use, unless used on server setup.
- **Easily expanded:** Any new module is automatically imported.

Expand Down
18 changes: 11 additions & 7 deletions lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def read_config(self, config_path):
self.pref_topic = f"{config['mqtt']['prefix']}/{config['mqtt']['clientId']}"
self.pref_topic = self.pref_topic.lower()

print(config)
config['modules'] = [x.lower().replace('-', '_') for x in config['modules']]
return config

Expand Down Expand Up @@ -154,19 +153,24 @@ def setup_discovery_monitoring(self, addon, service, discovery_template):
if addon.getInfo.__annotations__.get('return') == dict:
discovery['json_attributes_topic'] = topic
discovery['icon'] = addon.icon
discovery['unit_of_measurement'] = addon.unit
if hasattr(addon, 'unit'):
discovery['unit_of_measurement'] = addon.unit
if addon.unit == 'json':
discovery['unit_of_measurement'] = ""
discovery['value_template'] = "{{ value_json.status }}"
discovery['json_attributes_template'] = "{{ value_json | tojson }}"
if hasattr(addon, 'title'):
discovery['title'] = addon.title
if hasattr(addon, 'entity_picture'):
discovery['entity_picture'] = addon.entity_picture
if hasattr(addon, 'device_class'):
discovery['device_class'] = addon.device_class
if hasattr(addon, 'state_class'):
discovery['state_class'] = addon.state_class

if addon.unit == 'json':
discovery['unit_of_measurement'] = ""
discovery['value_template'] = "{{ value_json.status }}"
discovery['json_attributes_template'] = "{{ value_json | tojson }}"

sensor_type = getattr(addon, 'sensor_type', 'sensor')
if sensor_type in ['binary_sensor', '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),
Expand Down
1 change: 1 addition & 0 deletions lnxlink/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@
- required_restart
- disk_usage
- nvidia_gpu
- update
"""
42 changes: 42 additions & 0 deletions lnxlink/modules/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import importlib.metadata
import time
import requests
import traceback


class Addon():
name = 'Update'
icon = 'mdi:update'
device_class = "firmware"
title = "LNXLink"
entity_picture = "https://github.com/bkbilly/lnxlink/raw/master/logo.png?raw=true"
sensor_type = "update"

def __init__(self):
self.last_time = 0
self.update_interval = 86400 # Check for updates every 24 hours
version = importlib.metadata.version('lnxlink')
self.message = {
"installed_version": version,
"latest_version": version,
"release_summary": "",
"release_url": "https://github.com/bkbilly/lnxlink/releases/latest",
}

def getInfo(self):
cur_time = time.time()
if cur_time - self.last_time > self.update_interval:
self._latest_version()
self.last_time = cur_time

return self.message

def _latest_version(self):
url = "https://api.github.com/repos/bkbilly/lnxlink/releases/latest"
try:
resp = requests.get(url=url).json()
self.message['latest_version'] = resp['tag_name']
self.message['release_summary'] = resp['body']
self.message['release_url'] = resp['html_url']
except:
traceback.print_exc()
2 changes: 1 addition & 1 deletion 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.3"
version = "2023.1.4"
description = "Internet Of Things (IOT) integration with Linux using MQTT"
readme = "README.md"
keywords = ["lnxlink"]
Expand Down

0 comments on commit 725a2b7

Please sign in to comment.