| 1 |
"""Updates a Sonos library whenever the beets library is changed. |
| 2 |
This is based on the Kodi Update plugin. |
| 3 |
""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import soco |
| 10 |
|
| 11 |
from beets.plugins import BeetsPlugin |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from beets.library import LibModel, Library |
| 15 |
|
| 16 |
|
| 17 |
class SonosUpdate(BeetsPlugin): |
| 18 |
def __init__(self) -> None: |
| 19 |
super().__init__() |
| 20 |
self.register_listener("database_change", self.listen_for_db_change) |
| 21 |
|
| 22 |
def listen_for_db_change(self, lib: Library, model: LibModel) -> None: |
| 23 |
"""Listens for beets db change and register the update""" |
| 24 |
self.register_listener("cli_exit", self.update) |
| 25 |
|
| 26 |
def update(self, lib: Library) -> None: |
| 27 |
"""When the client exists try to send refresh request to a Sonos |
| 28 |
controller. |
| 29 |
""" |
| 30 |
self._log.info("Requesting a Sonos library update...") |
| 31 |
|
| 32 |
device = soco.discovery.any_soco() |
| 33 |
|
| 34 |
if device: |
| 35 |
device.music_library.start_library_update() |
| 36 |
else: |
| 37 |
self._log.warning("Could not find a Sonos device.") |
| 38 |
return |
| 39 |
|
| 40 |
self._log.info("Sonos update triggered") |