/
/
/
1"""Helpers/utils for the Spotify Connect provider."""
2
3from __future__ import annotations
4
5import hashlib
6import shutil
7
8GO_LIBRESPOT_BINARY = "go-librespot"
9
10
11def get_go_librespot_binary() -> str:
12 """
13 Locate the go-librespot binary on the system PATH.
14
15 In the official Docker image and Home Assistant add-on the binary is installed
16 automatically; manual installs need it on PATH (e.g. ``brew install go-librespot``
17 on macOS, or a release from https://github.com/devgianlu/go-librespot/releases).
18
19 :return: Absolute path to the go-librespot executable.
20 :raises RuntimeError: When the binary cannot be found on PATH.
21 """
22 if binary := shutil.which(GO_LIBRESPOT_BINARY):
23 return binary
24 msg = (
25 "go-librespot binary not found on PATH. Install it (e.g. `brew install go-librespot` "
26 "on macOS, or grab a release from https://github.com/devgianlu/go-librespot/releases) "
27 "and make sure it is reachable on PATH."
28 )
29 raise RuntimeError(msg)
30
31
32def generate_device_id(identity_key: str) -> str:
33 """
34 Derive a stable Spotify device id (40 hex chars) from a daemon's identity key.
35
36 Passing a fixed ``device_id`` to go-librespot keeps the Spotify Connect device
37 identity stable across daemon restarts, so the Spotify app keeps recognising it
38 as the same speaker instead of spawning a fresh device each time.
39
40 :param identity_key: The daemon's unique identity key.
41 """
42 return hashlib.sha256(identity_key.encode()).hexdigest()[:40]
43