Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for EnOcean devices."""
2 
3 import voluptuous as vol
4 
5 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
6 from homeassistant.const import CONF_DEVICE
7 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.typing import ConfigType
10 
11 from .const import DATA_ENOCEAN, DOMAIN, ENOCEAN_DONGLE
12 from .dongle import EnOceanDongle
13 
14 CONFIG_SCHEMA = vol.Schema(
15  {DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.string})}, extra=vol.ALLOW_EXTRA
16 )
17 
18 
19 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
20  """Set up the EnOcean component."""
21  # support for text-based configuration (legacy)
22  if DOMAIN not in config:
23  return True
24 
25  if hass.config_entries.async_entries(DOMAIN):
26  # We can only have one dongle. If there is already one in the config,
27  # there is no need to import the yaml based config.
28  return True
29 
30  hass.async_create_task(
31  hass.config_entries.flow.async_init(
32  DOMAIN, context={"source": SOURCE_IMPORT}, data=config[DOMAIN]
33  )
34  )
35 
36  return True
37 
38 
39 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
40  """Set up an EnOcean dongle for the given entry."""
41  enocean_data = hass.data.setdefault(DATA_ENOCEAN, {})
42  usb_dongle = EnOceanDongle(hass, config_entry.data[CONF_DEVICE])
43  await usb_dongle.async_setup()
44  enocean_data[ENOCEAN_DONGLE] = usb_dongle
45 
46  return True
47 
48 
49 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
50  """Unload ENOcean config entry."""
51 
52  enocean_dongle = hass.data[DATA_ENOCEAN][ENOCEAN_DONGLE]
53  enocean_dongle.unload()
54  hass.data.pop(DATA_ENOCEAN)
55 
56  return True
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:19
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:49
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:39