"""The LG webOS TV integration.""" from aiowebostv import WebOsClient from homeassistant.components import notify as hass_notify from homeassistant.const import ( ATTR_CONFIG_ENTRY_ID, CONF_CLIENT_SECRET, CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STOP, Platform, ) from homeassistant.core import Event, HomeAssistant from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, PLATFORMS from .coordinator import WebOsTvConfigEntry, WebOsTvDataUpdateCoordinator from .services import async_setup_services CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the LG webOS TV platform.""" async_setup_services(hass) return True async def async_setup_entry(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> bool: """Set the config entry up.""" host = entry.data[CONF_HOST] key = entry.data[CONF_CLIENT_SECRET] client = WebOsClient(host, key, client_session=async_get_clientsession(hass)) entry.runtime_data = coordinator = WebOsTvDataUpdateCoordinator(hass, entry, client) await coordinator.async_config_entry_first_refresh() await client.register_state_update_callback(coordinator.async_handle_update) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) # set up notify platform, no entry support for notify component yet, # have to use discovery to load platform. hass.async_create_task( discovery.async_load_platform( hass, Platform.NOTIFY, DOMAIN, { CONF_NAME: entry.title, ATTR_CONFIG_ENTRY_ID: entry.entry_id, }, {}, ) ) async def async_on_stop(_event: Event) -> None: """Unregister callbacks and disconnect.""" client.clear_state_update_callbacks() await client.disconnect() entry.async_on_unload( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_on_stop) ) return True async def async_unload_entry(hass: HomeAssistant, entry: WebOsTvConfigEntry) -> bool: """Unload a config entry.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): client = entry.runtime_data.client await hass_notify.async_reload(hass, DOMAIN) client.clear_state_update_callbacks() await client.disconnect() return unload_ok