Skip to content

Commit b4c49ab

Browse files
authored
Merge pull request #12 from HAEdwin/Use-config_flow-settings-and-extended-error-handling
Use config flow settings and extended error handling
2 parents e5bb7e2 + 9187f86 commit b4c49ab

File tree

7 files changed

+169
-7
lines changed

7 files changed

+169
-7
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.

custom_components/apsystems_ecu_proxy/api.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def get_config_value(self, key, default_type):
7575
"""Get config value."""
7676
return default_type(self.config_entry.data.get(key))
7777

78+
def update_config(self, new_config_entry: ConfigEntry):
79+
"""Update configuration values based on a new config entry."""
80+
_LOGGER.debug("Updating config for API on port %s", self.port)
81+
self.send_to_ema = self.get_config_value("send_to_ema", bool)
82+
self.message_ignore_age = self.get_config_value("message_ignore_age", int)
83+
self.ema_host = self.get_config_value("ema_host", str)
84+
7885
async def start(self) -> bool:
7986
"""Start listening socket server."""
8087
try:
@@ -172,9 +179,13 @@ async def data_received(
172179
self.callback(ecu)
173180
except ConnectionResetError:
174181
_LOGGER.warning("Error: Connection was reset")
175-
except Exception: # noqa: BLE001
176-
_LOGGER.warning("Exception error with %s", traceback.format_exc())
177-
182+
except Exception:
183+
_LOGGER.warning(
184+
"Exception error with %s where data is: %s",
185+
traceback.format_exc(),
186+
data,
187+
)
188+
178189
def get_model(self, model_code: str) -> str:
179190
"""Get model from model code."""
180191
if model := ECU_MODELS_216.get(model_code) or ECU_MODELS_215.get(

custom_components/apsystems_ecu_proxy/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
],
1515
"requirements": [],
1616
"single_config_entry": true,
17-
"version": "v1.1.0"
17+
"version": "v1.2.0"
1818
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"options": {
3+
"step": {
4+
"init": {
5+
"data": {
6+
"ema_host": "EMA Host (3.67.1.32)",
7+
"message_ignore_age": "Alte Nachrichten ignorieren (Standard 1800 Sekunden)",
8+
"max_stub_interval": "Maximales Stub-Intervall (Standard 300 Sekunden)",
9+
"no_update_timeout": "Zeitüberschreitung beim Zurücksetzen der Werte (Standard 660 Sekunden)",
10+
"send_to_ema": "An EMA versenden"
11+
},
12+
"title": "Konfiguration"
13+
}
14+
}
15+
},
16+
"config": {
17+
"step": {
18+
"user": {
19+
"data": {
20+
"ema_host": "EMA Host (3.67.1.32)",
21+
"message_ignore_age": "Alte Nachrichten ignorieren (Standard 1800 Sekunden)",
22+
"max_stub_interval": "Maximales Stub-Intervall (Standard 300 Sekunden)",
23+
"no_update_timeout": "Zeitüberschreitung beim Zurücksetzen der Werte (Standard 660 Sekunden)",
24+
"send_to_ema": "An EMA versenden"
25+
},
26+
"title": "Konfiguration"
27+
}
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"options": {
3+
"step": {
4+
"init": {
5+
"data": {
6+
"ema_host": "EMA Host (3.67.1.32)",
7+
"message_ignore_age": "Ignorar mensajes antiguos (predeterminado 1800 segundos)",
8+
"max_stub_interval": "Intervalo máximo de código auxiliar (predeterminado 300 segundos)",
9+
"no_update_timeout": "Restablecer el tiempo de espera de los valores (predeterminado 660 segundos)",
10+
"send_to_ema": "Enviar a EMA"
11+
},
12+
"title": "Configuración"
13+
}
14+
}
15+
},
16+
"config": {
17+
"step": {
18+
"user": {
19+
"data": {
20+
"ema_host": "EMA Host (3.67.1.32)",
21+
"message_ignore_age": "Ignorar mensajes antiguos (predeterminado 1800 segundos)",
22+
"max_stub_interval": "Intervalo máximo de código auxiliar (predeterminado 300 segundos)",
23+
"no_update_timeout": "Restablecer el tiempo de espera de los valores (predeterminado 660 segundos)",
24+
"send_to_ema": "Enviar a EMA"
25+
},
26+
"title": "Configuración"
27+
}
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"options": {
3+
"step": {
4+
"init": {
5+
"data": {
6+
"ema_host": "EMA Host (3.67.1.32)",
7+
"message_ignore_age": "Ignorer les anciens messages (1 800 secondes par défaut)",
8+
"max_stub_interval": "Intervalle de stub maximum (300 secondes par défaut)",
9+
"no_update_timeout": "Délai d'expiration des valeurs de réinitialisation (660 secondes par défaut)",
10+
"send_to_ema": "Expédier vers l'EMA"
11+
},
12+
"title": "configuration"
13+
}
14+
}
15+
},
16+
"config": {
17+
"step": {
18+
"user": {
19+
"data": {
20+
"ema_host": "EMA Host (3.67.1.32)",
21+
"message_ignore_age": "Ignorer les anciens messages (1 800 secondes par défaut)",
22+
"max_stub_interval": "Intervalle de stub maximum (300 secondes par défaut)",
23+
"no_update_timeout": "Délai d'expiration des valeurs de réinitialisation (660 secondes par défaut)",
24+
"send_to_ema": "Expédier vers l'EMA"
25+
},
26+
"title": "configuration"
27+
}
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"options": {
3+
"step": {
4+
"init": {
5+
"data": {
6+
"ema_host": "EMA Host (3.67.1.32)",
7+
"message_ignore_age": "Negeer oude berichten (standaard 1800 seconden)",
8+
"max_stub_interval": "Maximale stub interval (standaard 300 seconden)",
9+
"no_update_timeout": "Reset waarden timeout (standaard 660 seconden)",
10+
"send_to_ema": "Verzend naar EMA"
11+
},
12+
"title": "Configuratie"
13+
}
14+
}
15+
},
16+
"config": {
17+
"step": {
18+
"user": {
19+
"data": {
20+
"ema_host": "EMA Host (3.67.1.32)",
21+
"message_ignore_age": "Negeer oude berichten (standaard 1800 seconden)",
22+
"max_stub_interval": "Maximale stub interval (standaard 300 seconden)",
23+
"no_update_timeout": "Reset waarden timeout (standaard 660 seconden)",
24+
"send_to_ema": "Verzend naar EMA"
25+
},
26+
"title": "Configuratie"
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)