Skip to content

Commit

Permalink
feat: enable cache validity to be passed to TouchlineSL
Browse files Browse the repository at this point in the history
In turn, the `TouchlineSL` class will then pass this down to the
modules, ensuring they are instantiated with the correct cache
validity
  • Loading branch information
jnsgruk committed Dec 3, 2024
1 parent 3913ef6 commit df2dafd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pytouchlinesl/touchlinesl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand All @@ -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()
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ 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")
assert isinstance(m, 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")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_touchlinesl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df2dafd

Please sign in to comment.