From df2dafd1d94b919c3ee244b5e0ba0454e23afb5d Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Tue, 3 Dec 2024 10:23:39 +0000 Subject: [PATCH] feat: enable cache validity to be passed to `TouchlineSL` In turn, the `TouchlineSL` class will then pass this down to the modules, ensuring they are instantiated with the correct cache validity --- pytouchlinesl/touchlinesl.py | 15 +++++++++++---- tests/conftest.py | 13 +++++++++++++ tests/test_touchlinesl.py | 9 +++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pytouchlinesl/touchlinesl.py b/pytouchlinesl/touchlinesl.py index 3a9b5f9..a054a20 100644 --- a/pytouchlinesl/touchlinesl.py +++ b/pytouchlinesl/touchlinesl.py @@ -31,13 +31,15 @@ def __init__( username: str | None = None, password: str | None = None, client: BaseClient | None = None, + cache_validity: int = 30, ): """Construct the instance with either credentials or an authenticated client. Args: - username: (Optional) Username for TouchlineSL account. Ignored if client is passed. - password: (Optional) Password for TouchlineSL account. Ignored if client is passed. - client: (Optional) An instance of a RothAPI class. + username: (Optional) Username for TouchlineSL account. Ignored if client is passed. + password: (Optional) Password for TouchlineSL account. Ignored if client is passed. + client: (Optional) An instance of a RothAPI class. + cache_validity: (Optional) The number of seconds for which module data should be cached. """ self._modules: list[Module] = [] @@ -52,6 +54,8 @@ def __init__( raise TypeError("username and password must be strings if no client is provided") self._client = RothAPI(username=username, password=password) + self._cache_validity = cache_validity + async def user_id(self) -> int: """Return the unique user ID of the authenticated account.""" return await self._client.user_id() @@ -64,7 +68,10 @@ async def modules(self, *, refresh: bool = False) -> list[Module]: """ if not self._modules or refresh: data = await self._client.modules() - self._modules = [Module(client=self._client, module_data=m) for m in data] + self._modules = [ + Module(client=self._client, module_data=m, cache_validity=self._cache_validity) + for m in data + ] return self._modules diff --git a/tests/conftest.py b/tests/conftest.py index f66a81b..4348746 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,6 +11,12 @@ def test_touchlinesl() -> TouchlineSL: return TouchlineSL(client=client) +@pytest.fixture +def test_touchlinesl_short_cache() -> TouchlineSL: + client = FakeRothAPI() + return TouchlineSL(client=client, cache_validity=0.1) + + @pytest.fixture async def test_module(test_touchlinesl: TouchlineSL) -> Module: m = await test_touchlinesl.module(module_id="1234a5678a9123a456a7891234a56789") @@ -18,6 +24,13 @@ async def test_module(test_touchlinesl: TouchlineSL) -> Module: return m +@pytest.fixture +async def test_module_short_cache(test_touchlinesl_short_cache: TouchlineSL) -> Module: + m = await test_touchlinesl_short_cache.module(module_id="1234a5678a9123a456a7891234a56789") + assert isinstance(m, Module) + return m + + @pytest.fixture async def test_zone(test_module: Module) -> Zone: z = await test_module.zone_by_name("Kitchen") diff --git a/tests/test_touchlinesl.py b/tests/test_touchlinesl.py index 4aa0c06..61e6678 100644 --- a/tests/test_touchlinesl.py +++ b/tests/test_touchlinesl.py @@ -40,6 +40,15 @@ async def test_modules(test_touchlinesl: TouchlineSL): modules = await test_touchlinesl.modules() for m in modules: assert isinstance(m, Module) + assert m._cache_validity == 30000 + + +@pytest.mark.asyncio +async def test_modules_short_cache(test_touchlinesl_short_cache: TouchlineSL): + modules = await test_touchlinesl_short_cache.modules() + for m in modules: + assert isinstance(m, Module) + assert m._cache_validity == 100 @pytest.mark.asyncio