Skip to content

Commit

Permalink
Merge pull request #26 from jasonmadigan/dynamic-reload
Browse files Browse the repository at this point in the history
feat: added dynamic reload
  • Loading branch information
jasonmadigan authored Jul 26, 2024
2 parents a219190 + 53c7cf8 commit 054ac80
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions custom_components/hkc_alarm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pyhkc.hkc_api import HKCAlarm
from datetime import timedelta
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from .const import DOMAIN, DEFAULT_UPDATE_INTERVAL, CONF_UPDATE_INTERVAL

async def async_setup_entry(hass, entry):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
panel_id = entry.data["panel_id"]
panel_password = entry.data["panel_password"]
user_code = entry.data["user_code"]
Expand All @@ -12,30 +14,32 @@ async def async_setup_entry(hass, entry):
HKCAlarm, panel_id, panel_password, user_code
)

# Get update interval from options, or use default
update_interval = entry.options.get(CONF_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL)
SCAN_INTERVAL = timedelta(seconds=update_interval)
scan_interval = timedelta(seconds=update_interval)

# Create a dictionary to store both the HKCAlarm instance and SCAN_INTERVAL
entry_data = {
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"hkc_alarm": hkc_alarm,
"scan_interval": SCAN_INTERVAL
"scan_interval": scan_interval,
}

# Store the dictionary in hass.data
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = entry_data

@callback
def update_options(entry):
"""Update options."""
nonlocal entry_data
update_interval = entry.options.get(CONF_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL)
entry_data["scan_interval"] = timedelta(seconds=update_interval)
def update_options(updated_entry: ConfigEntry) -> None:
update_interval = updated_entry.options.get(CONF_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL)
hass.data[DOMAIN][updated_entry.entry_id]["scan_interval"] = timedelta(seconds=update_interval)

entry.add_update_listener(update_options)

# Load platforms
await hass.config_entries.async_forward_entry_setups(
entry, ["alarm_control_panel", "sensor"]
)
return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
unload_ok = await hass.config_entries.async_unload_platforms(entry, ["sensor", "alarm_control_panel"])
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)

0 comments on commit 054ac80

Please sign in to comment.