Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Soma Smartshades."""
2 
3 from __future__ import annotations
4 
5 from api.soma_api import SomaApi
6 import voluptuous as vol
7 
8 from homeassistant import config_entries
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, CONF_PORT, Platform
11 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.typing import ConfigType
14 
15 from .const import API, DEVICES, DOMAIN, HOST, PORT
16 
17 CONFIG_SCHEMA = vol.Schema(
18  vol.All(
19  cv.deprecated(DOMAIN),
20  {
21  DOMAIN: vol.Schema(
22  {vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT): cv.string}
23  )
24  },
25  ),
26  extra=vol.ALLOW_EXTRA,
27 )
28 
29 PLATFORMS = [Platform.COVER, Platform.SENSOR]
30 
31 
32 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
33  """Set up the Soma component."""
34  if DOMAIN not in config:
35  return True
36 
37  hass.async_create_task(
38  hass.config_entries.flow.async_init(
39  DOMAIN,
40  data=config[DOMAIN],
41  context={"source": config_entries.SOURCE_IMPORT},
42  )
43  )
44 
45  return True
46 
47 
48 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
49  """Set up Soma from a config entry."""
50  hass.data[DOMAIN] = {}
51  api = await hass.async_add_executor_job(SomaApi, entry.data[HOST], entry.data[PORT])
52  devices = await hass.async_add_executor_job(api.list_devices)
53  hass.data[DOMAIN] = {API: api, DEVICES: devices["shades"]}
54 
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56 
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
61  """Unload a config entry."""
62  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:32
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:48
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:60