@@ -246,7 +246,7 @@ def __init__(self, hass, config: dict[str,Any], options: dict[str,Any], is_temp=
246
246
self ._hass = hass
247
247
self ._store_key = self ._install_id
248
248
self ._store = StuderCoordinatorStore (hass , self ._store_key )
249
- self ._cache = None
249
+ self ._cache = {}
250
250
self ._cache_last_write = datetime .min
251
251
252
252
# Diagnostics gathering
@@ -263,18 +263,14 @@ async def start(self) -> bool:
263
263
self ._entity_map_ts = datetime .now ()
264
264
265
265
# Make sure our cache is available
266
- if self ._cache is None :
267
- if self ._store :
268
- _LOGGER .debug (f"Read persisted cache" )
269
- store = await self ._store .async_get_data () or {}
270
- self ._cache = store .get ("cache" , {})
271
- else :
272
- self ._cache = {}
266
+ await self ._async_read_cache ()
273
267
268
+ # Start our Api
274
269
return await self ._api .start ()
275
270
276
271
277
272
async def stop (self ):
273
+ # Stop our Api
278
274
await self ._api .stop ()
279
275
280
276
@@ -377,17 +373,7 @@ async def _async_update_data(self):
377
373
await self ._async_request_all_data ()
378
374
379
375
# Periodically persist the cache
380
- if self ._hass and \
381
- self ._store and \
382
- self ._cache and \
383
- (datetime .now () - self ._cache_last_write ).total_seconds () > CACHE_WRITE_PERIOD :
384
-
385
- _LOGGER .debug (f"Persist cache" )
386
- self ._cache_last_write = datetime .now ()
387
-
388
- store = await self ._store .async_get_data () or {}
389
- store ["cache" ] = self ._cache
390
- await self ._store .async_set_data (store )
376
+ await self ._async_persist_cache ()
391
377
392
378
# return updated data
393
379
return self ._get_data ()
@@ -449,26 +435,26 @@ async def async_modify_data(self, entity: StuderEntityData, value):
449
435
return False
450
436
451
437
452
- async def _async_update_cache (self , context , data , force = False ):
453
- """
454
- Update the memory cache.
455
- Persisted cache is saved periodicaly by another function
456
- """
457
- if self ._cache :
458
- data ["ts" ] = datetime .now ()
459
- self ._cache [context ] = data
460
-
461
-
462
- async def _async_fetch_from_cache (self , context ):
463
- """
464
- Fetch from the memory cache
465
- """
466
- if self ._cache :
467
- _LOGGER .debug (f"Fetch from cache: { context } " )
468
- return self ._cache .get (context , {})
438
+ async def _async_read_cache (self ):
439
+ if self ._store :
440
+ _LOGGER .debug (f"Read persisted cache" )
441
+ store = await self ._store .async_get_data () or {}
442
+ self ._cache = store .get ("cache" , {})
469
443
else :
470
- return {}
471
-
444
+ _LOGGER .warning (f"Using empty cache; no store available to read persisted cache from" )
445
+ self ._cache = {}
446
+
447
+
448
+ async def _async_persist_cache (self ):
449
+ if self ._store and (datetime .now () - self ._cache_last_write ).total_seconds () > CACHE_WRITE_PERIOD :
450
+
451
+ _LOGGER .debug (f"Persist cache" )
452
+ self ._cache_last_write = datetime .now ()
453
+
454
+ store = await self ._store .async_get_data () or {}
455
+ store ["cache" ] = self ._cache
456
+ await self ._store .async_set_data (store )
457
+
472
458
473
459
def _getModified (self , entity : StuderEntityData ) -> Any :
474
460
"""
@@ -483,15 +469,14 @@ async def _addModified(self, entity: StuderEntityData, value: Any):
483
469
"""
484
470
Remember a modified params value. Persist it in cache.
485
471
"""
486
- if self ._cache :
487
- modified_params = self ._cache .get (MODIFIED_PARAMS , {})
488
- modified_params [entity .object_id ] = value
472
+ modified_params = self ._cache .get (MODIFIED_PARAMS , {})
473
+ modified_params [entity .object_id ] = value
489
474
490
- self ._cache [MODIFIED_PARAMS ] = modified_params
491
- self ._cache [MODIFIED_PARAMS_TS ] = datetime .now ()
475
+ self ._cache [MODIFIED_PARAMS ] = modified_params
476
+ self ._cache [MODIFIED_PARAMS_TS ] = datetime .now ()
492
477
493
- # Trigger write of cache
494
- self ._cache_last_write = datetime .min
478
+ # Trigger write of cache
479
+ self ._cache_last_write = datetime .min
495
480
496
481
497
482
async def _addDiagnostic (self , diag_key : str , success : bool , e : Exception | None = None ):
0 commit comments