Skip to content

Commit 8dd51e8

Browse files
committed
Optimize Parameter Value Fetching
2 parents a68bbc7 + 8efe050 commit 8dd51e8

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='wolf_comm',
8-
version='0.0.46',
8+
version='0.0.47',
99
author="Jan Rothkegel",
1010
author_email="jan.rothkegel@web.de",
1111
description="A package to communicate with Wolf SmartSet Cloud",

wolf_comm/wolf_client.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,13 @@ def try_and_parse(text, times):
244244
@staticmethod
245245
async def fetch_localized_text(culture: str):
246246
async with aiohttp.ClientSession() as session:
247-
# Try requested language first
248247
url = f"https://www.wolf-smartset.com/js/localized-text/text.culture.{culture}.js"
249248
async with session.get(url) as response:
250249
if response.status == 200 or response.status == 304:
251250
return await response.text()
252251

253-
# Fall back to English if requested language fails
254252
if culture != 'en':
255-
_LOGGER.debug("Language %s not found, falling back to English", culture)
253+
_LOGGER.debug("Culture %s not found, falling back to English", culture)
256254
url = "https://www.wolf-smartset.com/js/localized-text/text.culture.en.js"
257255
async with session.get(url) as response:
258256
if response.status == 200 or response.status == 304:
@@ -272,54 +270,57 @@ async def load_localized_json(self, region_input: str):
272270

273271
# api/portal/GetParameterValues
274272
async def fetch_value(self, gateway_id, system_id, parameters: list[Parameter]):
275-
# group requested parametes by bundle_id to do a single request per bundle_id
276273
values_combined = []
277274
bundles = {}
275+
last_access_map = {}
276+
278277
for param in parameters:
279-
if param.bundle_id not in bundles:
280-
bundles[param.bundle_id] = []
281-
bundles[param.bundle_id].append(param)
278+
bundles.setdefault(param.bundle_id, []).append(param)
279+
last_access_map.setdefault(param.bundle_id, None)
282280

283-
for bundle_id in bundles:
284-
if len(bundles[bundle_id]) == 0:
281+
for bundle_id, params in bundles.items():
282+
if not params:
285283
continue
286-
data = {
287-
BUNDLE_ID: bundle_id,
288-
BUNDLE: False,
289-
VALUE_ID_LIST: [param.value_id for param in parameters],
290-
GATEWAY_ID: gateway_id,
291-
SYSTEM_ID: system_id,
292-
GUI_ID_CHANGED: False,
293-
SESSION_ID: self.session_id,
294-
LAST_ACCESS: self.last_access,
295-
}
296-
_LOGGER.debug('Requesting %s values for BUNDLE_ID: %s', len(bundles[bundle_id]), bundle_id)
297284

298-
res = await self.__request("post","api/portal/GetParameterValues",json=data,headers={"Content-Type": "application/json"})
285+
data = {
286+
BUNDLE_ID: bundle_id,
287+
BUNDLE: False,
288+
VALUE_ID_LIST: [param.value_id for param in params],
289+
GATEWAY_ID: gateway_id,
290+
SYSTEM_ID: system_id,
291+
GUI_ID_CHANGED: False,
292+
SESSION_ID: self.session_id,
293+
LAST_ACCESS: last_access_map[bundle_id],
294+
}
295+
296+
_LOGGER.debug('Requesting %s values for BUNDLE_ID: %s', len(params), bundle_id)
297+
res = await self.__request("post", "api/portal/GetParameterValues", json=data, headers={"Content-Type": "application/json"})
299298

300299
if ERROR_CODE in res or ERROR_TYPE in res:
301-
if ERROR_MESSAGE in res and res[ERROR_MESSAGE] == ERROR_READ_PARAMETER:
302-
raise ParameterReadError(res)
303-
raise FetchFailed(res)
304-
305-
values_with_value = [Value(v[VALUE_ID], v[VALUE], v[STATE]) for v in res[VALUES] if VALUE in v]
306-
values_combined += values_with_value
300+
error_msg = f"Error {res.get(ERROR_CODE, '')}: {res.get(ERROR_MESSAGE, str(res))}"
301+
if ERROR_MESSAGE in res and res[ERROR_MESSAGE] == ERROR_READ_PARAMETER:
302+
raise ParameterReadError(error_msg)
303+
raise FetchFailed(error_msg)
304+
305+
values_combined.extend(
306+
Value(v[VALUE_ID], v[VALUE], v[STATE])
307+
for v in res[VALUES]
308+
if VALUE in v
309+
)
310+
last_access_map[bundle_id] = res[LAST_ACCESS]
307311

308-
self.last_access = res[LAST_ACCESS]
309312
_LOGGER.debug('requested values for %s parameters, got values for %s ', len(parameters), len(values_combined))
310313
return values_combined
311-
#return [
312-
# Value(v[VALUE_ID], v[VALUE], v[STATE]) for v in res[VALUES] if VALUE in v
313-
#]
314314

315315
# api/portal/WriteParameterValues
316-
async def write_value(self, gateway_id, system_id, Value):
316+
async def write_value(self, gateway_id, system_id, bundle_id, Value):
317317
data = {
318318
WRITE_PARAMETER_VALUES: [
319319
{"ValueId": Value[VALUE_ID], "Value": Value[STATE]}
320320
],
321321
SYSTEM_ID: system_id,
322322
GATEWAY_ID: gateway_id,
323+
BUNDLE_ID: bundle_id,
323324
}
324325

325326
res = await self.__request(
@@ -332,12 +333,10 @@ async def write_value(self, gateway_id, system_id, Value):
332333
_LOGGER.debug("Written values: %s", res)
333334

334335
if ERROR_CODE in res or ERROR_TYPE in res:
336+
error_msg = f"Error {res.get(ERROR_CODE, '')}: {res.get(ERROR_MESSAGE, str(res))}"
335337
if ERROR_MESSAGE in res and res[ERROR_MESSAGE] == ERROR_READ_PARAMETER:
336-
raise ParameterWriteError(res)
337-
raise WriteFailed(res)
338-
339-
if LAST_ACCESS in res:
340-
self.last_access = res[LAST_ACCESS]
338+
raise ParameterWriteError(error_msg)
339+
raise WriteFailed(error_msg)
341340

342341
return res
343342

0 commit comments

Comments
 (0)