Skip to content

Commit ae77c33

Browse files
committed
Fix creating cache folder during initialization
1 parent c892897 commit ae77c33

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

plugwise_usb/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ async def initialize(self) -> None:
276276
self._network = StickNetwork(self._controller)
277277
self._network.cache_folder = self._cache_folder
278278
self._network.cache_enabled = self._cache_enabled
279+
if self._cache_enabled:
280+
await self._network.initialize_cache()
279281

280282
@raise_not_connected
281283
@raise_not_initialized
@@ -285,6 +287,8 @@ async def start_network(self) -> None:
285287
self._network = StickNetwork(self._controller)
286288
self._network.cache_folder = self._cache_folder
287289
self._network.cache_enabled = self._cache_enabled
290+
if self._cache_enabled:
291+
await self._network.initialize_cache()
288292
await self._network.start()
289293

290294
@raise_not_connected

plugwise_usb/network/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ..api import NodeEvent, NodeType, StickEvent
1313
from ..connection import StickController
1414
from ..constants import UTF8
15-
from ..exceptions import MessageError, NodeError, StickError, StickTimeout
15+
from ..exceptions import CacheError, MessageError, NodeError, StickError, StickTimeout
1616
from ..messages.requests import (
1717
CirclePlusAllowJoiningRequest,
1818
NodeInfoRequest,
@@ -103,6 +103,12 @@ def cache_folder(self, cache_folder: str) -> None:
103103
for node in self._nodes.values():
104104
node.cache_folder = cache_folder
105105

106+
async def initialize_cache(self) -> None:
107+
"""Initialize the cache folder."""
108+
if not self._cache_enabled:
109+
raise CacheError("Unable to initialize cache, enable cache first.")
110+
await self._register.initialize_cache()
111+
106112
@property
107113
def controller_active(self) -> bool:
108114
"""Return True if network controller (Circle+) is discovered and active."""

plugwise_usb/network/registry.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from ..api import NodeType
1111
from ..constants import UTF8
12-
from ..exceptions import NodeError
12+
from ..exceptions import CacheError, NodeError
1313
from ..messages.requests import CirclePlusScanRequest, NodeAddRequest, NodeRemoveRequest
1414
from ..messages.responses import (
1515
CirclePlusScanResponse,
@@ -57,19 +57,18 @@ def cache_enabled(self) -> bool:
5757
def cache_enabled(self, enable: bool = True) -> None:
5858
"""Enable or disable usage of cache."""
5959
if enable and not self._cache_enabled:
60-
_LOGGER.debug("Cache is enabled")
60+
_LOGGER.debug("Enable cache")
6161
self._network_cache = NetworkRegistrationCache(self._cache_folder)
62-
self._network_cache_file_task = create_task(
63-
self._network_cache.initialize_cache()
64-
)
6562
elif not enable and self._cache_enabled:
66-
if self._network_cache is not None:
67-
self._network_cache_file_task = create_task(
68-
self._network_cache.delete_cache()
69-
)
70-
_LOGGER.debug("Cache is disabled")
63+
_LOGGER.debug("Disable cache")
7164
self._cache_enabled = enable
7265

66+
async def initialize_cache(self) -> None:
67+
"""Initialize cache"""
68+
if not self._cache_enabled:
69+
raise CacheError("Unable to initialize cache, enable cache first.")
70+
await self._network_cache.initialize_cache()
71+
7372
@property
7473
def cache_folder(self) -> str:
7574
"""Path to folder to store cached data."""

0 commit comments

Comments
 (0)