Skip to content

Commit cacdc2e

Browse files
author
marq24
committed
.
1 parent 0fc16da commit cacdc2e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

custom_components/goecharger_api2/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ async def check_device_registry(hass: HomeAssistant):
214214

215215

216216
class GoeChargerDataUpdateCoordinator(DataUpdateCoordinator):
217+
218+
_debounced_update_task: asyncio.Task | None = None
219+
217220
def __init__(self, hass: HomeAssistant, config_entry):
218221
lang = hass.config.language.lower()
219222
self._hass = hass
@@ -259,6 +262,7 @@ def __init__(self, hass: HomeAssistant, config_entry):
259262
self._CLIENT_COMMUNICATION_ERROR_TS = 0
260263
self._CLIENT_COMMUNICATION_ERROR_COUNT = 0
261264
self._RESTART_TRIGGERED = False
265+
self._debounced_update_task = None
262266
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
263267

264268
# Callable[[Event], Any]
@@ -272,6 +276,7 @@ def clear_data(self):
272276
self._CLIENT_COMMUNICATION_ERROR_TS = 0
273277
self._CLIENT_COMMUNICATION_ERROR_COUNT = 0
274278
self._RESTART_TRIGGERED = False
279+
self._debounced_update_task = None
275280

276281
async def trigger_restart_delayed(self) -> None:
277282
# Generate a random sleep time between 5 and 10 minutes (300 and 600 seconds)
@@ -333,6 +338,15 @@ async def _async_update_data(self) -> dict:
333338
# ret = await self.bridge.async_write_values(kv_pairs)
334339
# return ret
335340

341+
def _request_update_in_5_sec(self):
342+
if self._debounced_update_task is not None and not self._debounced_update_task.done():
343+
self._debounced_update_task.cancel()
344+
self._debounced_update_task = asyncio.create_task(self._debounce_coordinator_update())
345+
346+
async def _debounce_coordinator_update(self):
347+
await asyncio.sleep(5)
348+
await self.async_refresh()
349+
336350
async def async_write_key(self, key: str, value, entity: Entity = None) -> dict:
337351
"""Update single data"""
338352
result = await self.bridge.write_value_to_key(key, value)
@@ -352,6 +366,7 @@ async def async_write_key(self, key: str, value, entity: Entity = None) -> dict:
352366
self.data = self.bridge._versions | self.bridge._states | self.bridge._config
353367
self.async_update_listeners()
354368

369+
self._request_update_in_5_sec()
355370
return result
356371

357372
async def async_write_multiple_keys(self, attr:dict, key: str, value, entity: Entity = None) -> dict:
@@ -373,6 +388,8 @@ async def async_write_multiple_keys(self, attr:dict, key: str, value, entity: En
373388
self.data = self.bridge._versions | self.bridge._states | self.bridge._config
374389
self.async_update_listeners()
375390

391+
392+
self._request_update_in_5_sec()
376393
return result
377394

378395
async def read_versions(self):

0 commit comments

Comments
 (0)