Skip to content

Commit a45f0a3

Browse files
committed
fix: move unique ID generation, error suppression & response validation to generic_call
1 parent 466fdc3 commit a45f0a3

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

ocpp/charge_point.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,6 @@ async def call(self, payload, suppress=True, unique_id=None):
381381
"""
382382
camel_case_payload = snake_to_camel_case(serialize_as_dict(payload))
383383

384-
unique_id = (
385-
unique_id if unique_id is not None else str(self._unique_id_generator())
386-
)
387-
388384
action_name = payload.__class__.__name__
389385
# Due to deprecated call and callresults, remove in the future.
390386
if "Payload" in payload.__class__.__name__:
@@ -397,15 +393,6 @@ async def call(self, payload, suppress=True, unique_id=None):
397393
unique_id=unique_id
398394
)
399395

400-
if response.message_type_id == MessageType.CallError:
401-
LOGGER.warning("Received a CALLError: %s'", response)
402-
if suppress:
403-
return
404-
raise response.to_exception()
405-
else:
406-
response.action = action_name
407-
validate_payload(response, self._ocpp_version)
408-
409396
snake_case_payload = camel_to_snake_case(response.payload)
410397
# Create the correct Payload instance based on the received payload. If
411398
# this method is called with a call.BootNotificationPayload, then it
@@ -437,6 +424,10 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
437424
CallError.
438425
"""
439426

427+
unique_id = (
428+
unique_id if unique_id is not None else str(self._unique_id_generator())
429+
)
430+
440431
call = Call(
441432
unique_id=unique_id,
442433
action=action,
@@ -450,7 +441,7 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
450441
async with self._call_lock:
451442
await self._send(call.to_json())
452443
try:
453-
return await self._get_specific_response(
444+
response = await self._get_specific_response(
454445
call.unique_id, self._response_timeout
455446
)
456447
except asyncio.TimeoutError:
@@ -459,6 +450,16 @@ async def generic_call(self, action, payload_json, suppress=True, unique_id=None
459450
f"{call.to_json()}."
460451
)
461452

453+
if response.message_type_id == MessageType.CallError:
454+
LOGGER.warning("Received a CALLError: %s'", response)
455+
if suppress:
456+
return
457+
raise response.to_exception()
458+
else:
459+
response.action = call.action
460+
validate_payload(response, self._ocpp_version)
461+
return response
462+
462463
async def _get_specific_response(self, unique_id, timeout):
463464
"""
464465
Return response with given unique ID or raise an asyncio.TimeoutError.

0 commit comments

Comments
 (0)