MQTT auth and improvements implemented
This commit is contained in:
parent
42e5047a5f
commit
30e68a738d
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,3 +1,8 @@
|
||||
# ---> mpd
|
||||
#
|
||||
config.py
|
||||
*.err
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "report"]
|
||||
path = report
|
||||
url = https://git.mount-mockery.de/pylib/report.git
|
22
config_example/config.py
Normal file
22
config_example/config.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 -*-
|
||||
import os
|
||||
import report
|
||||
|
||||
MQTT_USER = "user"
|
||||
MQTT_PASS = "pass"
|
||||
MQTT_SERVER = "localhost"
|
||||
MQTT_TOPIC = "hifi/mpd"
|
||||
|
||||
#
|
||||
# Logging
|
||||
#
|
||||
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
|
||||
APP_NAME = "mpd"
|
||||
LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout'
|
||||
LOGLVL = 'DEBUG'
|
||||
|
||||
LOGHOST = 'cutelog'
|
||||
LOGPORT = 19996
|
||||
|
||||
formatter = report.LONG_FMT
|
52
mpd.py
Normal file
52
mpd.py
Normal file
@ -0,0 +1,52 @@
|
||||
import config
|
||||
import logging
|
||||
import paho.mqtt.client as paho
|
||||
import report
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
ROOT_LOGGER_NAME = 'root'
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
|
||||
|
||||
|
||||
class mpc(object):
|
||||
PLAYING_TXT = 'playing'
|
||||
|
||||
def __init__(self, state_callback):
|
||||
logger.info("Starting MPD monitor...")
|
||||
self.__state_callback__ = state_callback
|
||||
self.__state__ = None
|
||||
self.set_state(False)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
output = subprocess.check_output(["mpc", "status"])
|
||||
out_txt = output.decode('UTF-8')
|
||||
self.set_state(self.PLAYING_TXT in out_txt)
|
||||
time.sleep(.2)
|
||||
|
||||
def set_state(self, target_state):
|
||||
if self.__state__ != target_state:
|
||||
self.__state__ = target_state
|
||||
logger.info('MPD state changed to %s', self.__state__)
|
||||
self.__state_callback__(self.__state__)
|
||||
|
||||
|
||||
def send_msg_mqtt(state):
|
||||
client= paho.Client("mpd")
|
||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||
client.connect(config.MQTT_SERVER, 1883)
|
||||
logger.info("Sending MPD status information to mqtt %s = %s", config.MQTT_TOPIC, str(state))
|
||||
client.publish(config.MQTT_TOPIC, "true" if state else "false")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
||||
|
||||
mpd = mpc(send_msg_mqtt)
|
||||
mpd.run()
|
||||
import paho.mqtt.client as paho
|
1
report
Submodule
1
report
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
paho-mqtt
|
Loading…
x
Reference in New Issue
Block a user