Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The openhome component."""
2 
3 import logging
4 
5 import aiohttp
6 from async_upnp_client.client import UpnpError
7 from openhomedevice.device import Device
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers import config_validation as cv
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 PLATFORMS = [Platform.MEDIA_PLAYER, Platform.UPDATE]
20 
21 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
22 
23 
24 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
25  """Cleanup before removing config entry."""
26  unload_ok = await hass.config_entries.async_unload_platforms(
27  config_entry, PLATFORMS
28  )
29  hass.data[DOMAIN].pop(config_entry.entry_id)
30 
31  return unload_ok
32 
33 
35  hass: HomeAssistant,
36  config_entry: ConfigEntry,
37 ) -> bool:
38  """Set up the configuration config entry."""
39  _LOGGER.debug("Setting up config entry: %s", config_entry.unique_id)
40 
41  device = await hass.async_add_executor_job(Device, config_entry.data[CONF_HOST])
42 
43  try:
44  await device.init()
45  except (TimeoutError, aiohttp.ClientError, UpnpError) as exc:
46  raise ConfigEntryNotReady from exc
47 
48  _LOGGER.debug("Initialised device: %s", device.uuid())
49 
50  hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = device
51 
52  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
53 
54  return True
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:24
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:37