diff --git a/lnxlink/__main__.py b/lnxlink/__main__.py index 2f7a57a..fdb2db3 100755 --- a/lnxlink/__main__.py +++ b/lnxlink/__main__.py @@ -152,7 +152,7 @@ def setup_discovery_monitoring(self, addon, service, discovery_template): state_topic = f"{self.pref_topic}/{self.config['mqtt']['statsPrefix']}/{subtopic}" discovery = discovery_template.copy() - discovery['name'] = addon.name.lower().replace(' ', '_') + discovery['name'] = f"{self.config['mqtt']['clientId']} {addon.name.lower().replace(' ', '_')}" discovery['unique_id'] = f"{self.config['mqtt']['clientId']}_{service}" discovery['state_topic'] = state_topic if addon.getInfo.__annotations__.get('return') == dict: @@ -160,11 +160,11 @@ def setup_discovery_monitoring(self, addon, service, discovery_template): if hasattr(addon, 'icon'): discovery['icon'] = addon.icon 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 }}" + else: + discovery['unit_of_measurement'] = addon.unit if hasattr(addon, 'title'): discovery['title'] = addon.title if hasattr(addon, 'entity_picture'): @@ -185,7 +185,7 @@ def setup_discovery_control(self, addon, service, control_name, options, discove 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['name'] = f"{self.config['mqtt']['clientId']} {control_name.lower().replace(' ', '_')}" discovery['unique_id'] = f"{self.config['mqtt']['clientId']}_{control_name}" discovery['enabled_by_default'] = options.get('enabled', True) discovery["command_topic"] = f"{self.pref_topic}/commands/{service}/{control_name}/" diff --git a/lnxlink/consts.py b/lnxlink/consts.py index 43533b8..50ff3d2 100644 --- a/lnxlink/consts.py +++ b/lnxlink/consts.py @@ -49,7 +49,7 @@ update_interval: 5 modules: - bash -- bluetooth_battery +- battery - camera_used - cpu - disk_usage diff --git a/lnxlink/modules/battery.py b/lnxlink/modules/battery.py new file mode 100644 index 0000000..8e9dee5 --- /dev/null +++ b/lnxlink/modules/battery.py @@ -0,0 +1,32 @@ +import subprocess +import jc + + +class Addon(): + def __init__(self, lnxlink): + self.name = 'Battery' + self.sensor_type = 'sensor' + self.icon = 'mdi:battery' + self.device_class = 'battery' + self.unit = 'json' + + def getInfo(self) -> dict: + stdout = subprocess.run( + 'upower --dump', + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE).stdout.decode("UTF-8") + upower_json = jc.parse('upower', stdout) + + devices = {'status': None} + for device in upower_json: + if 'detail' in device: + if 'percentage' in device['detail']: + name = ''.join([ + device.get('vendor', ''), + device.get('model', ''), + ]).strip() + devices[name] = device['detail']['percentage'] + if devices['status'] is None: + devices['status'] = device['detail']['percentage'] + return devices diff --git a/lnxlink/modules/bluetooth_battery.py b/lnxlink/modules/bluetooth_battery.py deleted file mode 100644 index 4d3db31..0000000 --- a/lnxlink/modules/bluetooth_battery.py +++ /dev/null @@ -1,26 +0,0 @@ -import subprocess -import re - - -class Addon(): - 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( - 'upower --dump | grep percentage', - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE).stdout.decode("UTF-8") - results = stdout.strip().split() - - percentage = -1 - if len(results) >= 2: - if re.match(r'\d+%', results[1]): - percentage = int(results[1].replace("%", "")) - - return percentage diff --git a/pyproject.toml b/pyproject.toml index a40a91f..3ae202a 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "lnxlink" -version = "2023.2.0" +version = "2023.3.0" description = "Internet Of Things (IOT) integration with Linux using MQTT" readme = "README.md" keywords = ["lnxlink"] @@ -28,6 +28,7 @@ dependencies = [ "pgi>=0.0.11.2", "pyOpenSSL>=22.1.0", "requests>=2.28.1", + "jc>=1.23.0" ]