diff --git a/README.md b/README.md index 3ce7a87..a516bde 100644 --- a/README.md +++ b/README.md @@ -214,12 +214,16 @@ Create a cron entry like this (you might need to update the path in the cron ent ``` ## How to update -Navigate to the folder where Rapsberry Pi MQTT Monitor is installed and pull the git repository: +Remote updates via Home Assistant are now available. + +To use these you need to have the script running as a service. (the installer now supports this) + +Manual update: ```bash -git pull +cd rpi-mqtt-monitor +python3 src/update.py ``` -* Note that sometimes you might need to add new variables to our src/config.py file, so make sure you check the example file and update your config.py file accordingly. ## Home Assistant Integration diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index ab837e2..72045ad 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -146,18 +146,11 @@ def get_manufacturer(): def check_git_update(script_dir): - full_cmd = "git -C {} remote update && git -C {} status -uno".format(script_dir, script_dir) - try: - git_update = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") - except subprocess.CalledProcessError as e: - print("Error updating git repository:", e.output) - - if any(s in git_update for s in ('Your branch is up to date', 'Your branch is up-to-date', 'Votre branche est à jour')): + if config.version == update.check_git_version_remote(script_dir): git_update = { "installed_ver": config.version, "new_ver": config.version, } - else: git_update = { "installed_ver": config.version, @@ -193,9 +186,9 @@ def print_measured_values( cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_ print(" Version: " + config.version) print("") print(":: Device Information") - print(" Model Name: " + check_model_name().strip()) - print(" Manufacturer: " + get_manufacturer().strip()) - print(" OS: " + get_os().strip()) + print(" Model Name: " + check_model_name()) + print(" Manufacturer: " + get_manufacturer()) + print(" OS: " + get_os()) print(" Hostname: " + hostname) print(" IP Address: " + get_network_ip()) if args.service: @@ -228,10 +221,11 @@ def config_json(what_config): "unit_of_measurement": "", "device": { "identifiers": [hostname], - "manufacturer": manufacturer, - "model": model_name, + "manufacturer": 'github.com/hjelev', + "model": 'RPi MQTT Monitor ' + config.version, "name": hostname, - "sw_version": config.version, + "sw_version": os, + "hw_version": model_name + " by " + manufacturer, "configuration_url": "https://github.com/hjelev/rpi-mqtt-monitor" } } @@ -306,7 +300,7 @@ def config_json(what_config): version = check_git_version(script_dir).strip() data["icon"] = "mdi:update" data["name"] = "RPi MQTT Monitor Update" - data["title"] = "RPi MQTT Monitor v" + version + data["title"] = "Version" data["state_topic"] = config.mqtt_topic_prefix + "/" + hostname + "/" + "git_update" data["value_template"] = "{{ {'installed_version': value_json.installed_ver, 'latest_version': value_json.new_ver } | to_json }}" data["device_class"] = "firmware" @@ -486,7 +480,7 @@ def parse_arguments(): exit() if args.version: - installed_version = check_git_version(script_dir).strip() + installed_version = config.version latest_versino = update.check_git_version_remote(script_dir).strip() print("Installed version: " + installed_version) print("Latest version: " + latest_versino) diff --git a/src/update.py b/src/update.py index 00e4e97..a47e23a 100644 --- a/src/update.py +++ b/src/update.py @@ -18,9 +18,19 @@ def update_config(current_config, example_config): missing_assignments = {var: value for var, value in example_assignments.items() if var not in current_assignments} if missing_assignments: + with open(current_config, 'ab+') as f: # Open the file in binary mode + f.seek(-1, os.SEEK_END) # Move the cursor to the last character + last_char = f.read(1) # Read the last character + + # If the last character is not a newline, write a newline + if last_char != b'\n': + with open(current_config, 'a') as f: # Open the file in text mode + f.write('\n') + + # Write the missing assignments with open(current_config, 'a') as f: for var, value in missing_assignments.items(): - f.write('\n{} = {!r}'.format(var, value)) + f.write('{} = {!r}\n'.format(var, value)) def display_config_differences(current_config, example_config, display=True):