Skip to content

Commit

Permalink
fix: make cache not global (#122)
Browse files Browse the repository at this point in the history
* feat: add datetime parsing in cleanrecord

* chore: lint

* chore: lint

* fix: timezone for non-3.11

* chore: lint

* feat: add is_available for ha and here in future

* fix: add timeout as a variable and set a longer default timeout for cloud

* chore: lint

* fix: is_available true by default

* fix: status type as class variable

* fix: don't update status when it was none before listener

* fix: reduce info logs

* fix: don't cache device cache

* chore: lint

* fix: double keepalive

* fix: don't continue calling unsupported functions

* fix: revert keepalive for now
  • Loading branch information
Lash-L authored Sep 21, 2023
1 parent 36932c1 commit e119201
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions roborock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,21 @@ def __init__(self, attribute: RoborockAttribute, api: RoborockClient):
self.task = RepeatableTask(self.api.event_loop, self._async_value, EVICT_TIME)
self._value: Any = None
self._mutex = asyncio.Lock()
self.unsupported: bool = False

@property
def value(self):
return self._value

async def _async_value(self):
self._value = await self.api._send_command(self.attribute.get_command)
if self.unsupported:
return None
try:
self._value = await self.api._send_command(self.attribute.get_command)
except UnknownMethodError as err:
# Limit the amount of times we call unsupported methods
self.unsupported = True
raise err
return self._value

async def async_value(self):
Expand Down Expand Up @@ -161,9 +169,6 @@ async def refresh_value(self):
await self._async_value()


device_cache: dict[str, dict[CacheableAttribute, AttributeCache]] = {}


class RoborockClient:
def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int = 4) -> None:
self.event_loop = get_running_loop_or_create_one()
Expand All @@ -176,13 +181,9 @@ def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int =
self.keep_alive = KEEPALIVE
self._diagnostic_data: dict[str, dict[str, Any]] = {}
self._logger = RoborockLoggerAdapter(device_info.device.name, _LOGGER)
cache = device_cache.get(device_info.device.duid)
if not cache:
cache = {
cacheable_attribute: AttributeCache(attr, self) for cacheable_attribute, attr in get_cache_map().items()
}
device_cache[device_info.device.duid] = cache
self.cache: dict[CacheableAttribute, AttributeCache] = cache
self.cache: dict[CacheableAttribute, AttributeCache] = {
cacheable_attribute: AttributeCache(attr, self) for cacheable_attribute, attr in get_cache_map().items()
}
self._listeners: list[Callable[[str, CacheableAttribute, RoborockBase], None]] = []
self.is_available: bool = True
self.queue_timeout = queue_timeout
Expand Down

0 comments on commit e119201

Please sign in to comment.