1 """Media Source models."""
3 from __future__
import annotations
6 from dataclasses
import dataclass
7 from typing
import Any, cast
12 from .const
import DOMAIN, URI_SCHEME, URI_SCHEME_REGEX
15 @dataclass(slots=True)
17 """Represents a playable media."""
24 """Represent a browsable media file."""
27 self, *, domain: str |
None, identifier: str |
None, **kwargs: Any
29 """Initialize media source browse media."""
30 media_content_id = f
"{URI_SCHEME}{domain or ''}"
32 media_content_id += f
"/{identifier}"
34 super().
__init__(media_content_id=media_content_id, **kwargs)
40 @dataclass(slots=True)
42 """A parsed media item."""
47 target_media_player: str |
None
50 """Browse this item."""
51 if self.domain
is None:
55 media_class=MediaClass.APP,
56 media_content_type=MediaType.APPS,
57 title=
"Media Sources",
60 children_media_class=MediaClass.APP,
62 base.children = sorted(
67 media_class=MediaClass.APP,
68 media_content_type=MediaType.APP,
69 thumbnail=f
"https://brands.home-assistant.io/_/{source.domain}/logo.png",
74 for source
in self.hass.data[DOMAIN].values()
76 key=
lambda item: item.title,
83 """Resolve to playable item."""
88 """Return media source that owns this item."""
89 return cast(MediaSource, self.hass.data[DOMAIN][self.domain])
93 cls, hass: HomeAssistant, uri: str, target_media_player: str |
None
95 """Create an item from a uri."""
96 if not (match := URI_SCHEME_REGEX.match(uri)):
97 raise ValueError(
"Invalid media source URI")
99 domain = match.group(
"domain")
100 identifier = match.group(
"identifier")
102 return cls(hass, domain, identifier, target_media_player)
106 """Represents a source of media files."""
108 name: str |
None =
None
111 """Initialize a media source."""
113 if not self.
namename:
117 """Resolve a media item to a playable item."""
118 raise NotImplementedError
122 raise NotImplementedError