Skip to content

Commit 390e6d3

Browse files
lackasCFenner
andauthored
fix: cache the payload in fetch_all_features on the per-device service (#813)
Co-authored-by: Christopher Fenner <9592452+CFenner@users.noreply.github.com>
1 parent 9478a04 commit 390e6d3

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

PyViCare/PyViCareCachedServiceBase.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def getProperty(self, accessor: ViCareDeviceAccessor, property_name: str) -> Any
3434
entities = self._extract_entities(data, accessor)
3535
return readFeature(entities, property_name)
3636

37+
def fetch_all_features(self, accessor: ViCareDeviceAccessor) -> Any:
38+
# cached, so a caller refreshing with fetch_all_features() warms the
39+
# cache the following getProperty calls read from
40+
return self._get_or_update_cache(accessor)
41+
3742
def setProperty(self, accessor: ViCareDeviceAccessor, property_name: str, action: str, data: Any) -> Any:
3843
response = super().setProperty(accessor, property_name, action, data)
3944
self.clear_cache()

PyViCare/PyViCareCachedServiceViaGateway.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,3 @@ def _fetch_uncached(self, accessor: ViCareDeviceAccessor) -> Any:
2828

2929
def _extract_entities(self, data: dict, accessor: ViCareDeviceAccessor) -> list[dict[str, Any]]:
3030
return filter_features_for_device(data["data"], accessor.device_id)
31-
32-
def fetch_all_features(self, accessor: ViCareDeviceAccessor) -> Any:
33-
# cached too, so per-device coordinators share one bulk fetch
34-
return self._get_or_update_cache(accessor)

tests/test_PyViCareCachedService.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def setUp(self):
2121
self.service = ViCareCachedService(
2222
self.oauth_mock, [], self.CACHE_DURATION)
2323

24+
def test_fetch_all_features_is_cached(self):
25+
"""fetch_all_features() must warm the cache, like the gateway variant.
26+
27+
Callers that refresh with fetch_all_features() and then read properties
28+
would otherwise pay for two requests per interval.
29+
"""
30+
with now_is('2000-01-01 00:00:00'):
31+
self.service.fetch_all_features(self.accessor)
32+
self.service.getProperty(self.accessor, "someprop")
33+
self.assertEqual(self.oauth_mock.get.call_count, 1)
34+
2435
def test_getProperty_existing(self):
2536
self.service.getProperty(self.accessor, "someprop")
2637
self.oauth_mock.get.assert_called_once_with(

0 commit comments

Comments
 (0)