|
5 | 5 | rather than a comma-separated text blob. |
6 | 6 |
|
7 | 7 | Flow: user (host/port) → modules_menu → add_module (repeat) → finish |
8 | | -Options: init (menu) → add_module / remove_module → done |
| 8 | +Options: init (menu) → add_module / remove_module / change_connection / |
| 9 | + change_reporting → done |
9 | 10 | """ |
10 | 11 |
|
11 | 12 | from __future__ import annotations |
|
27 | 28 | import voluptuous as vol |
28 | 29 |
|
29 | 30 | from .const import ( |
| 31 | + CONF_LIVE_REPORTING, |
30 | 32 | CONF_MODULE_BARCODE, |
31 | 33 | CONF_MODULE_NAME, |
32 | 34 | CONF_MODULE_PEAK_POWER, |
33 | 35 | CONF_MODULE_STRING, |
34 | 36 | CONF_MODULES, |
| 37 | + CONF_WRITE_INTERVAL, |
| 38 | + DEFAULT_LIVE_REPORTING, |
35 | 39 | DEFAULT_PEAK_POWER, |
36 | 40 | DEFAULT_PORT, |
| 41 | + DEFAULT_WRITE_INTERVAL, |
37 | 42 | DOMAIN, |
38 | 43 | ) |
39 | 44 |
|
@@ -109,7 +114,7 @@ def _modules_description(modules: list[dict[str, Any]]) -> str: |
109 | 114 | class PyTapConfigFlow(ConfigFlow, domain=DOMAIN): |
110 | 115 | """Handle a config flow for PyTap.""" |
111 | 116 |
|
112 | | - VERSION = 4 |
| 117 | + VERSION = 5 |
113 | 118 |
|
114 | 119 | def __init__(self) -> None: |
115 | 120 | """Initialize the config flow.""" |
@@ -242,7 +247,13 @@ async def async_step_init( |
242 | 247 | """Show the options menu: change connection / add / remove / done.""" |
243 | 248 | return self.async_show_menu( |
244 | 249 | step_id="init", |
245 | | - menu_options=["change_connection", "add_module", "remove_module", "done"], |
| 250 | + menu_options=[ |
| 251 | + "change_connection", |
| 252 | + "change_reporting", |
| 253 | + "add_module", |
| 254 | + "remove_module", |
| 255 | + "done", |
| 256 | + ], |
246 | 257 | description_placeholders={ |
247 | 258 | "modules_list": _modules_description(self._modules), |
248 | 259 | }, |
@@ -308,6 +319,54 @@ async def async_step_change_connection( |
308 | 319 | errors=errors, |
309 | 320 | ) |
310 | 321 |
|
| 322 | + async def async_step_change_reporting( |
| 323 | + self, user_input: dict[str, Any] | None = None |
| 324 | + ) -> ConfigFlowResult: |
| 325 | + """Allow the user to change reporting settings. |
| 326 | +
|
| 327 | + Presents a 'Live reporting' checkbox (default ON) that delivers |
| 328 | + readings as they arrive. When unchecked, a write_interval field |
| 329 | + appears and readings are cached, then pushed as a time-weighted |
| 330 | + average every ``write_interval`` seconds. |
| 331 | + """ |
| 332 | + if user_input is not None: |
| 333 | + new_data = {**self._config_entry.data} |
| 334 | + live = user_input.get(CONF_LIVE_REPORTING, DEFAULT_LIVE_REPORTING) |
| 335 | + new_data[CONF_LIVE_REPORTING] = live |
| 336 | + if live: |
| 337 | + # Live mode — clear any stored interval |
| 338 | + new_data.pop(CONF_WRITE_INTERVAL, None) |
| 339 | + else: |
| 340 | + new_data[CONF_WRITE_INTERVAL] = user_input.get( |
| 341 | + CONF_WRITE_INTERVAL, DEFAULT_WRITE_INTERVAL |
| 342 | + ) |
| 343 | + self.hass.config_entries.async_update_entry( |
| 344 | + self._config_entry, data=new_data |
| 345 | + ) |
| 346 | + return await self.async_step_init() |
| 347 | + |
| 348 | + current_live = self._config_entry.data.get( |
| 349 | + CONF_LIVE_REPORTING, DEFAULT_LIVE_REPORTING |
| 350 | + ) |
| 351 | + current_write_interval = self._config_entry.data.get( |
| 352 | + CONF_WRITE_INTERVAL, DEFAULT_WRITE_INTERVAL |
| 353 | + ) |
| 354 | + schema = vol.Schema( |
| 355 | + { |
| 356 | + vol.Optional( |
| 357 | + CONF_LIVE_REPORTING, default=current_live |
| 358 | + ): bool, |
| 359 | + vol.Optional( |
| 360 | + CONF_WRITE_INTERVAL, default=current_write_interval |
| 361 | + ): vol.All(vol.Coerce(int), vol.Range(min=5, max=300)), |
| 362 | + } |
| 363 | + ) |
| 364 | + |
| 365 | + return self.async_show_form( |
| 366 | + step_id="change_reporting", |
| 367 | + data_schema=schema, |
| 368 | + ) |
| 369 | + |
311 | 370 | async def async_step_add_module( |
312 | 371 | self, user_input: dict[str, Any] | None = None |
313 | 372 | ) -> ConfigFlowResult: |
|
0 commit comments