|
| 1 | +"""Service actions for mass_queue.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from homeassistant.config_entries import ConfigEntryState |
| 8 | +from homeassistant.core import ( |
| 9 | + HomeAssistant, |
| 10 | + ServiceCall, |
| 11 | + SupportsResponse, |
| 12 | + callback, |
| 13 | +) |
| 14 | +from homeassistant.exceptions import ServiceValidationError |
| 15 | +from homeassistant.helpers import entity_registry as er |
| 16 | + |
| 17 | +from .const import ( |
| 18 | + ATTR_CONFIG_ENTRY_ID, |
| 19 | + ATTR_PLAYER_ENTITY, |
| 20 | + DOMAIN, |
| 21 | + SERVICE_GET_QUEUE_ITEMS, |
| 22 | + SERVICE_MOVE_QUEUE_ITEM_DOWN, |
| 23 | + SERVICE_MOVE_QUEUE_ITEM_NEXT, |
| 24 | + SERVICE_MOVE_QUEUE_ITEM_UP, |
| 25 | + SERVICE_PLAY_QUEUE_ITEM, |
| 26 | + SERVICE_REMOVE_QUEUE_ITEM, |
| 27 | + SERVICE_SEND_COMMAND, |
| 28 | +) |
| 29 | +from .schemas import ( |
| 30 | + MOVE_QUEUE_ITEM_DOWN_SERVICE_SCHEMA, |
| 31 | + MOVE_QUEUE_ITEM_NEXT_SERVICE_SCHEMA, |
| 32 | + MOVE_QUEUE_ITEM_UP_SERVICE_SCHEMA, |
| 33 | + PLAY_QUEUE_ITEM_SERVICE_SCHEMA, |
| 34 | + QUEUE_ITEMS_SERVICE_SCHEMA, |
| 35 | + REMOVE_QUEUE_ITEM_SERVICE_SCHEMA, |
| 36 | + SEND_COMMAND_SERVICE_SCHEMA, |
| 37 | +) |
| 38 | + |
| 39 | +if TYPE_CHECKING: |
| 40 | + from music_assistant_client import MusicAssistantClient |
| 41 | + |
| 42 | + from . import MassQueueEntryData |
| 43 | + |
| 44 | + |
| 45 | +@callback |
| 46 | +def register_actions(hass) -> None: |
| 47 | + """Registers actions with Home Assistant.""" |
| 48 | + hass.services.async_register( |
| 49 | + DOMAIN, |
| 50 | + SERVICE_GET_QUEUE_ITEMS, |
| 51 | + get_queue_items, |
| 52 | + schema=QUEUE_ITEMS_SERVICE_SCHEMA, |
| 53 | + supports_response=SupportsResponse.ONLY, |
| 54 | + ) |
| 55 | + hass.services.async_register( |
| 56 | + DOMAIN, |
| 57 | + SERVICE_MOVE_QUEUE_ITEM_DOWN, |
| 58 | + move_queue_item_down, |
| 59 | + schema=MOVE_QUEUE_ITEM_DOWN_SERVICE_SCHEMA, |
| 60 | + supports_response=SupportsResponse.NONE, |
| 61 | + ) |
| 62 | + hass.services.async_register( |
| 63 | + DOMAIN, |
| 64 | + SERVICE_MOVE_QUEUE_ITEM_NEXT, |
| 65 | + move_queue_item_next, |
| 66 | + schema=MOVE_QUEUE_ITEM_NEXT_SERVICE_SCHEMA, |
| 67 | + supports_response=SupportsResponse.NONE, |
| 68 | + ) |
| 69 | + hass.services.async_register( |
| 70 | + DOMAIN, |
| 71 | + SERVICE_MOVE_QUEUE_ITEM_UP, |
| 72 | + move_queue_item_up, |
| 73 | + schema=MOVE_QUEUE_ITEM_UP_SERVICE_SCHEMA, |
| 74 | + supports_response=SupportsResponse.NONE, |
| 75 | + ) |
| 76 | + hass.services.async_register( |
| 77 | + DOMAIN, |
| 78 | + SERVICE_PLAY_QUEUE_ITEM, |
| 79 | + play_queue_item, |
| 80 | + schema=PLAY_QUEUE_ITEM_SERVICE_SCHEMA, |
| 81 | + supports_response=SupportsResponse.NONE, |
| 82 | + ) |
| 83 | + hass.services.async_register( |
| 84 | + DOMAIN, |
| 85 | + SERVICE_REMOVE_QUEUE_ITEM, |
| 86 | + remove_queue_item, |
| 87 | + schema=REMOVE_QUEUE_ITEM_SERVICE_SCHEMA, |
| 88 | + supports_response=SupportsResponse.NONE, |
| 89 | + ) |
| 90 | + hass.services.async_register( |
| 91 | + DOMAIN, |
| 92 | + SERVICE_SEND_COMMAND, |
| 93 | + send_command, |
| 94 | + schema=SEND_COMMAND_SERVICE_SCHEMA, |
| 95 | + supports_response=SupportsResponse.OPTIONAL, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def _get_mass_entity_config_entry_id(hass, entity_id): |
| 100 | + """Helper to grab config entry ID from entity ID.""" |
| 101 | + registry = er.async_get(hass) |
| 102 | + return registry.async_get(entity_id).config_entry_id |
| 103 | + |
| 104 | + |
| 105 | +@callback |
| 106 | +def _get_config_entry( |
| 107 | + hass: HomeAssistant, |
| 108 | + config_entry_id: str, |
| 109 | +) -> MusicAssistantClient: |
| 110 | + """Get Music Assistant Client from config_entry_id.""" |
| 111 | + entry: MassQueueEntryData | None |
| 112 | + if not (entry := hass.config_entries.async_get_entry(config_entry_id)): |
| 113 | + exc = "Entry not found." |
| 114 | + raise ServiceValidationError(exc) |
| 115 | + if entry.state is not ConfigEntryState.LOADED: |
| 116 | + exc = "Entry not loaded" |
| 117 | + raise ServiceValidationError(exc) |
| 118 | + return entry |
| 119 | + |
| 120 | + |
| 121 | +def get_mass_entry(hass, entity_id): |
| 122 | + """Helper function to pull MA Config Entry.""" |
| 123 | + config_id = _get_mass_entity_config_entry_id(hass, entity_id) |
| 124 | + return _get_config_entry(hass, config_id) |
| 125 | + |
| 126 | + |
| 127 | +def _get_mass_queue_entries(hass): |
| 128 | + """Gets all entries for mass_queue domain.""" |
| 129 | + entries = hass.config_entries.async_entries() |
| 130 | + return [entry for entry in entries if entry.domain == "mass_queue"] |
| 131 | + |
| 132 | + |
| 133 | +def find_mass_queue_entry(hass, mass_url): |
| 134 | + """Finds the mass_queue entry for the given MA URL.""" |
| 135 | + entries = _get_mass_queue_entries(hass) |
| 136 | + for entry in entries: |
| 137 | + entry_url = entry.runtime_data.mass.connection.ws_server_url |
| 138 | + if entry_url == mass_url: |
| 139 | + return entry |
| 140 | + msg = f"Cannot find entry for Music Assistant at {mass_url}" |
| 141 | + raise ServiceValidationError(msg) |
| 142 | + |
| 143 | + |
| 144 | +def get_entity_actions_controller(hass, entity_id): |
| 145 | + """Gets the actions for the selected entity.""" |
| 146 | + mass_entry = get_mass_entry(hass, entity_id) |
| 147 | + mass = mass_entry.runtime_data.mass.connection.ws_server_url |
| 148 | + mass_queue_entry = find_mass_queue_entry(hass, mass) |
| 149 | + return mass_queue_entry.runtime_data.actions |
| 150 | + |
| 151 | + |
| 152 | +async def get_queue_items(call: ServiceCall): |
| 153 | + """Service wrapper to get queue items.""" |
| 154 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 155 | + hass = call.hass |
| 156 | + actions = get_entity_actions_controller(hass, entity_id) |
| 157 | + return await actions.get_queue_items(call) |
| 158 | + |
| 159 | + |
| 160 | +async def move_queue_item_down(call: ServiceCall): |
| 161 | + """Service wrapper to move queue item down.""" |
| 162 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 163 | + hass = call.hass |
| 164 | + actions = get_entity_actions_controller(hass, entity_id) |
| 165 | + return await actions.move_queue_item_down(call) |
| 166 | + |
| 167 | + |
| 168 | +async def move_queue_item_next(call: ServiceCall): |
| 169 | + """Service wrapper to move queue item next.""" |
| 170 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 171 | + hass = call.hass |
| 172 | + actions = get_entity_actions_controller(hass, entity_id) |
| 173 | + return await actions.move_queue_item_next(call) |
| 174 | + |
| 175 | + |
| 176 | +async def move_queue_item_up(call: ServiceCall): |
| 177 | + """Service wrapper to move queue item up.""" |
| 178 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 179 | + hass = call.hass |
| 180 | + actions = get_entity_actions_controller(hass, entity_id) |
| 181 | + return await actions.move_queue_item_up(call) |
| 182 | + |
| 183 | + |
| 184 | +async def play_queue_item(call: ServiceCall): |
| 185 | + """Service wrapper to play a queue item.""" |
| 186 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 187 | + hass = call.hass |
| 188 | + actions = get_entity_actions_controller(hass, entity_id) |
| 189 | + return await actions.play_queue_item(call) |
| 190 | + |
| 191 | + |
| 192 | +async def remove_queue_item(call: ServiceCall): |
| 193 | + """Service wrapper to remove a queue item.""" |
| 194 | + entity_id = call.data[ATTR_PLAYER_ENTITY] |
| 195 | + hass = call.hass |
| 196 | + actions = get_entity_actions_controller(hass, entity_id) |
| 197 | + return await actions.remove_queue_item(call) |
| 198 | + |
| 199 | + |
| 200 | +async def send_command(call: ServiceCall): |
| 201 | + """Service wrapper to send command to Music Assistant.""" |
| 202 | + entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
| 203 | + hass = call.hass |
| 204 | + entry = hass.config_entries.async_get_entry(entry_id) |
| 205 | + actions = entry.runtime_data.actions |
| 206 | + return await actions.send_command(call) |
0 commit comments