Skip to content

Commit

Permalink
Cleanup only counter part of dabpumps.api_history file
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Apr 7, 2024
1 parent d91da74 commit bd6413d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions custom_components/dabpumps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ def __init__(self, hass, username, password):
self._history_key = username.lower()
self._history_store = DabPumpsApiHistoryStore(hass, self._history_key)

# DO NOT cleanup history store after each restart.
# we rely on it now if communication to DAB Pumps fails
#
# self._hass.async_create_task(self._history_store.async_remove())
# Cleanup the history store after each restart.
asyncio.run_coroutine_threadsafe(self._async_cleanup_diagnostics(), hass.loop)
else:
# Use from a temporary coordinator during config-flow first time setup of component
self._hass = None
Expand Down Expand Up @@ -527,6 +525,25 @@ async def _async_worker(self, timestamp, context, request, response, token):
self._hass.async_create_task(_async_worker(self, timestamp, context, request, response, token))


async def _async_cleanup_diagnostics(self):
# worker function
async def _async_worker(self):
# Sanity check
if not self._history_store:
return None

# Only the counter part is reset.
# We retain the history and details information as we rely on it if communication to DAB Pumps fails.
data = await self._history_store.async_get_data() or {}
data["counter"] = {}
await self._history_store.async_set_data(data)

# Create the worker task to update diagnostics in the background,
# but do not let main loop wait for it to finish
if self._hass:
self._hass.async_create_task(_async_worker(self))


class DabPumpsApiAuthError(Exception):
"""Exception to indicate authentication failure."""

Expand Down Expand Up @@ -555,7 +572,7 @@ def __init__(self, hass, key):

async def _async_migrate_func(self, old_major_version, old_minor_version, old_data):
"""Migrate the history store data"""
if old_major_version < 2:
if old_major_version <= 1:
# version 1 had a flat structure and did not take into account to have multiple installations (with different username+password)
data = {
self._key: old_data
Expand Down

0 comments on commit bd6413d

Please sign in to comment.