From 21fa1b8ae4d6e46f02d5f98958f4f0459696df06 Mon Sep 17 00:00:00 2001 From: Masoko Date: Sun, 14 Apr 2024 17:56:01 +0300 Subject: [PATCH] display home assistant wake on lan switch when argument --hass is used (#97) --- README.md | 5 +++++ src/config.py.example | 2 +- src/rpi-cpu2mqtt.py | 25 ++++++++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7c3c6e0..9a8b559 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## What is new +* 2024-03-24: --hass to display configuration for Home Assistant wake on lan switch +* 2024-02-20: Shutdown button added (only works when running as service) * 2024-02-05: System Restart button added (only works when running as service) * 2024-01-28: Remote updates via Home Assistant are now available * 2024-01-28: Improved error handling for the MQTT connection @@ -61,6 +63,7 @@ options: --service, -s run script as a service --version, -v display version --update, -u update script and config + --hass, -H display Home assistant wake on lan configuration ``` @@ -232,6 +235,8 @@ python3 src/update.py If you are using discovery_messages, then this step is not required as a new MQTT device will be automatically created in Home Assistant and all you need to do is add it to a dashboard. +Use '''python3 src/rpi-cpu2mqtt.py --hass''' to display the configuration for Home Assistant wake on lan switch. + [moved to wiki](../../wiki/Home-Assistant-Integration-(outdated)) ## To Do diff --git a/src/config.py.example b/src/config.py.example index cd8408e..d9b3ab9 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -21,7 +21,7 @@ discovery_messages = True # Enable remote restart button in Home Assistant restart_button = True -# Enable remote shutdiwb button in Home Assistant +# Enable remote shutdown button in Home Assistant shutdown_button = True # Binary sensor that displays when there are updates diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 4f061a2..a56817b 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -20,8 +20,6 @@ import re import html import uuid -# get device host name - used in mqtt topic -hostname = socket.gethostname() def check_wifi_signal(format): try: @@ -546,7 +544,8 @@ def parse_arguments(): 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, sleep interval is configurable in config.py', 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) + parser.add_argument('--update', '-u', action='store_true', help='update script and config then exit', default=False) + parser.add_argument('--hass', '-H', action='store_true', help='display Home assistant wake on lan configuration', default=False) args = parser.parse_args() if args.update: @@ -573,6 +572,22 @@ def parse_arguments(): print("No update available") exit() + if args.hass: + hass_config = """Add this to your Home Assistant switches.yaml file: + + - platform: wake_on_lan + mac: "{}" + host: "{}" + name: "{}-switch" + turn_off: + service: mqtt.publish + data: + topic: "homeassistant/update/{}/command" + payload: "shutdown" + """.format(get_mac_address(), get_network_ip(), hostname, hostname ) + print(hass_config) + exit() + return args @@ -640,8 +655,6 @@ def update_status(): break - - def on_message(client, userdata, msg): global exit_flag, thread1, thread2 print("Received message: ", msg.payload.decode()) @@ -673,6 +686,8 @@ def on_message(client, userdata, msg): exit_flag = False stop_event = threading.Event() script_dir = os.path.dirname(os.path.realpath(__file__)) +# get device host name - used in mqtt topic +hostname = socket.gethostname() if __name__ == '__main__': args = parse_arguments();