11"""Roborock storage."""
22
3- import dataclasses
43import logging
54from pathlib import Path
65import shutil
1716
1817STORAGE_PATH = f".storage/{ DOMAIN } "
1918MAPS_PATH = "maps"
20- CACHE_VERSION = 1
19+ CACHE_VERSION = 2
2120
2221
2322def _storage_path_prefix (hass : HomeAssistant , entry_id : str ) -> Path :
@@ -44,6 +43,31 @@ def remove(path_prefix: Path) -> None:
4443 await hass .async_add_executor_job (remove , path_prefix )
4544
4645
46+ class StoreImpl (Store [dict [str , Any ]]):
47+ """Store implementation for Roborock cache."""
48+
49+ def __init__ (self , hass : HomeAssistant , entry_id : str ) -> None :
50+ """Initialize StoreImpl."""
51+ super ().__init__ (
52+ hass ,
53+ version = CACHE_VERSION ,
54+ key = f"{ DOMAIN } /{ entry_id } " ,
55+ private = True ,
56+ )
57+
58+ async def _async_migrate_func (
59+ self ,
60+ old_major_version : int ,
61+ old_minor_version : int ,
62+ old_data : dict [str , Any ],
63+ ) -> dict [str , Any ]:
64+ """Wipe out old caches with the old format."""
65+ if old_major_version == 1 :
66+ # No need for migration as version 1 was never in any stable releases
67+ return {}
68+ return old_data
69+
70+
4771class CacheStore (Cache ):
4872 """Store and retrieve cache for a Roborock device.
4973
@@ -55,19 +79,14 @@ class CacheStore(Cache):
5579
5680 def __init__ (self , hass : HomeAssistant , entry_id : str ) -> None :
5781 """Initialize CacheStore."""
58- self ._cache_store = Store [dict [str , Any ]](
59- hass ,
60- version = CACHE_VERSION ,
61- key = f"{ DOMAIN } /{ entry_id } " ,
62- private = True ,
63- )
82+ self ._cache_store = StoreImpl (hass , entry_id )
6483 self ._cache_data : CacheData | None = None
6584
6685 async def get (self ) -> CacheData :
6786 """Retrieve cached metadata."""
6887 if self ._cache_data is None :
6988 if data := await self ._cache_store .async_load ():
70- self ._cache_data = CacheData ( ** data )
89+ self ._cache_data = CacheData . from_dict ( data )
7190 else :
7291 self ._cache_data = CacheData ()
7392
@@ -80,7 +99,7 @@ async def set(self, value: CacheData) -> None:
8099 async def flush (self ) -> None :
81100 """Flush cached metadata to disk."""
82101 if self ._cache_data is not None :
83- await self ._cache_store .async_save (dataclasses . asdict ( self ._cache_data ))
102+ await self ._cache_store .async_save (self ._cache_data . as_dict ( ))
84103
85104 async def async_remove (self ) -> None :
86105 """Remove cached metadata from disk."""
0 commit comments