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