MQTT auth and improvements implemented
This commit is contained in:
parent
93cbfc5dd1
commit
8a4e7f4f37
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,3 +1,8 @@
|
||||
# ---> spotify
|
||||
#
|
||||
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
|
24
config_example/config.py
Normal file
24
config_example/config.py
Normal file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 -*-
|
||||
import os
|
||||
import report
|
||||
|
||||
MQTT_USER = "user"
|
||||
MQTT_PASS = "pass"
|
||||
MQTT_SERVER = "localhost"
|
||||
MQTT_TOPIC = "hifi/spotify"
|
||||
|
||||
DEVICE_NAME = "Multimedia"
|
||||
|
||||
#
|
||||
# Logging
|
||||
#
|
||||
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
|
||||
APP_NAME = "spotify"
|
||||
LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout'
|
||||
LOGLVL = 'DEBUG'
|
||||
|
||||
LOGHOST = 'cutelog'
|
||||
LOGPORT = 19996
|
||||
|
||||
formatter = report.LONG_FMT
|
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
|
76
spotify.py
Normal file
76
spotify.py
Normal file
@ -0,0 +1,76 @@
|
||||
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('main')
|
||||
|
||||
|
||||
class librespot(object):
|
||||
ON_CMD = ['play', ]
|
||||
OFF_CMD = ['pause', 'stop', ]
|
||||
|
||||
def __init__(self, state_callback):
|
||||
logger.info("Starting Librespot...")
|
||||
self.__state_callback__ = state_callback
|
||||
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
|
||||
shell=False,
|
||||
# We pipe the output to an internal pipe
|
||||
stderr=subprocess.PIPE)
|
||||
self.__state__ = None
|
||||
self.set_state(False)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
output = self.__process__.stderr.readline()
|
||||
# Polling returns None when the program is still running, return_code otherwise
|
||||
return_code = self.__process__.poll()
|
||||
if return_code is not None:
|
||||
self.__process__.close()
|
||||
# Program ended, get exit/return code
|
||||
raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code))
|
||||
|
||||
# If the output is not empty, feed it to the function, strip the newline first
|
||||
if output:
|
||||
out_txt = output.decode('utf-8').strip('\n').strip()
|
||||
out_txt = out_txt[out_txt.find(']') + 2:]
|
||||
logger.debug("librespot output: %s", out_txt)
|
||||
if "command=" in out_txt:
|
||||
command = out_txt[out_txt.find('command=') + 8:].strip().lower()
|
||||
logger.debug("librespot command: %s", command)
|
||||
if command.startswith("load"):
|
||||
self.set_state(command.split(',')[2].strip() == 'true')
|
||||
#
|
||||
if command in self.ON_CMD:
|
||||
self.set_state(True)
|
||||
if command in self.OFF_CMD:
|
||||
self.set_state(False)
|
||||
|
||||
def set_state(self, target_state):
|
||||
if target_state != self.__state__:
|
||||
self.__state__ = target_state
|
||||
logger.info("spotify state changed to %s", self.__state__)
|
||||
self.__state_callback__(self.__state__)
|
||||
|
||||
|
||||
def send_msg_mqtt(state):
|
||||
client= paho.Client("spotify")
|
||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||
client.connect(config.MQTT_SERVER, 1883)
|
||||
logger.info("Sending Spotify 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)
|
||||
|
||||
ls = librespot(send_msg_mqtt)
|
||||
ls.run()
|
Loading…
x
Reference in New Issue
Block a user