Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add functionality to trigger HA events #235

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
3 changes: 3 additions & 0 deletions spotify/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Setup base
ARG LIBRESPOT_VERSION="0.4.2"
ENV PYTHONUNBUFFERED=1

# hadolint ignore=DL3003
RUN \
apk add --no-cache --virtual .build-dependencies \
Expand All @@ -18,6 +20,7 @@ RUN \
\
&& apk add --no-cache \
pulseaudio=16.1-r10 \
python3=3.11.4-r0 \
\
&& cargo install \
--locked \
Expand Down
3 changes: 2 additions & 1 deletion spotify/rootfs/etc/services.d/spotifyd/run
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ if bashio::debug; then
options+=(--verbose)
fi

# Run librespot
# Run librespot runner
exec librespot "${options[@]}"

46 changes: 46 additions & 0 deletions spotify/rootfs/etc/services.d/spotifyd/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import subprocess
import sys
import re
from typing import Dict, Any

EVENT_PREFIX = 'spotify_connect_'
MSG_EVENT_NAME = 'message'
LINE_REGEX = r'\[(?P<date_time>[0-9-]{10}T[0-9:]{8}Z) (?P<severity>[A-Z]+). (?P<sender>[ -~]+)] (?P<message>[ -~]+)'
MSG_REGEXES = {
'loading_song_regex': r'Loading <(?P<song_name>[ -~]+)> with Spotify URI <spotify:track:(?P<track_id>[A-z0-9]+)>',
'loaded_song_regex': r'<(?P<song_name>[ -~]+)> \((?P<song_duration>[0-9]+) ms\) loaded'
}


def trigger_event(event_name: str, parameters: Dict[str, Any]):
print(f"EVENT: {event_name}, PARAM: {parameters}")
# TODO: Trigger HA Event
pass


def main():
with subprocess.Popen(['librespot', *sys.argv[1:]],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) as p:
for line in p.stdout:
line = line.decode()
try:
parsed_line = re.match(LINE_REGEX, line)
event_params = parsed_line.groupdict()
msg = event_params.get('message')

trigger_event(MSG_EVENT_NAME, event_params)

for event, regex in MSG_REGEXES.items():
if re.match(regex, msg):
trigger_event(event, event_params)
continue

except Exception as e:
sys.stdout.write(f"W: Failed to parse line: {e}")

sys.stdout.write(line)


if __name__ == '__main__':
main()