From f8b2fca18f826687e7b961cc96ce8ddc3efda4a3 Mon Sep 17 00:00:00 2001 From: Maikel Punie Date: Sat, 29 Jun 2024 14:52:38 +0200 Subject: [PATCH] close #106 --- requirements.txt | 1 + velbusaio/controller.py | 1 + velbusaio/handler.py | 13 +++++++------ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 5ed7aee..7f2250a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +aiofile backoff>=1.10.0 pyserial>=3.5.0 pyserial-asyncio-fast>=0.11 diff --git a/velbusaio/controller.py b/velbusaio/controller.py index 101024d..915d937 100644 --- a/velbusaio/controller.py +++ b/velbusaio/controller.py @@ -123,6 +123,7 @@ async def stop(self) -> None: async def connect(self, test_connect: bool = False) -> None: """Connect to the bus and load all the data.""" + await self._handler.read_protocol_data() auth = None # connect to the bus if ":" in self._dsn: diff --git a/velbusaio/handler.py b/velbusaio/handler.py index 6115e51..99add93 100644 --- a/velbusaio/handler.py +++ b/velbusaio/handler.py @@ -16,6 +16,8 @@ import os import pathlib +from aiofile import async_open + from typing import TYPE_CHECKING, Awaitable, Callable import pkg_resources @@ -49,15 +51,14 @@ def __init__( self._modulescan_address = 0 self._scan_complete = False self._scan_delay_msec = 0 - with open( + + async def read_protocol_data(self): + async with async_open( pkg_resources.resource_filename(__name__, "protocol.json") ) as protocol_file: - self.pdata = json.load(protocol_file) + self.pdata = json.loads(await protocol_file.read()) - async def scan(self) -> None: - reload_cache = not os.path.isfile( - pathlib.Path(f"{get_cache_dir()}/usecache.yes") - ) + async def scan(self, reload_cache: bool = False) -> None: self._log.info(f"Start module scan, reload cache {reload_cache}") while self._modulescan_address < 254: address = 0