|
1 | | -"""Initialise Module for ECU Proxy.""" |
2 | | - |
3 | 1 | from datetime import datetime, timedelta |
4 | 2 | import logging |
5 | 3 | from typing import Any |
@@ -37,15 +35,48 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): |
37 | 35 |
|
38 | 36 | hass.data.setdefault(DOMAIN, {}) |
39 | 37 |
|
| 38 | + # Initialize the API manager |
40 | 39 | api_handler = APIManager(hass, config_entry) |
41 | 40 | await api_handler.setup_socket_servers() |
42 | 41 |
|
| 42 | + # Save the API handler in hass.data for later use |
43 | 43 | hass.data[DOMAIN][config_entry.entry_id] = {"api_handler": api_handler} |
44 | 44 |
|
| 45 | + # Forward any configured platforms (e.g., sensors) |
45 | 46 | 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 | + |
46 | 51 | return True |
47 | 52 |
|
48 | 53 |
|
| 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 | + |
49 | 80 | async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: |
50 | 81 | """Unload a config entry.""" |
51 | 82 |
|
@@ -98,7 +129,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: |
98 | 129 | hass, self.midnight_reset, "0", "0", "0", local=True |
99 | 130 | ) |
100 | 131 |
|
101 | | - # Get configuration. If initial data else options. |
| 132 | + # Get configuration |
102 | 133 | self.no_update_timeout = int(self.config_entry.data.get("no_update_timeout")) |
103 | 134 |
|
104 | 135 | # Add listener for 0 or None if no update. |
|
0 commit comments