88
99from .const import DOMAIN , LOGGER , PLATFORMS
1010
11+ type VeluxConfigEntry = ConfigEntry [PyVLX ]
12+
1113
1214async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
1315 """Set up the velux component."""
14- module = VeluxModule (hass , entry .data )
15- try :
16- module .setup ()
17- await module .async_start ()
16+ host = entry .data [CONF_HOST ]
17+ password = entry .data [CONF_PASSWORD ]
18+ pyvlx = PyVLX (host = host , password = password )
1819
20+ LOGGER .debug ("Velux interface started" )
21+ try :
22+ await pyvlx .load_scenes ()
23+ await pyvlx .load_nodes ()
1924 except PyVLXException as ex :
2025 LOGGER .exception ("Can't connect to velux interface: %s" , ex )
2126 return False
2227
23- hass .data .setdefault (DOMAIN , {})[entry .entry_id ] = module
28+ entry .runtime_data = pyvlx
29+
30+ async def on_hass_stop (event ):
31+ """Close connection when hass stops."""
32+ LOGGER .debug ("Velux interface terminated" )
33+ await pyvlx .disconnect ()
34+
35+ async def async_reboot_gateway (service_call : ServiceCall ) -> None :
36+ await pyvlx .reboot_gateway ()
37+
38+ entry .async_on_unload (
39+ hass .bus .async_listen_once (EVENT_HOMEASSISTANT_STOP , on_hass_stop )
40+ )
41+
42+ hass .services .async_register (DOMAIN , "reboot_gateway" , async_reboot_gateway )
2443
2544 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
2645
@@ -30,39 +49,3 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3049async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
3150 """Unload a config entry."""
3251 return await hass .config_entries .async_unload_platforms (entry , PLATFORMS )
33-
34-
35- class VeluxModule :
36- """Abstraction for velux component."""
37-
38- def __init__ (self , hass , domain_config ):
39- """Initialize for velux component."""
40- self .pyvlx = None
41- self ._hass = hass
42- self ._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 .pyvlx .disconnect ()
51-
52- async def async_reboot_gateway (service_call : ServiceCall ) -> None :
53- await self .pyvlx .reboot_gateway ()
54-
55- self ._hass .bus .async_listen_once (EVENT_HOMEASSISTANT_STOP , on_hass_stop )
56- host = self ._domain_config .get (CONF_HOST )
57- password = self ._domain_config .get (CONF_PASSWORD )
58- self .pyvlx = PyVLX (host = host , password = password )
59-
60- self ._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 .pyvlx .load_scenes ()
68- await self .pyvlx .load_nodes ()
0 commit comments