Skip to content

Commit 1ebfb35

Browse files
fix: using cache only a single time
1 parent 1f9894e commit 1ebfb35

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

roborock/containers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def decamelize_obj(d: dict | list, ignore_keys: list[str]):
7676
@dataclass
7777
class RoborockBase:
7878
_ignore_keys = [] # type: ignore
79+
is_cached = False
7980

8081
@classmethod
8182
def from_dict(cls, data: dict[str, Any]):

roborock/local_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import async_timeout
99

10-
from . import DeviceData
10+
from . import DeviceData, RoborockBase
1111
from .api import COMMANDS_SECURED, QUEUE_TIMEOUT, RoborockClient
1212
from .exceptions import CommandVacuumError, RoborockConnectionException, RoborockException
1313
from .protocol import MessageParser
@@ -135,4 +135,9 @@ async def send_message(self, roborock_messages: list[RoborockMessage] | Roborock
135135
if exception:
136136
await self.async_disconnect()
137137
raise exception
138+
is_cached = next(
139+
(response for response in responses if isinstance(response, RoborockBase) and response.is_cached), None
140+
)
141+
if is_cached:
142+
await self.async_disconnect()
138143
return responses

roborock/util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def wrapped(*args, **kwargs):
3636

3737
class CacheableResult:
3838
last_run_result = None
39+
use_count = 0
3940

4041

4142
RT = TypeVar("RT", bound=Callable[..., Coroutine])
@@ -48,9 +49,12 @@ def fallback_cache(func: RT) -> RT:
4849
async def wrapped(*args, **kwargs):
4950
try:
5051
cache.last_run_result = await func(*args, **kwargs)
52+
cache.use_count = 0
5153
except Exception as e:
52-
if cache.last_run_result is None:
54+
if cache.last_run_result is None or cache.use_count > 0:
5355
raise e
56+
cache.last_run_result.is_cached = True
57+
cache.use_count += 1
5458
return cache.last_run_result
5559

5660
return wrapped # type: ignore

0 commit comments

Comments
 (0)