mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Store runtime data inside the config entry in VLC telnet (#116803)
store runtime data inside the config entry
This commit is contained in:
parent
64b5881652
commit
65120e5789
@ -1,5 +1,7 @@
|
|||||||
"""The VLC media player Telnet integration."""
|
"""The VLC media player Telnet integration."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from aiovlc.client import Client
|
from aiovlc.client import Client
|
||||||
from aiovlc.exceptions import AuthError, ConnectError
|
from aiovlc.exceptions import AuthError, ConnectError
|
||||||
|
|
||||||
@ -8,12 +10,22 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
|
|
||||||
from .const import DATA_AVAILABLE, DATA_VLC, DOMAIN, LOGGER
|
from .const import LOGGER
|
||||||
|
|
||||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||||
|
|
||||||
|
VlcConfigEntry = ConfigEntry["VlcData"]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
@dataclass
|
||||||
|
class VlcData:
|
||||||
|
"""Runtime data definition."""
|
||||||
|
|
||||||
|
vlc: Client
|
||||||
|
available: bool
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: VlcConfigEntry) -> bool:
|
||||||
"""Set up VLC media player Telnet from a config entry."""
|
"""Set up VLC media player Telnet from a config entry."""
|
||||||
config = entry.data
|
config = entry.data
|
||||||
|
|
||||||
@ -31,15 +43,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
LOGGER.warning("Failed to connect to VLC: %s. Trying again", err)
|
LOGGER.warning("Failed to connect to VLC: %s. Trying again", err)
|
||||||
available = False
|
available = False
|
||||||
|
|
||||||
|
async def _disconnect_vlc() -> None:
|
||||||
|
"""Disconnect from VLC."""
|
||||||
|
LOGGER.debug("Disconnecting from VLC")
|
||||||
|
try:
|
||||||
|
await vlc.disconnect()
|
||||||
|
except ConnectError as err:
|
||||||
|
LOGGER.warning("Connection error: %s", err)
|
||||||
|
|
||||||
if available:
|
if available:
|
||||||
try:
|
try:
|
||||||
await vlc.login()
|
await vlc.login()
|
||||||
except AuthError as err:
|
except AuthError as err:
|
||||||
await disconnect_vlc(vlc)
|
await _disconnect_vlc()
|
||||||
raise ConfigEntryAuthFailed from err
|
raise ConfigEntryAuthFailed from err
|
||||||
|
|
||||||
domain_data = hass.data.setdefault(DOMAIN, {})
|
entry.runtime_data = VlcData(vlc, available)
|
||||||
domain_data[entry.entry_id] = {DATA_VLC: vlc, DATA_AVAILABLE: available}
|
|
||||||
|
entry.async_on_unload(_disconnect_vlc)
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
@ -48,21 +69,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
entry_data = hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
vlc = entry_data[DATA_VLC]
|
|
||||||
|
|
||||||
await disconnect_vlc(vlc)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def disconnect_vlc(vlc: Client) -> None:
|
|
||||||
"""Disconnect from VLC."""
|
|
||||||
LOGGER.debug("Disconnecting from VLC")
|
|
||||||
try:
|
|
||||||
await vlc.disconnect()
|
|
||||||
except ConnectError as err:
|
|
||||||
LOGGER.warning("Connection error: %s", err)
|
|
||||||
|
@ -25,7 +25,8 @@ from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import DATA_AVAILABLE, DATA_VLC, DEFAULT_NAME, DOMAIN, LOGGER
|
from . import VlcConfigEntry
|
||||||
|
from .const import DEFAULT_NAME, DOMAIN, LOGGER
|
||||||
|
|
||||||
MAX_VOLUME = 500
|
MAX_VOLUME = 500
|
||||||
|
|
||||||
@ -34,13 +35,13 @@ _P = ParamSpec("_P")
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: VlcConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the vlc platform."""
|
"""Set up the vlc platform."""
|
||||||
# CONF_NAME is only present in imported YAML.
|
# CONF_NAME is only present in imported YAML.
|
||||||
name = entry.data.get(CONF_NAME) or DEFAULT_NAME
|
name = entry.data.get(CONF_NAME) or DEFAULT_NAME
|
||||||
vlc = hass.data[DOMAIN][entry.entry_id][DATA_VLC]
|
vlc = entry.runtime_data.vlc
|
||||||
available = hass.data[DOMAIN][entry.entry_id][DATA_AVAILABLE]
|
available = entry.runtime_data.available
|
||||||
|
|
||||||
async_add_entities([VlcDevice(entry, vlc, name, available)], True)
|
async_add_entities([VlcDevice(entry, vlc, name, available)], True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user