Skip to content

Commit 1581600

Browse files
authored
Update __init__.py
- Added config update listener - Added updater for API instances settings
1 parent e5bb7e2 commit 1581600

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

custom_components/apsystems_ecu_proxy/__init__.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"""Initialise Module for ECU Proxy."""
2-
31
from datetime import datetime, timedelta
42
import logging
53
from typing import Any
@@ -37,15 +35,48 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
3735

3836
hass.data.setdefault(DOMAIN, {})
3937

38+
# Initialize the API manager
4039
api_handler = APIManager(hass, config_entry)
4140
await api_handler.setup_socket_servers()
4241

42+
# Save the API handler in hass.data for later use
4343
hass.data[DOMAIN][config_entry.entry_id] = {"api_handler": api_handler}
4444

45+
# Forward any configured platforms (e.g., sensors)
4546
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
47+
48+
# Add an update listener to listen for config entry changes
49+
config_entry.add_update_listener(update_listener)
50+
4651
return True
4752

4853

54+
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
55+
"""Handle configuration entry updates."""
56+
_LOGGER.debug("Config entry updated: %s", config_entry.data)
57+
58+
# Get updated data from the config entry
59+
new_timeout = int(config_entry.data.get("no_update_timeout"))
60+
# Update the configuration for the relevant API handler(s)
61+
api_handler = hass.data[DOMAIN][config_entry.entry_id]["api_handler"]
62+
63+
if api_handler:
64+
if new_timeout != api_handler.no_update_timeout:
65+
_LOGGER.debug("no_update_timeout has changed. Updating API manager.")
66+
api_handler.no_update_timeout = new_timeout
67+
68+
# Reset the existing no_update_timer.
69+
if api_handler.no_update_timer_unregister:
70+
api_handler.no_update_timer_unregister()
71+
api_handler.no_update_timer_unregister = async_call_later(
72+
hass, timedelta(seconds=new_timeout), api_handler.fire_no_update
73+
)
74+
75+
# Update config values in api module.
76+
for socket_server in api_handler.socket_servers:
77+
socket_server.update_config(config_entry)
78+
79+
4980
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
5081
"""Unload a config entry."""
5182

@@ -98,7 +129,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
98129
hass, self.midnight_reset, "0", "0", "0", local=True
99130
)
100131

101-
# Get configuration. If initial data else options.
132+
# Get configuration
102133
self.no_update_timeout = int(self.config_entry.data.get("no_update_timeout"))
103134

104135
# Add listener for 0 or None if no update.

0 commit comments

Comments
 (0)