Skip to content

Commit

Permalink
feat(mqtt): publish discovery message for bootloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ismarslomic committed May 9, 2024
1 parent 318bcec commit 302d592
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 80 deletions.
14 changes: 14 additions & 0 deletions docs/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
"description": "The MQTT base topic under which to publish the Raspberry Pi sensor data topics",
"title": "Base Topic"
},
"discovery_topic_prefix": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": "homeassistant",
"description": "The prefix for Mqtt Discovery topic subscribed by Home Assistant.",
"title": "Discovery Topic Prefix"
},
"sensor_name": {
"anyOf": [
{
Expand Down Expand Up @@ -291,6 +304,7 @@
"authentication": null,
"tls": null,
"base_topic": "home/nodes",
"discovery_topic_prefix": "homeassistant",
"sensor_name": "rpi-{hostname}"
},
"description": "Settings for the MQTT broker connection"
Expand Down
100 changes: 52 additions & 48 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ Model/schema for settings of rpi-mqtt

### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| mqtt | `object` | | [MqttSettings](#mqttsettings) | | `{"hostname": "127.0.0.1", "port": 1883, "client_id": "rpi-mqtt", "authentication": null, "tls": null, "base_topic": "home/nodes", "sensor_name": "rpi-{hostname}"}` | Settings for the MQTT broker connection | |
| script | `object` | | [ScriptSettings](#scriptsettings) | | `{"update_interval": 60, "log_level": "INFO"}` | General settings for this python script | |
| sensors | `object` | | [SensorsMonitoringSettings](#sensorsmonitoringsettings) | | `{"boot_loader": true, "cpu_use": true, "cpu_load": true, "disk": true, "fan": true, "memory": true, "rpi_model": true, "ip_address": true, "hostname": true, "ethernet_mac_address": true, "wifi_mac_address": true, "wifi_connection": true, "os_kernel": true, "os_release": true, "available_updates": true, "boot_time": true, "temperature": true, "throttle": true}` | Settings for monitoring sensors | |

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|----------|----------|----------|---------------------------------------------------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------|----------|
| mqtt | `object` | | [MqttSettings](#mqttsettings) | | `{"hostname": "127.0.0.1", "port": 1883, "client_id": "rpi-mqtt", "authentication": null, "tls": null, "base_topic": "home/nodes", "discovery_topic_prefix": "homeassistant", "sensor_name": "rpi-{hostname}"}` | Settings for the MQTT broker connection | |
| script | `object` | | [ScriptSettings](#scriptsettings) | | `{"update_interval": 60, "log_level": "INFO"}` | General settings for this python script | |
| sensors | `object` | | [SensorsMonitoringSettings](#sensorsmonitoringsettings) | | `{"boot_loader": true, "cpu_use": true, "cpu_load": true, "disk": true, "fan": true, "memory": true, "rpi_model": true, "ip_address": true, "hostname": true, "ethernet_mac_address": true, "wifi_mac_address": true, "wifi_connection": true, "os_kernel": true, "os_release": true, "available_updates": true, "boot_time": true, "temperature": true, "throttle": true}` | Settings for monitoring sensors | |

---

Expand All @@ -33,69 +32,74 @@ Settings for MQTT authentication

#### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| username | `string` || string | | | The MQTT authentication username | |
| password | `string` || string | | | The MQTT authentication password | |
| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|----------|----------|----------|-----------------|------------|---------|----------------------------------|----------|
| username | `string` || string | | | The MQTT authentication username | |
| password | `string` || string | | | The MQTT authentication password | |

## MqttSettings

Settings for the MQTT broker connection

#### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| hostname | `string` | | string | | `"127.0.0.1"` | The hostname or IP address of the MQTT broker to connect to | |
| port | `integer` | | integer | | `1883` | The TCP port the MQTT broker is listening on | |
| client_id | `string` | | string | | `"rpi-mqtt"` | The ID of this python program to use when connecting to the MQTT broker | |
| authentication | `object` | | [MqttAuthentication](#mqttauthentication) | | | The MQTT broker authentication credentials, if required by the broker | |
| tls | `object` | | [MqttTlsSettings](#mqtttlssettings) | | | The TLS for encrypted connection to the MQTT broker, if supporter by broker | |
| base_topic | `string` | | string | | `"home/nodes"` | The MQTT base topic under which to publish the Raspberry Pi sensor data topics | |
| sensor_name | `string` | | string | | `"rpi-{hostname}"` | The MQTT name for this Raspberry Pi as a sensor. Defaults to rpi-<rpi hostname>. | |
| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|------------------------|-----------|----------|-------------------------------------------|------------|--------------------|----------------------------------------------------------------------------------|----------|
| hostname | `string` | | string | | `"127.0.0.1"` | The hostname or IP address of the MQTT broker to connect to | |
| port | `integer` | | integer | | `1883` | The TCP port the MQTT broker is listening on | |
| client_id | `string` | | string | | `"rpi-mqtt"` | The ID of this python program to use when connecting to the MQTT broker | |
| authentication | `object` | | [MqttAuthentication](#mqttauthentication) | | | The MQTT broker authentication credentials, if required by the broker | |
| tls | `object` | | [MqttTlsSettings](#mqtttlssettings) | | | The TLS for encrypted connection to the MQTT broker, if supporter by broker | |
| base_topic | `string` | | string | | `"home/nodes"` | The MQTT base topic under which to publish the Raspberry Pi sensor data topics | |
| discovery_topic_prefix | `string` | | string | | `"homeassistant"` | The prefix for Mqtt Discovery topic subscribed by Home Assistant. | |
| sensor_name | `string` | | string | | `"rpi-{hostname}"` | The MQTT name for this Raspberry Pi as a sensor. Defaults to rpi-<rpi hostname>. | |

## MqttTlsSettings

Settings for MQTT TLS

#### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| ca_certs | `string` || string | | | Path to the CA Certificate file to verify host | |
| certfile | `string` || string | | | Path to the PEM encoded client certificate | |
| keyfile | `string` || string | | | Path to the PEM encoded private key | |
| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|----------|----------|----------|-----------------|------------|---------|------------------------------------------------|----------|
| ca_certs | `string` || string | | | Path to the CA Certificate file to verify host | |
| certfile | `string` || string | | | Path to the PEM encoded client certificate | |
| keyfile | `string` || string | | | Path to the PEM encoded private key | |

## ScriptSettings

General settings for this python script

#### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| update_interval | `integer` | | integer | | `60` | The interval in seconds to update sensor data to the MQTT broker | |
| log_level | `string` | | [LogLevel](#loglevel) | | `"INFO"` | The log level of this python script | |
| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|-----------------|-----------|----------|-----------------------|------------|----------|------------------------------------------------------------------|----------|
| update_interval | `integer` | | integer | | `60` | The interval in seconds to update sensor data to the MQTT broker | |
| log_level | `string` | | [LogLevel](#loglevel) | | `"INFO"` | The log level of this python script | |

## SensorsMonitoringSettings

Settings for monitoring sensors

#### Type: `object`

| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
| -------- | ---- | -------- | --------------- | ---------- | ------- | ----------- | -------- |
| boot_loader | `boolean` | | boolean | | `true` | Enable the bootloader sensor | |
| cpu_use | `boolean` | | boolean | | `true` | Enable the CPU usage sensor | |
| cpu_load | `boolean` | | boolean | | `true` | Enable the CPU load sensor | |
| disk | `boolean` | | boolean | | `true` | Enable the disk usage sensor | |
| fan | `boolean` | | boolean | | `true` | Enable the fan speed sensor | |
| memory | `boolean` | | boolean | | `true` | Enable the memory usage sensor | |
| rpi_model | `boolean` | | boolean | | `true` | Enable the Rpi model sensor | |
| ip_address | `boolean` | | boolean | | `true` | Enable the IP address sensor | |
| hostname | `boolean` | | boolean | | `true` | Enable the hostname sensor | |
| ethernet_mac_address | `boolean` | | boolean | | `true` | Enable the ethernet mac address sensor | |
| wifi_mac_address | `boolean` | | boolean | | `true` | Enable the wifi mac address sensor | |
| wifi_connection | `boolean` | | boolean | | `true` | Enable the wifi connection info sensor | |
| os_kernel | `boolean` | | boolean | | `true` | Enable the os kernel sensor | |
| os_release | `boolean` | | boolean | | `true` | Enable the os release sensor | |
| available_updates | `boolean` | | boolean | | `true` | Enable the available updates sensor | |
| boot_time | `boolean` | | boolean | | `true` | Enable the boot time sensor | |
| temperature | `boolean` | | boolean | | `true` | Enable the temperature sensor | |
| throttle | `boolean` | | boolean | | `true` | Enable the throttling sensor | |
| Property | Type | Required | Possible values | Deprecated | Default | Description | Examples |
|----------------------|-----------|----------|-----------------|------------|---------|----------------------------------------|----------|
| boot_loader | `boolean` | | boolean | | `true` | Enable the bootloader sensor | |
| cpu_use | `boolean` | | boolean | | `true` | Enable the CPU usage sensor | |
| cpu_load | `boolean` | | boolean | | `true` | Enable the CPU load sensor | |
| disk | `boolean` | | boolean | | `true` | Enable the disk usage sensor | |
| fan | `boolean` | | boolean | | `true` | Enable the fan speed sensor | |
| memory | `boolean` | | boolean | | `true` | Enable the memory usage sensor | |
| rpi_model | `boolean` | | boolean | | `true` | Enable the Rpi model sensor | |
| ip_address | `boolean` | | boolean | | `true` | Enable the IP address sensor | |
| hostname | `boolean` | | boolean | | `true` | Enable the hostname sensor | |
| ethernet_mac_address | `boolean` | | boolean | | `true` | Enable the ethernet mac address sensor | |
| wifi_mac_address | `boolean` | | boolean | | `true` | Enable the wifi mac address sensor | |
| wifi_connection | `boolean` | | boolean | | `true` | Enable the wifi connection info sensor | |
| os_kernel | `boolean` | | boolean | | `true` | Enable the os kernel sensor | |
| os_release | `boolean` | | boolean | | `true` | Enable the os release sensor | |
| available_updates | `boolean` | | boolean | | `true` | Enable the available updates sensor | |
| boot_time | `boolean` | | boolean | | `true` | Enable the boot time sensor | |
| temperature | `boolean` | | boolean | | `true` | Enable the temperature sensor | |
| throttle | `boolean` | | boolean | | `true` | Enable the throttling sensor | |
8 changes: 8 additions & 0 deletions src/mqtt/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python3
"""Constants in Mqtt module"""

PAYLOAD_LWT_ONLINE = "online"
PAYLOAD_LWT_OFFLINE = "offline"
TOPIC_SENSOR_STATES_LWT_POSTFIX = "status"
TOPIC_COMMANDS_LWT_POSTFIX = "status"
TOPIC_SENSOR_STATES_POSTFIX = "monitor"
4 changes: 2 additions & 2 deletions src/mqtt/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import paho.mqtt.client as mqtt

from mqtt.mqtt_pub import RpiMqttPublisher
from mqtt.constants import PAYLOAD_LWT_OFFLINE
from mqtt.types import RpiMqttTopics
from settings.types import MqttSettings

Expand Down Expand Up @@ -39,7 +39,7 @@ def connect_and_loop(self):

# Define will message for lwt topics
for lwt_topic in self.mqtt_topics.lwt_topic_names:
self.will_set(lwt_topic, payload=RpiMqttPublisher.PAYLOAD_LWT_OFFLINE, retain=True)
self.will_set(lwt_topic, payload=PAYLOAD_LWT_OFFLINE, retain=True)

# noinspection PyBroadException
# pylint: disable=W0718
Expand Down
Loading

0 comments on commit 302d592

Please sign in to comment.