Skip to content

Commit

Permalink
0.6.0 fix for paho-mqtt v2 and ${HOSTNAME} placeholder for config
Browse files Browse the repository at this point in the history
  • Loading branch information
tofuSCHNITZEL committed Mar 16, 2024
1 parent 87aca4b commit ea01c3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ state of power and brightness every 10 seconds.
`git clone https://github.com/tofuSCHNITZEL/rpi-screenbrightness-mqtt`
2. run installer
`sudo ./rpi-screenbrightness-mqtt/install.sh`
3. edit config and enter your mqtt broker info and optional change the control and state topics
3. edit config and enter your mqtt broker info and optional change the control and state topics you can use "${HOSTNAME}" in clientid, state_topic, command_topic, brightness_state_topic, brightness_command_topic and it will be replaced by the hostname of the device
`sudo nano /etc/rpi_screenbrightness_mqtt.conf`


Expand Down
23 changes: 12 additions & 11 deletions rpi_screenbrightness_mqtt/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License: GNU GPLv3, see LICENSE

import configparser,sys, time
from socket import gethostname
import paho.mqtt.client as mqtt
from rpi_backlight import Backlight

Expand All @@ -18,11 +19,11 @@ def __init__(self, config_path):
self._mqttuser = self._config.get('mqtt', 'user')
self._mqttpassword = self._config.get('mqtt', 'password')
self._mqttconnectedflag = False
self._mqtt_state_topic = self._config.get('mqtt', 'state_topic')
self._mqtt_command_topic = self._config.get('mqtt', 'command_topic')
self._mqtt_brightness_state_topic = self._config.get('mqtt', 'brightness_state_topic')
self._mqtt_brightness_command_topic = self._config.get('mqtt', 'brightness_command_topic')
self._mqtt_clientid = self._config.get('mqtt', 'clientid')
self._mqtt_state_topic = self._config.get('mqtt', 'state_topic').replace('${HOSTNAME}',gethostname())
self._mqtt_command_topic = self._config.get('mqtt', 'command_topic').replace('${HOSTNAME}',gethostname())
self._mqtt_brightness_state_topic = self._config.get('mqtt', 'brightness_state_topic').replace('${HOSTNAME}',gethostname())
self._mqtt_brightness_command_topic = self._config.get('mqtt', 'brightness_command_topic').replace('${HOSTNAME}',gethostname())
self._mqtt_clientid = self._config.get('mqtt', 'clientid').replace('${HOSTNAME}',gethostname())
self._console_output = self._config.getboolean('misc', 'debug')

# initalise backlight object
Expand All @@ -38,15 +39,15 @@ def _print(self, message):
if self._console_output:
print(message)

def on_connect(self, client, userdata, flags, rc):
if rc == 0:
def on_connect(self, client, userdata, flags, reason_code, properties):
if reason_code == 0:
self._print("Connected!")
self._mqttconnectedflag = True
client.subscribe(self._mqtt_brightness_command_topic)
client.subscribe(self._mqtt_command_topic)
else:
self._mqttconnectedflag = False
self._print("Could not connect. Return code: " + str(rc))
self._print("Could not connect. Return code: " + str(reason_code))

def on_message(self, client, userdata, msg):
payload = str(msg.payload.decode("utf-8"))
Expand All @@ -62,8 +63,8 @@ def on_message(self, client, userdata, msg):
self._backlight.brightness = int(payload)
self.sendStatus(client)

def on_disconnect(self, client, userdata, rc):
self._print("disconnected. reason: " + str(rc))
def on_disconnect(self, client, userdata, flags, reason_code, properties):
self._print("disconnected. reason: " + str(reason_code))
self._mqttconnectedflag = False

def sendStatus(self, client):
Expand All @@ -75,7 +76,7 @@ def sendStatus(self, client):
client.publish(self._mqtt_state_topic, payload_power, 0, False)

def run(self):
client = mqtt.Client(self._mqtt_clientid)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, self._mqtt_clientid)
client.on_connect = self.on_connect
client.on_message = self.on_message
client.on_disconnect = self.on_disconnect
Expand Down
26 changes: 13 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from setuptools import setup, find_packages

setup(
name = 'rpi_screenbrightness_mqtt',
version = '0.5.0',
author = 'Tobias Perschon',
author_email = '[email protected]',
description = 'A simple service that conntects to an mqtt broker so you can control the backlight of an rpi (touch)screen via mqtt (and eg. homeassistant)',
license = 'GNU GPLv3',
url = 'https://github.com/tofuSCHNITZEL/rpi-screenbrightness-mqtt',
install_requires = ['rpi-backlight', 'paho-mqtt'],
packages = find_packages()
)
from setuptools import setup, find_packages

setup(
name = 'rpi_screenbrightness_mqtt',
version = '0.6.0',
author = 'Tobias Perschon',
author_email = '[email protected]',
description = 'A simple service that conntects to an mqtt broker so you can control the backlight of an rpi (touch)screen via mqtt (and eg. homeassistant)',
license = 'GNU GPLv3',
url = 'https://github.com/tofuSCHNITZEL/rpi-screenbrightness-mqtt',
install_requires = ['rpi-backlight', 'paho-mqtt'],
packages = find_packages()
)

0 comments on commit ea01c3f

Please sign in to comment.