mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Store runtime data inside the config entry in Radio Browser (#116821)
This commit is contained in:
parent
1cea22b8ba
commit
ead9c4af38
@ -11,10 +11,12 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN
|
||||
RadioBrowserConfigEntry = ConfigEntry[RadioBrowser]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: RadioBrowserConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Radio Browser from a config entry.
|
||||
|
||||
This integration doesn't set up any entities, as it provides a media source
|
||||
@ -28,11 +30,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
except (DNSError, RadioBrowserError) as err:
|
||||
raise ConfigEntryNotReady("Could not connect to Radio Browser API") from err
|
||||
|
||||
hass.data[DOMAIN] = radios
|
||||
entry.runtime_data = radios
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
del hass.data[DOMAIN]
|
||||
return True
|
||||
|
@ -6,7 +6,7 @@ import mimetypes
|
||||
|
||||
from radios import FilterBy, Order, RadioBrowser, Station
|
||||
|
||||
from homeassistant.components.media_player import BrowseError, MediaClass, MediaType
|
||||
from homeassistant.components.media_player import MediaClass, MediaType
|
||||
from homeassistant.components.media_source.error import Unresolvable
|
||||
from homeassistant.components.media_source.models import (
|
||||
BrowseMediaSource,
|
||||
@ -14,9 +14,9 @@ from homeassistant.components.media_source.models import (
|
||||
MediaSourceItem,
|
||||
PlayMedia,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from . import RadioBrowserConfigEntry
|
||||
from .const import DOMAIN
|
||||
|
||||
CODEC_TO_MIMETYPE = {
|
||||
@ -40,24 +40,21 @@ class RadioMediaSource(MediaSource):
|
||||
|
||||
name = "Radio Browser"
|
||||
|
||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
def __init__(self, hass: HomeAssistant, entry: RadioBrowserConfigEntry) -> None:
|
||||
"""Initialize RadioMediaSource."""
|
||||
super().__init__(DOMAIN)
|
||||
self.hass = hass
|
||||
self.entry = entry
|
||||
|
||||
@property
|
||||
def radios(self) -> RadioBrowser | None:
|
||||
def radios(self) -> RadioBrowser:
|
||||
"""Return the radio browser."""
|
||||
return self.hass.data.get(DOMAIN)
|
||||
return self.entry.runtime_data
|
||||
|
||||
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
|
||||
"""Resolve selected Radio station to a streaming URL."""
|
||||
radios = self.radios
|
||||
|
||||
if radios is None:
|
||||
raise Unresolvable("Radio Browser not initialized")
|
||||
|
||||
station = await radios.station(uuid=item.identifier)
|
||||
if not station:
|
||||
raise Unresolvable("Radio station is no longer available")
|
||||
@ -77,9 +74,6 @@ class RadioMediaSource(MediaSource):
|
||||
"""Return media."""
|
||||
radios = self.radios
|
||||
|
||||
if radios is None:
|
||||
raise BrowseError("Radio Browser not initialized")
|
||||
|
||||
return BrowseMediaSource(
|
||||
domain=DOMAIN,
|
||||
identifier=None,
|
||||
|
Loading…
x
Reference in New Issue
Block a user