/
/
/
1"""Inbound custom-namespace messages from the Music Assistant Cast receiver app."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING, Any
6
7from pychromecast.controllers import BaseController
8
9from .constants import DASHBOARD_NAMESPACE
10
11if TYPE_CHECKING:
12 from collections.abc import Callable
13
14 from pychromecast.generated.cast_channel_pb2 import CastMessage
15
16PLAYER_COMMANDS = ("next", "previous")
17
18
19class MassCastCommandController(BaseController):
20 """Handles playback commands the Music Assistant Cast receiver app forwards."""
21
22 def __init__(self, on_command: Callable[[str], None]) -> None:
23 """
24 Initialize the controller.
25
26 :param on_command: Called with "next" or "previous" when the device's own
27 UI (Google Home app, touch controls, remote) asks for a queue jump.
28 """
29 super().__init__(DASHBOARD_NAMESPACE)
30 self._on_command = on_command
31
32 def receive_message(self, _message: CastMessage, data: dict[str, Any]) -> bool:
33 """
34 Handle an incoming message on the Music Assistant Cast namespace.
35
36 :param _message: The raw Cast protocol message.
37 :param data: The parsed JSON payload.
38 """
39 if data.get("type") != "player_command":
40 return False
41 command = data.get("command")
42 if command not in PLAYER_COMMANDS:
43 return False
44 self._on_command(command)
45 return True
46