diff --git a/README.md b/README.md index 9c6941c..49ff7da 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/rpi_screenbrightness_mqtt/run.py b/rpi_screenbrightness_mqtt/run.py index 737964c..57d2510 100644 --- a/rpi_screenbrightness_mqtt/run.py +++ b/rpi_screenbrightness_mqtt/run.py @@ -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 @@ -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 @@ -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")) @@ -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): @@ -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 diff --git a/setup.py b/setup.py index b829d7d..f7a1a57 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,13 @@ -from setuptools import setup, find_packages - -setup( - name = 'rpi_screenbrightness_mqtt', - version = '0.5.0', - author = 'Tobias Perschon', - author_email = 'tobias@perschon.at', - 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 = 'tobias@perschon.at', + 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() +)