Media Player
media_player
media_player domain implementation.
This module also contains the FavoriteItem helper returned by
MediaPlayer.favorites, which recursively traverses the
media_player/browse_media tree and flattens it into a list of directly
playable items.
SPEC
module-attribute
SPEC: DomainSpec[MediaPlayer] = register_domain(DomainSpec(name='media_player', entity_cls=MediaPlayer))
The DomainSpec registered with the shared DomainRegistry.
NowPlaying
dataclass
Structured snapshot of the media currently playing on a media player.
Groups identity-related media attributes into a single object.
Position/progress fields are intentionally excluded — they change
continuously during playback. Instances are frozen so two snapshots
can be compared with == to detect whether the playing media
changed.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str or None
|
Active input source. |
title |
str or None
|
Media title. |
artist |
str or None
|
Media artist. |
album |
str or None
|
Album name. |
channel |
str or None
|
Channel name. |
content_type |
str or None
|
Media content type identifier. |
content_id |
str or None
|
Media content id. |
duration |
int or None
|
Media duration in seconds. |
entity_picture |
str or None
|
Absolute URL of the entity picture / album art. |
queue_position |
int or None
|
Current position in the play queue. |
queue_size |
int or None
|
Total items in the play queue. |
playlist |
str or None
|
Playlist name. |
repeat |
str or None
|
Repeat mode. |
next |
bool
|
Whether skip-next is supported. |
previous |
bool
|
Whether skip-previous is supported. |
Source code in src/haclient/domains/media_player.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
FavoriteItem
A flattened, directly-playable entry discovered via browse_media.
The item remembers which MediaPlayer it belongs to, along with the
media_content_id / media_content_type pair needed to play it.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
Display title. |
media_content_id |
str
|
Content identifier for playback. |
media_content_type |
str
|
Content type for playback. |
thumbnail |
str or None
|
Image URL. |
category |
str or None
|
Human-readable category label. |
media_class |
str or None
|
Raw media class from Home Assistant. |
Source code in src/haclient/domains/media_player.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
play
async
play() -> None
Play this favorite on its MediaPlayer.
Delegates to MediaPlayer.play_media with the captured
media_content_type and media_content_id, which invokes
the media_player.play_media Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
MediaPlayer
Bases: Entity
A Home Assistant media player entity.
Provides intent-specific methods for playback control, volume management, power, source selection, and media browsing/favorites.
Source code in src/haclient/domains/media_player.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
is_playing
property
is_playing: bool
Whether the media player is currently playing.
is_paused
property
is_paused: bool
Whether the media player is currently paused.
is_muted
property
is_muted: bool
Whether the media player is currently muted.
volume_level
property
volume_level: float | None
Current volume level (0.0–1.0) or None.
now_playing
property
now_playing: NowPlaying
Structured snapshot of the currently playing media.
Returns:
| Type | Description |
|---|---|
NowPlaying
|
A fresh, frozen snapshot built from the entity's current
attributes. The |
Notes
Every access constructs a new dataclass; no caching or I/O is performed. The result is independent of any subsequent state update, so it is safe to retain across event-loop turns.
__init__
__init__(entity_id: str, services: ServiceCaller, store: StateStore, clock: Clock) -> None
Initialise the media player and its media-change listener list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
Fully-qualified entity id (e.g. |
required |
services
|
ServiceCaller
|
Service-call port used to invoke HA services. |
required |
store
|
StateStore
|
State store the entity registers itself with. |
required |
clock
|
Clock
|
Scheduler used to dispatch async listeners. |
required |
Source code in src/haclient/domains/media_player.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
on_volume_change
on_volume_change(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for volume level changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
on_mute_change
on_mute_change(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for mute state changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
on_media_change
on_media_change(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when the playing media changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
on_play
on_play(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when playback starts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
on_pause
on_pause(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when playback pauses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
on_stop
on_stop(func: ValueChangeHandler) -> ValueChangeHandler
Register a listener for when playback stops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Sync or async callable invoked with |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The same func, returned for decorator use. |
Source code in src/haclient/domains/media_player.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
remove_granular_listener
remove_granular_listener(func: ValueChangeHandler) -> None
Remove a granular listener, including media-change listeners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
ValueChangeHandler
|
The exact handler previously registered via one of the
|
required |
Source code in src/haclient/domains/media_player.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
play
async
play() -> None
Resume / start playback.
Invokes the media_player.media_play Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call (for example, no media is loaded). |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
pause
async
pause() -> None
Pause playback.
Invokes the media_player.media_pause Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
play_pause
async
play_pause() -> None
Toggle play/pause.
Invokes the media_player.media_play_pause Home Assistant
service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
stop
async
stop() -> None
Stop playback.
Invokes the media_player.media_stop Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
next
async
next() -> None
Skip to the next track.
Invokes the media_player.media_next_track Home Assistant
service. This method does not consult
NowPlaying.next; calls to players that do not advertise
skip-next support will surface as CommandError. Pre-check
self.now_playing.next to avoid that.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call (e.g. the player does not support skip-next). |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
previous
async
previous() -> None
Skip to the previous track.
Invokes the media_player.media_previous_track Home Assistant
service. This method does not consult
NowPlaying.previous; calls to players that do not advertise
skip-previous support will surface as CommandError. Pre-check
self.now_playing.previous to avoid that.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
set_volume
async
set_volume(level: float) -> None
Set the volume level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
float
|
Volume level between |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If level is outside the |
Source code in src/haclient/domains/media_player.py
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | |
mute
async
mute(muted: bool = True) -> None
Mute or unmute the media player.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
muted
|
bool
|
|
True
|
Source code in src/haclient/domains/media_player.py
565 566 567 568 569 570 571 572 573 | |
power_on
async
power_on() -> None
Power the media player on.
Invokes the media_player.turn_on Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
power_off
async
power_off() -> None
Power the media player off.
Invokes the media_player.turn_off Home Assistant service.
Raises:
| Type | Description |
|---|---|
CommandError
|
If Home Assistant rejects the service call. |
HTTPError
|
If the REST call returns a non-2xx response. |
TimeoutError
|
If the call exceeds the configured request timeout. |
ConnectionClosedError
|
If the WebSocket disconnects mid-call. |
Source code in src/haclient/domains/media_player.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |
select_source
async
select_source(source: str) -> None
Select an input source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Name of the input source. Must be one of the values reported
in the entity's |
required |
Source code in src/haclient/domains/media_player.py
611 612 613 614 615 616 617 618 619 620 | |
play_media
async
play_media(media_content_type: str, media_content_id: str, **extra: Any) -> None
Play a specific media item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
media_content_type
|
str
|
Media content type identifier (e.g. |
required |
media_content_id
|
str
|
Media content id understood by the underlying integration. |
required |
**extra
|
Any
|
Additional fields forwarded verbatim to Home Assistant
(e.g. |
{}
|
Source code in src/haclient/domains/media_player.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
browse_media
async
browse_media(media_content_type: str | None = None, media_content_id: str | None = None) -> dict[str, Any]
Issue a single media_player/browse_media WebSocket command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
media_content_type
|
str or None
|
Restrict browsing to this content type. |
None
|
media_content_id
|
str or None
|
Restrict browsing to this content id. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The raw result dictionary from Home Assistant. |
Raises:
| Type | Description |
|---|---|
HAClientError
|
If the command fails or returns an unexpected response. |
Source code in src/haclient/domains/media_player.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
favorites
async
favorites(*, max_depth: int = _MAX_BROWSE_DEPTH, max_nodes: int = _MAX_BROWSE_NODES) -> list[FavoriteItem]
Return a flattened list of playable items in the media tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_depth
|
int
|
Maximum recursion depth. |
_MAX_BROWSE_DEPTH
|
max_nodes
|
int
|
Hard upper bound on total nodes visited. |
_MAX_BROWSE_NODES
|
Returns:
| Type | Description |
|---|---|
list of FavoriteItem
|
The discovered playable items. |
Source code in src/haclient/domains/media_player.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |