Merge pull request #3 from leaskovski/master

Added HA MQTT Discovery functionality
This commit is contained in:
Masoko
2021-04-13 09:38:56 +03:00
committed by GitHub
3 changed files with 80 additions and 3 deletions

View File

@@ -51,6 +51,7 @@ This is the default configuration:
``` ```
random_delay = randrange(30) random_delay = randrange(30)
discovery_messages = False
group_messages = True group_messages = True
sleep_time = 0.5 sleep_time = 0.5
cpu_load = True cpu_load = True
@@ -63,6 +64,8 @@ memory = False
uptime = True uptime = True
``` ```
If the ```discovery_messages``` is set to true, the script will send MQTT Discovery config messages which allows Home Assistant to automatically add the sensors without having to define them in configuration. Note, this setting is only available for when ```group_messages``` is set to False.
If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format. If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format.
The group message looks like this: The group message looks like this:
``` ```
@@ -85,7 +88,7 @@ Create a cron entry like this (you might need to update the path in the cron ent
Once you installed the script on your raspberry you need to create some sensors in home assistant. Once you installed the script on your raspberry you need to create some sensors in home assistant.
If you are using ```discovery_messages```, then this step is not required as the sensors are auto discovered by Home Assistant and added.
This is the sensors configuration if ```group_messages = True``` assuming your sensors are separated in ```sensors.yaml``` file. This is the sensors configuration if ```group_messages = True``` assuming your sensors are separated in ```sensors.yaml``` file.
```yaml ```yaml

View File

@@ -12,6 +12,9 @@ mqtt_topic_prefix = "rpi-MQTT-monitor"
# If this is set to True the script will send just one message containing all values # If this is set to True the script will send just one message containing all values
group_messages = True group_messages = True
# If this is set, then the script will send MQTT discovery messages meaning a config less setup in HA. Only works for group_messages being False
discovery_messages = False
# Random delay in seconds before measuring the values # Random delay in seconds before measuring the values
# - this is used for de-synchronizing message if you run this script on many hosts, set this to 0 for no delay. # - this is used for de-synchronizing message if you run this script on many hosts, set this to 0 for no delay.
# - if you want a fix delay you can remove the randarnge function and just set the needed delay. # - if you want a fix delay you can remove the randarnge function and just set the needed delay.

View File

@@ -61,35 +61,106 @@ def check_uptime():
full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime" full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime"
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]) return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
def config_json(what_config):
data = {
"state_topic": "",
"icon": "",
"name": "",
"unique_id": "",
"unit_of_measurement": "",
}
data["state_topic"] = config.mqtt_topic_prefix+"/"+hostname+"/"+what_config
data["unique_id"] = hostname+"_"+what_config
if what_config == "cpuload":
data["icon"] = "mdi:speedometer"
data["name"] = hostname + " CPU Usage"
data["unit_of_measurement"] = "%"
elif what_config == "cputemp":
data["icon"] = "hass:thermometer"
data["name"] = hostname + " CPU Temperature"
data["unit_of_measurement"] = "°C"
elif what_config == "diskusage":
data["icon"] = "mdi:harddisk"
data["name"] = hostname + " Disk Usage"
data["unit_of_measurement"] = "%"
elif what_config == "voltage":
data["icon"] = "mdi:speedometer"
data["name"] = hostname + " CPU Voltage"
data["unit_of_measurement"] = "V"
elif what_config == "swap":
data["icon"] = "mdi:harddisk"
data["name"] = hostname + " Disk Swap"
data["unit_of_measurement"] = "%"
elif what_config == "memory":
data["icon"] = "mdi:memory"
data["name"] = hostname + " Memory Usage"
data["unit_of_measurement"] = "%"
elif what_config == "sys_clock_speed":
data["icon"] = "mdi:speedometer"
data["name"] = hostname + " CPU Clock Speed"
data["unit_of_measurement"] = "MHz"
elif what_config == "uptime_days":
data["icon"] = "mdi:timer-outline"
data["name"] = hostname + " Uptime"
data["unit_of_measurement"] = "days"
else:
return ""
# Return our built discovery config
return json.dumps(data)
def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0): def publish_to_mqtt (cpu_load = 0, cpu_temp = 0, used_space = 0, voltage = 0, sys_clock_speed = 0, swap = 0, memory = 0, uptime_days = 0):
# connect to mqtt server # connect to mqtt server
client = paho.Client() client = paho.Client()
client.username_pw_set(config.mqtt_user, config.mqtt_password) client.username_pw_set(config.mqtt_user, config.mqtt_password)
client.connect(config.mqtt_host, config.mqtt_port) client.connect(config.mqtt_host, int(config.mqtt_port))
# publish monitored values to MQTT # publish monitored values to MQTT
if config.cpu_load: if config.cpu_load:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cpuload/config", config_json('cpuload'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cpuload", cpu_load, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.cpu_temp: if config.cpu_temp:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_cputemp/config", config_json('cputemp'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/cputemp", cpu_temp, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.used_space: if config.used_space:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_diskusage/config", config_json('diskusage'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/diskusage", used_space, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.voltage: if config.voltage:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_voltage/config", config_json('voltage'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/voltage", voltage, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.swap: if config.swap:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_swap/config", config_json('swap'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/swap", swap, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.memory: if config.memory:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_memory/config", config_json('memory'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/memory", memory, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.sys_clock_speed: if config.sys_clock_speed:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_sys_clock_speed/config", config_json('sys_clock_speed'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/sys_clock_speed", sys_clock_speed, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.uptime_days: if config.uptime:
if config.discovery_messages:
client.publish("homeassistant/sensor/"+config.mqtt_topic_prefix+"/"+hostname+"_uptime_days/config", config_json('uptime_days'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1) client.publish(config.mqtt_topic_prefix+"/"+hostname+"/uptime_days", uptime_days, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
# disconect from mqtt server # disconect from mqtt server