Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for VELUX KLF 200 devices."""
2 
3 from pyvlx import PyVLX, PyVLXException
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
7 from homeassistant.core import HomeAssistant, ServiceCall
8 
9 from .const import DOMAIN, LOGGER, PLATFORMS
10 
11 
12 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
13  """Set up the velux component."""
14  module = VeluxModule(hass, entry.data)
15  try:
16  module.setup()
17  await module.async_start()
18 
19  except PyVLXException as ex:
20  LOGGER.exception("Can't connect to velux interface: %s", ex)
21  return False
22 
23  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = module
24 
25  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
26 
27  return True
28 
29 
30 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Unload a config entry."""
32  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
33 
34 
36  """Abstraction for velux component."""
37 
38  def __init__(self, hass, domain_config):
39  """Initialize for velux component."""
40  self.pyvlxpyvlx = None
41  self._hass_hass = hass
42  self._domain_config_domain_config = domain_config
43 
44  def setup(self):
45  """Velux component setup."""
46 
47  async def on_hass_stop(event):
48  """Close connection when hass stops."""
49  LOGGER.debug("Velux interface terminated")
50  await self.pyvlxpyvlx.disconnect()
51 
52  async def async_reboot_gateway(service_call: ServiceCall) -> None:
53  await self.pyvlxpyvlx.reboot_gateway()
54 
55  self._hass_hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
56  host = self._domain_config_domain_config.get(CONF_HOST)
57  password = self._domain_config_domain_config.get(CONF_PASSWORD)
58  self.pyvlxpyvlx = PyVLX(host=host, password=password)
59 
60  self._hass_hass.services.async_register(
61  DOMAIN, "reboot_gateway", async_reboot_gateway
62  )
63 
64  async def async_start(self):
65  """Start velux component."""
66  LOGGER.debug("Velux interface started")
67  await self.pyvlxpyvlx.load_scenes()
68  await self.pyvlxpyvlx.load_nodes()
def __init__(self, hass, domain_config)
Definition: __init__.py:38
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:12
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30