Compare commits

...

3 Commits

Author SHA1 Message Date
e023c4a06a Title message improved - preload 2022-08-24 14:30:13 +01:00
3137bbf930 Systemctl service improved 2022-08-24 14:29:46 +01:00
826bd78926 Exception handling for MQTT 2022-08-24 13:59:59 +01:00
2 changed files with 37 additions and 22 deletions

View File

@ -6,7 +6,8 @@ import sys
SERVICE_FILE = """ SERVICE_FILE = """
[Unit] [Unit]
Description=Spotify Service Description=Spotify Service
After=mosquitto.target After=network-online.target
Wants=network-online.target
[Service] [Service]
User=%(UID)d User=%(UID)d
Group=%(GID)d Group=%(GID)d

View File

@ -30,8 +30,10 @@ class librespot(object):
# We pipe the output to an internal pipe # We pipe the output to an internal pipe
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.__state__ = None self.__state__ = None
self.__preload_state__ = False
self.__title__ = None self.__title__ = None
self.__spot_id__ = None self.__spot_id__ = None
self.__spot_id_preload__ = None
self.set_state(False) self.set_state(False)
self.set_title("") self.set_title("")
@ -49,29 +51,36 @@ class librespot(object):
if output: if output:
out_txt = output.decode('utf-8').strip('\n').strip() out_txt = output.decode('utf-8').strip('\n').strip()
out_txt = out_txt[out_txt.find(']') + 2:] out_txt = out_txt[out_txt.find(']') + 2:]
logger.debug("librespot output: %s", out_txt) #logger.debug("librespot output: %s", out_txt)
# TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>" # TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
if out_txt.lower().startswith("loading"): if out_txt.lower().startswith("loading"):
self.__spot_id__ = out_txt.split("<")[2][:-1] logger.debug("librespot: %s", out_txt)
logger.info("Track-ID %s identified", self.__spot_id__) if self.__preload_state__:
if self.__state__: self.__spot_id_preload__ = out_txt.split("<")[2][:-1]
self.set_title(self.get_title_by_id()) logger.info("Upcomming Track-ID %s identified", self.__spot_id__)
else: else:
self.set_title("") self.__spot_id__ = out_txt.split("<")[2][:-1]
logger.info("Current Track-ID %s identified", self.__spot_id__)
if "command=" in out_txt: if "command=" in out_txt:
command = out_txt[out_txt.find('command=') + 8:].strip().lower() command = out_txt[out_txt.find('command=') + 8:].strip().lower()
logger.debug("librespot command: %s", command) logger.debug("librespot command: %s", command)
if command.startswith("preload"):
self.__preload_state__ = True
if command.startswith("load"): if command.startswith("load"):
self.set_state(command.split(',')[2].strip() == 'true') self.set_state(command.split(',')[2].strip() == 'true')
#
self.__preload_state__ = False
self.__spot_id__ = self.__spot_id_preload__
# #
if command in self.ON_CMD: elif command in self.ON_CMD:
self.set_state(True) self.set_state(True)
if command in self.OFF_CMD: elif command in self.OFF_CMD:
self.set_state(False) self.set_state(False)
if self.__state__: if self.__state__:
self.set_title(self.get_title_by_id()) self.set_title(self.get_title_by_id())
else: else:
self.set_title("") self.set_title("")
def get_title_by_id(self): def get_title_by_id(self):
if self.__spot_id__ is None: if self.__spot_id__ is None:
@ -100,19 +109,24 @@ class librespot(object):
def send_state_msg_mqtt(state): def send_state_msg_mqtt(state):
client= paho.Client("spotify") client= paho.Client("spotify")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883) try:
topic = config.MQTT_TOPIC + "/state" client.connect(config.MQTT_SERVER, 1883)
logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state)) topic = config.MQTT_TOPIC + "/state"
client.publish(topic, "true" if state else "false") logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
client.publish(topic, "true" if state else "false")
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending state information")
def send_title_msg_mqtt(title): def send_title_msg_mqtt(title):
client= paho.Client("spotify") client= paho.Client("spotify")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883) try:
topic = config.MQTT_TOPIC + "/title" client.connect(config.MQTT_SERVER, 1883)
logger.info("Sending Spotify status information to mqtt %s = %s", topic, title) topic = config.MQTT_TOPIC + "/title"
client.publish(topic, title) logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
client.publish(topic, title)
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending title information")
if __name__ == '__main__': if __name__ == '__main__':