diff --git a/README.md b/README.md index 2baa87c..28e229a 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## What is new +* 2024-12-01: Support for Home Assistant API (no MQTT needed) * 2024-11-06: External sensors by @pallago * 2024-10-25: Apt updates sensor * 2024-10-24: Added rpi_power_status sensor @@ -61,15 +62,19 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## CLI arguments ``` -usage: rpi-mqtt-monitor [-h] [--display] [--service] [--version] [--update] +usage: rpi-mqtt-monitor [-h] [-H] [-d] [-s] [-v] [-u] [-w] + +Monitor CPU load, temperature, frequency, free space, etc., and publish the data to an MQTT server or Home Assistant API. 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 - --hass, -H display Home assistant wake on lan configuration + -h, --help show this help message and exit + -H, --hass_api send readings via Home Assistant API (not via MQTT) + -d, --display display values on screen + -s, --service run script as a service, sleep interval is configurable in config.py + -v, --version display installed version and exit + -u, --update update script and config then exit + -w, --hass_wake display Home assistant wake on lan configuration + ``` @@ -88,10 +93,11 @@ Raspberry Pi MQTT monitor will be installed in the location where the installer The auto-installer needs the software below and will install it if its not found: +* git * python (2 or 3) * python-pip -* git -* paho-mqtt +* paho-mqtt (python module) +* requests (python module) Only python is not automatically installed, the rest of the dependencies should be handled by the auto installation. It will also help you configure the host and credentials for the mqtt server in config.py and create the service or cronjob configuration for you. @@ -104,14 +110,14 @@ It is recommended to run the script as a service, this way you can use the resta 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. +Use '''rpi-mqtt-monitor --hass_wake''' to display the configuration for Home Assistant wake on lan switch. [moved to wiki](../../wiki/Home-Assistant-Integration-(outdated)) ## To Do - fix uptime sensor to use timestamp -- integrate hass api +- improve hass api integration ## Feature request diff --git a/install.sh b/install.sh index ec5a8df..91c80e3 100755 --- a/install.sh +++ b/install.sh @@ -1,3 +1,4 @@ +#!/bin/bash find_python(){ if [[ $(python3 --version) ]]; then python=$(which python3) @@ -78,21 +79,7 @@ install_requirements(){ deactivate } -update_config(){ - if [ -f src/config.py ]; then - read -p "src/config.py already exists Do you want to remove it? (y/n) " yn - case $yn in - [Yy]* ) echo "replacing config file";; - [Nn]* ) return;; - * ) echo "Please answer y for yes or n for no.";; - esac - fi - - user=$(whoami) - sed -i "s/os_user_to_be_replaced/${user}/" src/config.py - - print_green "+ Copy config.py.example to config.py" - cp src/config.py.example src/config.py +mqtt_configuration(){ printm "MQTT settings" printf "Enter mqtt_host: " @@ -120,12 +107,65 @@ update_config(){ TOPIC=rpi-MQTT-monitor fi sed -i "s/rpi-MQTT-monitor/${TOPIC}/" src/config.py - + printf "Do you need to control your monitors? (default is No): " read CONTROL if [[ "$CONTROL" =~ ^([yY][eE][sS]|[yY])$ ]]; then sed -i "s/display_control = False/display_control = True/g" src/config.py fi + finish_message="MQTT broker" +} + +hass_api_configuration(){ + printf "Enter Home Assistant API URL (defalut is http://localhost:8123): " + read HA_URL + if [ -z "$HA_URL" ]; then + HA_URL="http://localhost:8123" + fi + sed -i "s|your_hass_host|${HA_URL}|" src/config.py + + printf "Enter Home Assistant API Token: " + read HA_TOKEN + sed -i "s|your_hass_token|${HA_TOKEN}|" src/config.py + hass_api=" --hass_api" + finish_message="Home Assistant API" +} + +update_config(){ + if [ -f src/config.py ]; then + read -p "src/config.py already exists Do you want to remove it? (y/n) " yn + case $yn in + [Yy]* ) echo "replacing config file";; + [Nn]* ) return;; + * ) echo "Please answer y for yes or n for no.";; + esac + fi + + print_green "+ Copy config.py.example to config.py" + cp src/config.py.example src/config.py + + user=$(whoami) + sed -i "s/os_user_to_be_replaced/${user}/" src/config.py + + echo "Do you want to use Home Assistant API or MQTT?" + echo "1) Home Assistant API" + echo "2) MQTT (default)" + read -p "Enter your choice [1 or 2]: " choice + + # Run the appropriate configuration function based on the user's choice + case $choice in + 1) + hass_api_configuration + ;; + 2 | "") + mqtt_configuration + ;; + *) + echo "Invalid choice. Defaulting to MQTT configuration." + mqtt_configuration + ;; + esac + print_green "+ config.py is updated with provided settings" @@ -152,8 +192,8 @@ set_cron(){ MIN=2 fi echo "Adding the line below to your crontab" - echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py" - echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py" >> tempcron + echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py${hass_api}" + echo "*/${MIN} * * * * cd ${cwd}; ${python} ${cwd}/src/rpi-cpu2mqtt.py${hass_api}" >> tempcron crontab tempcron fi rm tempcron @@ -178,7 +218,7 @@ set_service(){ sed -i "s/120/${MIN}/" src/config.py cwd=$(pwd) user=$(whoami) - exec_start="${python} ${cwd}/src/rpi-cpu2mqtt.py --service" + exec_start="${python} ${cwd}/src/rpi-cpu2mqtt.py --service${hass_api}" print_green "+ Copy rpi-mqtt-monitor.service to /etc/systemd/system/" sudo cp ${cwd}/rpi-mqtt-monitor.service /etc/systemd/system/ sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service @@ -221,7 +261,7 @@ main(){ done printm "Installation is complete." - echo "rpi-mqtt-monitor is now running and sending information to your MQTT broker." + echo "rpi-mqtt-monitor is now running and sending information to your ${finish_message}." echo "To see all available options run: rpi-mqtt-monitor -h in the terminal." } diff --git a/remote_install.sh b/remote_install.sh index 8166070..369bf30 100755 --- a/remote_install.sh +++ b/remote_install.sh @@ -1,4 +1,6 @@ #!/bin/bash +# Description: Remote Installation script for rpi-mqtt-monitor + printm(){ length=$(expr length "$1") length=$(($length + 4)) diff --git a/requirements.txt b/requirements.txt index 7249371..b8c6fd0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -paho-mqtt==1.6.1 \ No newline at end of file +paho-mqtt==1.6.1 +requests \ No newline at end of file diff --git a/src/config.py.example b/src/config.py.example index e272085..fdaa15b 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -10,6 +10,10 @@ mqtt_port = "1883" mqtt_discovery_prefix = "homeassistant" mqtt_topic_prefix = "rpi-MQTT-monitor" +# Below hass_ configuration is only needed if you use Home Assistant API instead of +hass_token = "your_hass_token" +hass_host = "your_hass_host" + # Messages configuration # Uncomment the line bellow to send just one CSV message containing all values (this method don't support HA discovery_messages) diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index c1705b0..632bbf2 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -20,6 +20,7 @@ import html import uuid import glob +import requests #import external sensor lib only if one uses external sensors if config.ext_sensors: # append folder ext_sensor_lib @@ -166,8 +167,9 @@ def check_cpu_temp(): def check_sys_clock_speed(): full_cmd = "awk '{printf (\"%0.0f\",$1/1000); }'