Skip to content

Commit

Permalink
Merge pull request #173 from mmattel/updates
Browse files Browse the repository at this point in the history
Update code and modules because of module changes
  • Loading branch information
Sennevds authored Mar 23, 2024
2 parents fae97d7 + d887b32 commit 7186a1f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ You need to have at least **python 3.6** installed to use System Sensors.
| mqtt:port | false | 1883 | Port of the MQTT broker |
| mqtt:user | false | \ | The userlogin( if defined) for the MQTT broker |
| mqtt:password | false | \ | the password ( if defined) for the MQTT broker |
| deviceName | true | \ | device name is sent with topic |
| tls | false | \ | Details of TLS settings broker |
| tls:ca_certs | false | \ | TLS settings ( if defined) for the MQTT broker |
| tls:certfile | false | \ | TLS settings ( if defined) for the MQTT broker |
| tls:keyfile | false | \ | TLS settings ( if defined) for the MQTT broker |
| client_id | true | \ | client id to connect to the MQTT broker |
| ha_status | false | hass | Status topic for homeassistant, defaults to _hass_ if not set |
| timezone | true | \ | Your local timezone (you can find the list of timezones here: [time zones](https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568)) |
| power_integer_state(Deprecated) | false | false | Deprecated |
| update_interval | false | 60 | The update interval to send new values to the MQTT broker |
Expand Down
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
paho-mqtt==1.6.1
psutil==5.9.4
pytz==2022.7.1
PyYAML==6.0.1
paho-mqtt
psutil
pytz
PyYAML
rpi_bad_power==0.1.0
importlib_metadata
11 changes: 6 additions & 5 deletions src/settings_example.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
mqtt:
hostname: 127.0.0.1
port: 1883 #defaults to 1883
port: 1883 # defaults to 1883
user: test
password: test
tls:
# ca_certs
# certfile
# keyfile
ca_certs: # path/file preferrable use directory tls-files
certfile: # path/file preferrable use directory tls-files
keyfile: # path/file preferrable use directory tls-files
deviceName: test
client_id: test
ha_status: hass # status topic for homeassistant: defaults to hass if key is omitted
timezone: Europe/Brussels
update_interval: 60 #Defaults to 60
update_interval: 60 # Defaults to 60
sensors:
temperature: true
display: true
Expand Down
27 changes: 23 additions & 4 deletions src/system_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import argparse
import threading
import paho.mqtt.client as mqtt
import importlib.metadata

from sensors import *

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

def check_zfs(mount_point):
for disk in psutil.disk_partitions():
Expand Down Expand Up @@ -190,8 +197,8 @@ def get_host_model():
def on_connect(client, userdata, flags, rc):
if rc == 0:
write_message_to_console('Connected to broker')
print("subscribing : hass/status")
client.subscribe('hass/status')
print("subscribing : " + f"{ha_status}/status")
client.subscribe(f"{ha_status}/status")
print("subscribing : " + f"system-sensors/sensor/{devicename}/availability")
mqttClient.publish(f'system-sensors/sensor/{devicename}/availability', 'online', retain=True)
print("subscribing : " + f"system-sensors/sensor/{devicename}/command")
Expand Down Expand Up @@ -245,9 +252,20 @@ def on_message(client, userdata, message):
deviceNameDisplay = settings['devicename']
deviceManufacturer = "RPI Foundation" if "rasp" in OS_DATA["ID"] else OS_DATA['NAME']
deviceModel = get_host_model()
ha_status = settings['ha_status']


mqttClient = mqtt.Client(client_id=settings['client_id'])
# https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html
# note that with version1, mqttv3 is used and no other migration is made
# if paho-mqtt v1.6.x gets removed, a full code migration must be made
if importlib.metadata.version("paho-mqtt")[0] == '1':
# for paho 1.x clients
mqttClient = mqtt.Client(client_id=settings['client_id'])
else:
# for paho 2.x clients
# note that a deprecation warning gets logged
mqttClient = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id=settings['client_id'])

mqttClient.on_connect = on_connect #attach function to callback
mqttClient.on_message = on_message
mqttClient.will_set(f'system-sensors/sensor/{devicename}/availability', 'offline', retain=True)
Expand All @@ -256,7 +274,8 @@ def on_message(client, userdata, message):
settings['mqtt']['user'], settings['mqtt']['password']
)

if 'tls' in settings and 'ca_certs' in settings['tls']:
# if ca_certs is populated, we expect the others have been populated accordingly
if settings['tls']['ca_certs'] != '':
mqttClient.tls_set(
ca_certs=settings['tls']['ca_certs'], certfile=settings['tls']['certfile'], keyfile=settings['tls']['keyfile']
)
Expand Down
3 changes: 3 additions & 0 deletions tls-files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore

0 comments on commit 7186a1f

Please sign in to comment.