Skip to content

Commit da5e4d8

Browse files
committed
request refresh after 10 sec after data-write
1 parent cacdc2e commit da5e4d8

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

custom_components/goecharger_api2/__init__.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
104104

105105

106106
def check_unload_services(hass: HomeAssistant):
107-
active_integration_configs = hass.config_entries.async_entries(domain=DOMAIN, include_disabled=False,
108-
include_ignore=False)
107+
active_integration_configs = hass.config_entries.async_entries(domain=DOMAIN, include_disabled=False, include_ignore=False)
109108
if active_integration_configs is not None and len(active_integration_configs) > 0:
110109
return False
111110
else:
@@ -289,6 +288,7 @@ async def trigger_restart_delayed(self) -> None:
289288

290289
async def _async_update_data(self) -> dict:
291290
"""Update data via library."""
291+
_LOGGER.debug(f"_async_update_data(): CALLED")
292292
if self._CLIENT_COMMUNICATION_ERROR_TS + 3600 > time():
293293
_LOGGER.info(f"_async_update_data(): skipping update due to client communication error for the next {3600 - (time() - self._CLIENT_COMMUNICATION_ERROR_TS)} seconds")
294294
return self.data
@@ -338,58 +338,45 @@ async def _async_update_data(self) -> dict:
338338
# ret = await self.bridge.async_write_values(kv_pairs)
339339
# return ret
340340

341-
def _request_update_in_5_sec(self):
341+
def _request_update_in_sec(self, seconds: int):
342342
if self._debounced_update_task is not None and not self._debounced_update_task.done():
343343
self._debounced_update_task.cancel()
344-
self._debounced_update_task = asyncio.create_task(self._debounce_coordinator_update())
344+
self._debounced_update_task = asyncio.create_task(self._debounce_coordinator_update(seconds))
345345

346-
async def _debounce_coordinator_update(self):
347-
await asyncio.sleep(5)
346+
async def _debounce_coordinator_update(self, seconds: int):
347+
await asyncio.sleep(seconds)
348+
if self.bridge is not None:
349+
self.bridge.reset_stored_update_ts()
348350
await self.async_refresh()
349351

350-
async def async_write_key(self, key: str, value, entity: Entity = None) -> dict:
351-
"""Update single data"""
352-
result = await self.bridge.write_value_to_key(key, value)
353-
_LOGGER.debug(f"write result: {result}")
354-
352+
def handle_write_resut(self, a_type, value, key, result, entity):
353+
_LOGGER.debug(f"write {a_type} result: {result}")
355354
if key in result:
356355
self.data[key] = result[key]
357356
else:
358-
_LOGGER.error(f"could not write value: '{value}' to: {key} result was: {result}")
359-
360-
if entity is not None:
361-
entity.async_schedule_update_ha_state(force_refresh=True)
357+
_LOGGER.error(f"could not write {a_type} value: '{value}' to: {key} result was: {result}")
362358

359+
do_refresh = True
363360
if self.intg_type == INTG_TYPE.CHARGER.value:
364361
# since we do not force an update when setting PV surplus data, we 'patch' internally our values
365362
if key == Tag.IDS.key:
366363
self.data = self.bridge._versions | self.bridge._states | self.bridge._config
367364
self.async_update_listeners()
365+
do_refresh = False
366+
367+
if do_refresh:
368+
if entity is not None:
369+
entity.async_schedule_update_ha_state(force_refresh=True)
370+
self._request_update_in_sec(10)
368371

369-
self._request_update_in_5_sec()
372+
async def async_write_key(self, key: str, value, entity: Entity = None) -> dict:
373+
result = await self.bridge.write_value_to_key(key, value)
374+
self.handle_write_resut("single", value, key, result, entity)
370375
return result
371376

372377
async def async_write_multiple_keys(self, attr:dict, key: str, value, entity: Entity = None) -> dict:
373-
"""Update single data"""
374378
result = await self.bridge._write_values_int(attr, key, value)
375-
_LOGGER.debug(f"write multiple result: {result}")
376-
377-
if key in result:
378-
self.data[key] = result[key]
379-
else:
380-
_LOGGER.error(f"could not write multiple value: '{value}' to: {key} result was: {result}")
381-
382-
if entity is not None:
383-
entity.async_schedule_update_ha_state(force_refresh=True)
384-
385-
if self.intg_type == INTG_TYPE.CHARGER.value:
386-
# since we do not force an update when setting PV surplus data, we 'patch' internally our values
387-
if key == Tag.IDS.key:
388-
self.data = self.bridge._versions | self.bridge._states | self.bridge._config
389-
self.async_update_listeners()
390-
391-
392-
self._request_update_in_5_sec()
379+
self.handle_write_resut("multiple", value, key, result, entity)
393380
return result
394381

395382
async def read_versions(self):

custom_components/goecharger_api2/pygoecharger_ha/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def clear_data(self):
9999
self._states = {}
100100
self._config = {}
101101

102+
def reset_stored_update_ts(self):
103+
self._LAST_CONFIG_UPDATE_TS = 0
104+
self._LAST_FULL_STATE_UPDATE_TS = 0
105+
102106
async def read_system(self) -> dict:
103107
return await self._read_filtered_data(filters=self._FILTER_SYSTEMS, log_info="read_system")
104108

0 commit comments

Comments
 (0)