Use runtime_data in dunehd (#136443)

This commit is contained in:
epenet 2025-01-24 15:45:34 +01:00 committed by GitHub
parent 98e59f01b7
commit 51bc56929b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 22 deletions

View File

@ -10,29 +10,21 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from .const import DOMAIN
PLATFORMS: Final[list[Platform]] = [Platform.MEDIA_PLAYER]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
type DuneHDConfigEntry = ConfigEntry[DuneHDPlayer]
async def async_setup_entry(hass: HomeAssistant, entry: DuneHDConfigEntry) -> bool:
"""Set up a config entry."""
host: str = entry.data[CONF_HOST]
player = DuneHDPlayer(host)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = player
entry.runtime_data = DuneHDPlayer(entry.data[CONF_HOST])
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: DuneHDConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -15,11 +15,11 @@ from homeassistant.components.media_player import (
MediaType,
async_process_play_media_url,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DuneHDConfigEntry
from .const import ATTR_MANUFACTURER, DEFAULT_NAME, DOMAIN
CONF_SOURCES: Final = "sources"
@ -37,14 +37,14 @@ DUNEHD_PLAYER_SUPPORT: Final[MediaPlayerEntityFeature] = (
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: DuneHDConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add Dune HD entities from a config_entry."""
unique_id = entry.entry_id
player: DuneHDPlayer = hass.data[DOMAIN][entry.entry_id]
async_add_entities([DuneHDPlayerEntity(player, DEFAULT_NAME, unique_id)], True)
async_add_entities(
[DuneHDPlayerEntity(entry.runtime_data, DEFAULT_NAME, entry.entry_id)], True
)
class DuneHDPlayerEntity(MediaPlayerEntity):