Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Home Assistant API (no MQTT needed) Experimental feature #160

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

```


Expand All @@ -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.
Expand All @@ -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

Expand Down
80 changes: 60 additions & 20 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
find_python(){
if [[ $(python3 --version) ]]; then
python=$(which python3)
Expand Down Expand Up @@ -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: "
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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."
}

Expand Down
2 changes: 2 additions & 0 deletions remote_install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
# Description: Remote Installation script for rpi-mqtt-monitor

printm(){
length=$(expr length "$1")
length=$(($length + 4))
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
paho-mqtt==1.6.1
paho-mqtt==1.6.1
requests
4 changes: 4 additions & 0 deletions src/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading