include update func. in main script and display version in hass device info

This commit is contained in:
Hristo
2024-01-28 17:44:00 +02:00
committed by Masoko
parent 9c6c54c189
commit 7d4f725e8b
4 changed files with 93 additions and 33 deletions

View File

@@ -40,6 +40,21 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and
* 2024-01-27: Updated the sensors names not to include the device name as per home assistant guidelines * 2024-01-27: Updated the sensors names not to include the device name as per home assistant guidelines
* 2024-01-10: Added support for Raspberry Pi 5 fan speed monitoring (only works on Raspberry Pi 5 with stock fan) * 2024-01-10: Added support for Raspberry Pi 5 fan speed monitoring (only works on Raspberry Pi 5 with stock fan)
## CLI arguments
```
usage: rpi-cpu2mqtt.py [-h] [--display] [--service] [--version] [--update]
options:
-h, --help show this help message and exit
--display, -d display values on screen
--service, -s run script as a service
--version, -v display version
--update, -u update script and config
```
## Installation ## Installation
### Automated ### Automated

View File

@@ -13,6 +13,7 @@ import json
import config import config
import os import os
import argparse import argparse
import update
# get device host name - used in mqtt topic # get device host name - used in mqtt topic
hostname = socket.gethostname() hostname = socket.gethostname()
@@ -142,8 +143,7 @@ def get_manufacturer():
return(pretty_name) return(pretty_name)
def check_git_update(): def check_git_update(script_dir):
script_dir = os.path.dirname(os.path.realpath(__file__))
full_cmd = "git -C {} remote update && git -C {} status -uno".format(script_dir, script_dir) full_cmd = "git -C {} remote update && git -C {} status -uno".format(script_dir, script_dir)
try: try:
git_update = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") git_update = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
@@ -157,16 +157,14 @@ def check_git_update():
return(git_update) return(git_update)
def check_git_version(): def check_git_version(script_dir):
script_dir = os.path.dirname(os.path.realpath(__file__))
full_cmd = "git -C {} describe --tags `git -C {} rev-list --tags --max-count=1`".format(script_dir, script_dir) full_cmd = "git -C {} describe --tags `git -C {} rev-list --tags --max-count=1`".format(script_dir, script_dir)
git_version = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") git_version = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
return(git_version) return(git_version)
def check_git_version_remote(): def check_git_version_remote(script_dir):
script_dir = os.path.dirname(os.path.realpath(__file__))
full_cmd = "git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir) full_cmd = "git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir)
result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
latest_tag = result.strip() latest_tag = result.strip()
@@ -184,7 +182,7 @@ def get_network_ip():
s.close() s.close()
return IP return IP
def print_values(): def print_measured_values():
print(":: rpi-mqtt-monitor") print(":: rpi-mqtt-monitor")
print(" Version: " + config.version) print(" Version: " + config.version)
print("") print("")
@@ -417,20 +415,44 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
client.disconnect() client.disconnect()
def parse_arguments():
if __name__ == '__main__':
# parse arguments
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--display', '-d', action='store_true', help='Display values on screen', default=False) parser.add_argument('--display', '-d', action='store_true', help='display values on screen', default=False)
parser.add_argument('--service', '-s', action='store_true', help='Run script as a service', default=False) parser.add_argument('--service', '-s', action='store_true', help='run script as a service, sleep interval is configurable in config.py', default=False)
parser.add_argument('--version', '-v', action='store_true', help='Display version', default=False) parser.add_argument('--version', '-v', action='store_true', help='display installed version and exit', default=False)
parser.add_argument('--update', '-u', action='store_true', help='update script and config then exit', default=False)
args = parser.parse_args() args = parser.parse_args()
if args.version: if args.update:
print("rpi-mqtt-monitor version: " + check_git_version().strip()) version = check_git_version_remote(script_dir).strip()
print("rpi-mqtt-monitor remote version: " + check_git_version_remote().strip()) git_update = check_git_update(script_dir)
if git_update == 'on':
git_update = True
else:
git_update = False
update.do_update(version, git_update)
exit() exit()
if args.version:
installed_version = check_git_version(script_dir).strip()
latest_versino = check_git_version_remote(script_dir).strip()
print("Installed version: " + installed_version)
print("Latest version: " + latest_versino)
if installed_version != latest_versino:
print("Update available")
else:
print("No update available")
exit()
return args
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
args = parse_arguments();
while True: while True:
# set all monitored values to False in case they are turned off in the config # set all monitored values to False in case they are turned off in the config
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = rpi5_fan_speed = git_update = False
@@ -465,11 +487,11 @@ if __name__ == '__main__':
if config.rpi5_fan_speed: if config.rpi5_fan_speed:
rpi5_fan_speed = check_rpi5_fan_speed() rpi5_fan_speed = check_rpi5_fan_speed()
if config.git_update: if config.git_update:
git_update = check_git_update() git_update = check_git_update(script_dir)
# Display collected values on screen if --display option is used # Display collected values on screen if --display option is used
if args.display: if args.display:
print_values() print_measured_values()
# Publish messages to MQTT # Publish messages to MQTT
if hasattr(config, 'group_messages') and config.group_messages: if hasattr(config, 'group_messages') and config.group_messages:

View File

@@ -1,6 +1,7 @@
import ast import ast
import os import os
import subprocess import subprocess
import config
def get_assignments(filename): def get_assignments(filename):
@@ -10,6 +11,7 @@ def get_assignments(filename):
assignments = {node.targets[0].id: ast.literal_eval(node.value) for node in ast.walk(tree) if isinstance(node, ast.Assign)} assignments = {node.targets[0].id: ast.literal_eval(node.value) for node in ast.walk(tree) if isinstance(node, ast.Assign)}
return assignments return assignments
def update_config(current_config, example_config): def update_config(current_config, example_config):
current_assignments = get_assignments(current_config) current_assignments = get_assignments(current_config)
example_assignments = get_assignments(example_config) example_assignments = get_assignments(example_config)
@@ -21,13 +23,15 @@ def update_config(current_config, example_config):
for var, value in missing_assignments.items(): for var, value in missing_assignments.items():
f.write('\n{} = {!r}'.format(var, value)) f.write('\n{} = {!r}'.format(var, value))
def display_config_differences(current_config, example_config):
def display_config_differences(current_config, example_config, display=True):
current_assignments = get_assignments(current_config) current_assignments = get_assignments(current_config)
example_assignments = get_assignments(example_config) example_assignments = get_assignments(example_config)
missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments} missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments}
if missing_assignments: if missing_assignments:
if display:
print("Missing variables:") print("Missing variables:")
for var, value in missing_assignments.items(): for var, value in missing_assignments.items():
print('\n{} = {!r}'.format(var, value)) print('\n{} = {!r}'.format(var, value))
@@ -35,16 +39,35 @@ def display_config_differences(current_config, example_config):
else: else:
return False return False
def update_config_version(version):
with open('config.py', 'r') as f:
lines = f.readlines()
with open('config.py', 'w') as f:
print(":: Updating config version to {}".format(version))
for line in lines:
if 'version = ' in line:
f.write('version = "{}"\n'.format(version))
else:
f.write(line)
def do_update(version=config.version, git_update=True, config_update=True):
script_dir = os.path.dirname(os.path.realpath(__file__)) script_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(script_dir) os.chdir(script_dir)
print("Current version: {}".format(config.version))
if git_update:
print(":: Updating git repository") print(":: Updating git repository")
result = subprocess.run(['git', '-C', script_dir, 'pull'], check=True, universal_newlines=True, stdout=subprocess.PIPE) result = subprocess.run(['git', '-C', script_dir, 'pull'], check=True, universal_newlines=True, stdout=subprocess.PIPE)
print(result.stdout) print(result.stdout)
if display_config_differences('config.py', 'config.py.example'): if display_config_differences('config.py', 'config.py.example') and config_update:
print(":: Updating config.py") print(":: Updating config.py")
update_config('config.py', 'config.py.example') update_config('config.py', 'config.py.example')
else:
print(":: No config.py updates needed") if version != config.version:
update_config_version(version)
if __name__ == '__main__':
do_update()