Skip to content

Commit 0b9eed6

Browse files
committed
Update code and modules because of module changes
1 parent f57beb9 commit 0b9eed6

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ You need to have at least **python 3.6** installed to use System Sensors.
5151
| mqtt:port | false | 1883 | Port of the MQTT broker |
5252
| mqtt:user | false | \ | The userlogin( if defined) for the MQTT broker |
5353
| mqtt:password | false | \ | the password ( if defined) for the MQTT broker |
54-
| deviceName | true | \ | device name is sent with topic |
54+
| tls | false | \ | Details of TLS settings broker |
55+
| tls:ca_certs | false | \ | TLS settings ( if defined) for the MQTT broker |
56+
| tls:certfile | false | \ | TLS settings ( if defined) for the MQTT broker |
57+
| tls:keyfile | false | \ | TLS settings ( if defined) for the MQTT broker |
5558
| client_id | true | \ | client id to connect to the MQTT broker |
59+
| ha_status | false | hass | Status topic for homeassistant, defaults to _hass_ if not set |
5660
| timezone | true | \ | Your local timezone (you can find the list of timezones here: [time zones](https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568)) |
5761
| power_integer_state(Deprecated) | false | false | Deprecated |
5862
| update_interval | false | 60 | The update interval to send new values to the MQTT broker |

requirements.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
paho-mqtt==1.6.1
2-
psutil==5.9.4
3-
pytz==2022.7.1
4-
PyYAML==6.0
1+
paho-mqtt
2+
psutil
3+
pytz
4+
PyYAML
55
rpi_bad_power==0.1.0
6+
importlib_metadata

src/settings_example.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
mqtt:
22
hostname: 127.0.0.1
3-
port: 1883 #defaults to 1883
3+
port: 1883 # defaults to 1883
44
user: test
55
password: test
6+
tls:
7+
ca_certs:
8+
certfile:
9+
keyfile:
610
deviceName: test
711
client_id: test
12+
ha_status: hass # status topic for homeassistant: defaults to hass if key is omitted
813
timezone: Europe/Brussels
9-
update_interval: 60 #Defaults to 60
14+
update_interval: 60 # Defaults to 60
1015
sensors:
1116
temperature: true
1217
display: true

src/system_sensors.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import argparse
1010
import threading
1111
import paho.mqtt.client as mqtt
12+
import importlib.metadata
1213

1314
from sensors import *
1415

@@ -141,6 +142,12 @@ def check_settings(settings):
141142
settings['sensors']['updates'] = False
142143
if 'power_integer_state' in settings:
143144
write_message_to_console('power_integer_state is deprecated please remove this option power state is now a binary_sensor!')
145+
# these two may be present or not, but in case they are not, create a default
146+
if 'ha_status' not in settings or settings['ha_status'] == '':
147+
settings['ha_status'] = 'hass'
148+
if 'tls' not in settings:
149+
settings['tls'] = {}
150+
settings['tls']['ca_certs'] = ''
144151

145152
def check_zfs(mount_point):
146153
for disk in psutil.disk_partitions():
@@ -184,8 +191,8 @@ def get_host_model():
184191
def on_connect(client, userdata, flags, rc):
185192
if rc == 0:
186193
write_message_to_console('Connected to broker')
187-
print("subscribing : hass/status")
188-
client.subscribe('hass/status')
194+
print("subscribing : " + f"{ha_status}/status")
195+
client.subscribe(f"{ha_status}/status")
189196
print("subscribing : " + f"system-sensors/sensor/{devicename}/availability")
190197
mqttClient.publish(f'system-sensors/sensor/{devicename}/availability', 'online', retain=True)
191198
print("subscribing : " + f"system-sensors/sensor/{devicename}/command")
@@ -239,9 +246,20 @@ def on_message(client, userdata, message):
239246
deviceNameDisplay = settings['devicename']
240247
deviceManufacturer = "RPI Foundation" if "rasp" in OS_DATA["ID"] else OS_DATA['NAME']
241248
deviceModel = get_host_model()
249+
ha_status = settings['ha_status']
242250

243251

244-
mqttClient = mqtt.Client(client_id=settings['client_id'])
252+
# https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html
253+
# note that with version1, mqttv3 is used and no other migration is made
254+
# if paho-mqtt v1.6.x gets removed, a full code migration must be made
255+
if importlib.metadata.version("paho-mqtt")[0] == '1':
256+
# for paho 1.x clients
257+
mqttClient = mqtt.Client(client_id=settings['client_id'])
258+
else:
259+
# for paho 2.x clients
260+
# note that a deprecation warning gets logged
261+
mqttClient = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id=settings['client_id'])
262+
245263
mqttClient.on_connect = on_connect #attach function to callback
246264
mqttClient.on_message = on_message
247265
mqttClient.will_set(f'system-sensors/sensor/{devicename}/availability', 'offline', retain=True)
@@ -250,6 +268,12 @@ def on_message(client, userdata, message):
250268
settings['mqtt']['user'], settings['mqtt']['password']
251269
)
252270

271+
# if ca_certs is populated, we expect the others have been populated accordingly
272+
if settings['tls']['ca_certs'] != '':
273+
mqttClient.tls_set(
274+
ca_certs=settings['tls']['ca_certs'], certfile=settings['tls']['certfile'], keyfile=settings['tls']['keyfile']
275+
)
276+
253277
signal.signal(signal.SIGTERM, signal_handler)
254278
signal.signal(signal.SIGINT, signal_handler)
255279

0 commit comments

Comments
 (0)