forked from giachello/netradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_source.py
113 lines (92 loc) · 3.3 KB
/
media_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
"""NetRadio Media Source Implementation."""
from homeassistant.components.climate.const import ATTR_HVAC_MODE
import logging
from homeassistant.components.media_player.const import (
MEDIA_CLASS_DIRECTORY,
MEDIA_CLASS_MUSIC,
MEDIA_TYPE_MUSIC,
)
from homeassistant.core import HomeAssistant
from homeassistant.components.media_player.errors import BrowseError
from homeassistant.components.media_source.const import MEDIA_MIME_TYPES
from homeassistant.components.media_source.error import MediaSourceError, Unresolvable
from homeassistant.components.media_source.models import (
BrowseMediaSource,
MediaSource,
MediaSourceItem,
PlayMedia,
)
from .const import (
MIME_TYPE,
DOMAIN,
ENTITY_ID,
CONF_NETRADIO_RADIOS,
CONF_NETRADIO_RADIO_URL,
CONF_NETRADIO_RADIO_NAME,
CONF_NETRADIO_RADIO_ICON,
)
_LOGGER = logging.getLogger(__name__)
async def async_get_media_source(hass: HomeAssistant):
"""Set up NetRadio media source."""
# _LOGGER.info("NetRadio setting up:" + DOMAIN)
source = hass.data[DOMAIN][DOMAIN]
return source
class NetRadioSource(MediaSource):
"""Provide NetRadio URLs as media sources."""
entity_id = ENTITY_ID
_radios: list
@property
def radios(self):
"""Return the list of configured net radios URLs."""
return self._radios
def __init__(self, hass: HomeAssistant, radios: list):
"""Initialize NetRadio source."""
super().__init__(DOMAIN)
self.hass = hass
self._radios = radios
self._state = "Starting"
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
"""Resolve media to a url."""
url = item.identifier
_LOGGER.info("NetRadio resolve media: " + url)
return PlayMedia(url, MIME_TYPE)
async def async_browse_media(self, item: MediaSourceItem) -> BrowseMediaSource:
"""Return media."""
return self._browse_media(item.identifier)
def _browse_media(self, source: str) -> BrowseMediaSource:
"""Browse media."""
# If only one media dir is configured, use that as the local media root
if source == "" or source is None:
source = "Netradio"
media = BrowseMediaSource(
domain=DOMAIN,
identifier=source,
media_class=MEDIA_CLASS_DIRECTORY,
media_content_type="",
title="Netradio",
can_play=False,
can_expand=True,
thumbnail=None,
)
# Append first level children
media.children = []
for child_name in self._radios:
child = BrowseMediaSource(
domain=DOMAIN,
identifier=child_name.get(CONF_NETRADIO_RADIO_URL),
media_class=MEDIA_CLASS_MUSIC,
media_content_type=MEDIA_TYPE_MUSIC,
title=child_name.get(CONF_NETRADIO_RADIO_NAME)
+ " URL: "
+ child_name.get(CONF_NETRADIO_RADIO_URL),
thumbnail=child_name.get(CONF_NETRADIO_RADIO_ICON),
can_play=True,
can_expand=False,
)
media.children.append(child)
_LOGGER.info("Browse media completed")
return media
@property
def name(self):
"""Return the name."""
return "NetRadio"