4
4
5
5
from collections .abc import Awaitable , Callable , Coroutine , Sequence
6
6
from contextlib import suppress
7
+ import dataclasses
7
8
from datetime import datetime
8
9
from functools import reduce , wraps
9
10
import logging
42
43
)
43
44
from homeassistant .components .media_source import BrowseMediaSource
44
45
from homeassistant .const import Platform
45
- from homeassistant .core import HomeAssistant , callback
46
+ from homeassistant .core import (
47
+ HomeAssistant ,
48
+ ServiceResponse ,
49
+ SupportsResponse ,
50
+ callback ,
51
+ )
46
52
from homeassistant .exceptions import HomeAssistantError , ServiceValidationError
47
53
from homeassistant .helpers import (
48
54
config_validation as cv ,
56
62
57
63
from .const import (
58
64
DOMAIN as HEOS_DOMAIN ,
65
+ SERVICE_GET_QUEUE ,
59
66
SERVICE_GROUP_VOLUME_DOWN ,
60
67
SERVICE_GROUP_VOLUME_SET ,
61
68
SERVICE_GROUP_VOLUME_UP ,
@@ -132,6 +139,12 @@ async def async_setup_entry(
132
139
"""Add media players for a config entry."""
133
140
# Register custom entity services
134
141
platform = entity_platform .async_get_current_platform ()
142
+ platform .async_register_entity_service (
143
+ SERVICE_GET_QUEUE ,
144
+ None ,
145
+ "async_get_queue" ,
146
+ supports_response = SupportsResponse .ONLY ,
147
+ )
135
148
platform .async_register_entity_service (
136
149
SERVICE_GROUP_VOLUME_SET ,
137
150
{vol .Required (ATTR_MEDIA_VOLUME_LEVEL ): cv .small_float },
@@ -155,20 +168,20 @@ def add_entities_callback(players: Sequence[HeosPlayer]) -> None:
155
168
add_entities_callback (list (coordinator .heos .players .values ()))
156
169
157
170
158
- type _FuncType [** _P ] = Callable [_P , Awaitable [Any ]]
159
- type _ReturnFuncType [** _P ] = Callable [_P , Coroutine [Any , Any , None ]]
171
+ type _FuncType [** _P , _R ] = Callable [_P , Awaitable [_R ]]
172
+ type _ReturnFuncType [** _P , _R ] = Callable [_P , Coroutine [Any , Any , _R ]]
160
173
161
174
162
- def catch_action_error [** _P ](
175
+ def catch_action_error [** _P , _R ](
163
176
action : str ,
164
- ) -> Callable [[_FuncType [_P ]], _ReturnFuncType [_P ]]:
177
+ ) -> Callable [[_FuncType [_P , _R ]], _ReturnFuncType [_P , _R ]]:
165
178
"""Return decorator that catches errors and raises HomeAssistantError."""
166
179
167
- def decorator (func : _FuncType [_P ]) -> _ReturnFuncType [_P ]:
180
+ def decorator (func : _FuncType [_P , _R ]) -> _ReturnFuncType [_P , _R ]:
168
181
@wraps (func )
169
- async def wrapper (* args : _P .args , ** kwargs : _P .kwargs ) -> None :
182
+ async def wrapper (* args : _P .args , ** kwargs : _P .kwargs ) -> _R :
170
183
try :
171
- await func (* args , ** kwargs )
184
+ return await func (* args , ** kwargs )
172
185
except (HeosError , ValueError ) as ex :
173
186
raise HomeAssistantError (
174
187
translation_domain = HEOS_DOMAIN ,
@@ -268,6 +281,12 @@ async def async_added_to_hass(self) -> None:
268
281
self .async_on_remove (self ._player .add_on_player_event (self ._player_update ))
269
282
await super ().async_added_to_hass ()
270
283
284
+ @catch_action_error ("get queue" )
285
+ async def async_get_queue (self ) -> ServiceResponse :
286
+ """Get the queue for the current player."""
287
+ queue = await self ._player .get_queue ()
288
+ return {"queue" : [dataclasses .asdict (item ) for item in queue ]}
289
+
271
290
@catch_action_error ("clear playlist" )
272
291
async def async_clear_playlist (self ) -> None :
273
292
"""Clear players playlist."""
0 commit comments