mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Migrate lg_netcast to use runtime_data (#147338)
This commit is contained in:
parent
35f310748e
commit
b13dd4e6ca
@ -2,8 +2,10 @@
|
||||
|
||||
from typing import Final
|
||||
|
||||
from pylgnetcast import LgNetCastClient
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
@ -13,21 +15,25 @@ PLATFORMS: Final[list[Platform]] = [Platform.MEDIA_PLAYER]
|
||||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
type LgNetCastConfigEntry = ConfigEntry[LgNetCastClient]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config_entry: LgNetCastConfigEntry
|
||||
) -> bool:
|
||||
"""Set up a config entry."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
host = config_entry.data[CONF_HOST]
|
||||
access_token = config_entry.data[CONF_ACCESS_TOKEN]
|
||||
|
||||
client = LgNetCastClient(host, access_token)
|
||||
|
||||
config_entry.runtime_data = client
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: LgNetCastConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
if unload_ok:
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -47,14 +47,13 @@ async def async_validate_trigger_config(
|
||||
except ValueError as err:
|
||||
raise InvalidDeviceAutomationConfig(err) from err
|
||||
|
||||
if DOMAIN in hass.data:
|
||||
for config_entry_id in device.config_entries:
|
||||
if hass.data[DOMAIN].get(config_entry_id):
|
||||
break
|
||||
else:
|
||||
raise InvalidDeviceAutomationConfig(
|
||||
f"Device {device.id} is not from an existing {DOMAIN} config entry"
|
||||
)
|
||||
if not any(
|
||||
entry.entry_id in device.config_entries
|
||||
for entry in hass.config_entries.async_loaded_entries(DOMAIN)
|
||||
):
|
||||
raise InvalidDeviceAutomationConfig(
|
||||
f"Device {device.id} is not from an existing {DOMAIN} config entry"
|
||||
)
|
||||
|
||||
return config
|
||||
|
||||
|
@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pylgnetcast import LG_COMMAND, LgNetCastClient, LgNetCastError
|
||||
from pylgnetcast import LG_COMMAND, LgNetCastError
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.components.media_player import (
|
||||
@ -15,13 +15,13 @@ from homeassistant.components.media_player import (
|
||||
MediaPlayerState,
|
||||
MediaType,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_MODEL, CONF_NAME
|
||||
from homeassistant.const import CONF_MODEL, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.trigger import PluggableAction
|
||||
|
||||
from . import LgNetCastConfigEntry
|
||||
from .const import ATTR_MANUFACTURER, DOMAIN
|
||||
from .triggers.turn_on import async_get_turn_on_trigger
|
||||
|
||||
@ -46,20 +46,15 @@ SUPPORT_LGTV = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: LgNetCastConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a LG Netcast Media Player from a config_entry."""
|
||||
|
||||
host = config_entry.data[CONF_HOST]
|
||||
access_token = config_entry.data[CONF_ACCESS_TOKEN]
|
||||
unique_id = config_entry.unique_id
|
||||
name = config_entry.data.get(CONF_NAME, DEFAULT_NAME)
|
||||
model = config_entry.data[CONF_MODEL]
|
||||
|
||||
client = LgNetCastClient(host, access_token)
|
||||
|
||||
hass.data[DOMAIN][config_entry.entry_id] = client
|
||||
client = config_entry.runtime_data
|
||||
|
||||
async_add_entities([LgTVDevice(client, name, model, unique_id=unique_id)])
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user