1
1
from pyhkc .hkc_api import HKCAlarm
2
2
from datetime import timedelta
3
+ from homeassistant .core import HomeAssistant
4
+ from homeassistant .config_entries import ConfigEntry
3
5
from homeassistant .core import callback
4
6
from .const import DOMAIN , DEFAULT_UPDATE_INTERVAL , CONF_UPDATE_INTERVAL
5
7
6
- async def async_setup_entry (hass , entry ) :
8
+ async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
7
9
panel_id = entry .data ["panel_id" ]
8
10
panel_password = entry .data ["panel_password" ]
9
11
user_code = entry .data ["user_code" ]
@@ -12,30 +14,32 @@ async def async_setup_entry(hass, entry):
12
14
HKCAlarm , panel_id , panel_password , user_code
13
15
)
14
16
15
- # Get update interval from options, or use default
16
17
update_interval = entry .options .get (CONF_UPDATE_INTERVAL , DEFAULT_UPDATE_INTERVAL )
17
- SCAN_INTERVAL = timedelta (seconds = update_interval )
18
+ scan_interval = timedelta (seconds = update_interval )
18
19
19
- # Create a dictionary to store both the HKCAlarm instance and SCAN_INTERVAL
20
- entry_data = {
20
+ hass .data .setdefault (DOMAIN , {})[entry .entry_id ] = {
21
21
"hkc_alarm" : hkc_alarm ,
22
- "scan_interval" : SCAN_INTERVAL
22
+ "scan_interval" : scan_interval ,
23
23
}
24
24
25
- # Store the dictionary in hass.data
26
- hass .data .setdefault (DOMAIN , {})[entry .entry_id ] = entry_data
27
-
28
25
@callback
29
- def update_options (entry ):
30
- """Update options."""
31
- nonlocal entry_data
32
- update_interval = entry .options .get (CONF_UPDATE_INTERVAL , DEFAULT_UPDATE_INTERVAL )
33
- entry_data ["scan_interval" ] = timedelta (seconds = update_interval )
26
+ def update_options (updated_entry : ConfigEntry ) -> None :
27
+ update_interval = updated_entry .options .get (CONF_UPDATE_INTERVAL , DEFAULT_UPDATE_INTERVAL )
28
+ hass .data [DOMAIN ][updated_entry .entry_id ]["scan_interval" ] = timedelta (seconds = update_interval )
34
29
35
30
entry .add_update_listener (update_options )
36
31
37
- # Load platforms
38
32
await hass .config_entries .async_forward_entry_setups (
39
33
entry , ["alarm_control_panel" , "sensor" ]
40
34
)
41
35
return True
36
+
37
+ async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
38
+ unload_ok = await hass .config_entries .async_unload_platforms (entry , ["sensor" , "alarm_control_panel" ])
39
+ if unload_ok :
40
+ hass .data [DOMAIN ].pop (entry .entry_id )
41
+ return unload_ok
42
+
43
+ async def async_reload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> None :
44
+ await async_unload_entry (hass , entry )
45
+ await async_setup_entry (hass , entry )
0 commit comments