Skip to content

Commit 7186a1f

Browse files
authored
Merge pull request #173 from mmattel/updates
Update code and modules because of module changes
2 parents fae97d7 + d887b32 commit 7186a1f

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
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
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +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
66
tls:
7-
# ca_certs
8-
# certfile
9-
# keyfile
7+
ca_certs: # path/file preferrable use directory tls-files
8+
certfile: # path/file preferrable use directory tls-files
9+
keyfile: # path/file preferrable use directory tls-files
1010
deviceName: test
1111
client_id: test
12+
ha_status: hass # status topic for homeassistant: defaults to hass if key is omitted
1213
timezone: Europe/Brussels
13-
update_interval: 60 #Defaults to 60
14+
update_interval: 60 # Defaults to 60
1415
sensors:
1516
temperature: true
1617
display: true

src/system_sensors.py

Lines changed: 23 additions & 4 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

@@ -147,6 +148,12 @@ def check_settings(settings):
147148
settings['sensors']['updates'] = False
148149
if 'power_integer_state' in settings:
149150
write_message_to_console('power_integer_state is deprecated please remove this option power state is now a binary_sensor!')
151+
# these two may be present or not, but in case they are not, create a default
152+
if 'ha_status' not in settings or settings['ha_status'] == '':
153+
settings['ha_status'] = 'hass'
154+
if 'tls' not in settings:
155+
settings['tls'] = {}
156+
settings['tls']['ca_certs'] = ''
150157

151158
def check_zfs(mount_point):
152159
for disk in psutil.disk_partitions():
@@ -190,8 +197,8 @@ def get_host_model():
190197
def on_connect(client, userdata, flags, rc):
191198
if rc == 0:
192199
write_message_to_console('Connected to broker')
193-
print("subscribing : hass/status")
194-
client.subscribe('hass/status')
200+
print("subscribing : " + f"{ha_status}/status")
201+
client.subscribe(f"{ha_status}/status")
195202
print("subscribing : " + f"system-sensors/sensor/{devicename}/availability")
196203
mqttClient.publish(f'system-sensors/sensor/{devicename}/availability', 'online', retain=True)
197204
print("subscribing : " + f"system-sensors/sensor/{devicename}/command")
@@ -245,9 +252,20 @@ def on_message(client, userdata, message):
245252
deviceNameDisplay = settings['devicename']
246253
deviceManufacturer = "RPI Foundation" if "rasp" in OS_DATA["ID"] else OS_DATA['NAME']
247254
deviceModel = get_host_model()
255+
ha_status = settings['ha_status']
248256

249257

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

259-
if 'tls' in settings and 'ca_certs' in settings['tls']:
277+
# if ca_certs is populated, we expect the others have been populated accordingly
278+
if settings['tls']['ca_certs'] != '':
260279
mqttClient.tls_set(
261280
ca_certs=settings['tls']['ca_certs'], certfile=settings['tls']['certfile'], keyfile=settings['tls']['keyfile']
262281
)

tls-files/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+

0 commit comments

Comments
 (0)