mobilityhouse / ocpp

Python implementation of the Open Charge Point Protocol (OCPP).
MIT License
744 stars 291 forks source link

Serialization of `call_result.SetVariables` is incorrect when `set_variable_result` attribute contains more than 1 element #635

Closed OrangeTux closed 2 months ago

OrangeTux commented 2 months ago

ocpp.charge_point.serialize_as_dict() wrongly serializes a payload that contains a collection of multiple items. The following payload contains a collection of 2 SetVariableResultType. But the serializes output contains only a single item.

Thanks @OSkrk for making me aware of this issue.

Consider this payload:

    payload = call_result.SetVariables(
        set_variable_result=[
            datatypes.SetVariableResultType(
                attribute_status=enums.SetVariableStatusType.accepted,
                component={
                    "name": "TemperatureSensor",
                    "instance": "First",
                    "evse": {"id": 1, "connector_id": 1},
                },
                variable={"name": "DisplayUnit", "instance": "Main"},
                attribute_type="Actual",
                attribute_status_info=None,
            ),
            datatypes.SetVariableResultType(
                attribute_status="Accepted",
                component={"name": "TxCtrlr"},
                variable={"name": "TxStopPoint"},
                attribute_type="Actual",
                attribute_status_info=None,
            ),
        ],
        custom_data=None,
    )

It's serialized as this:

           'set_variable_result': [{'attribute_status': 'Accepted',
                                     'attribute_status_info': None,
                                     'attribute_type': 'Actual',
                                     'component': {'name': 'TxCtrlr'},
                                     'variable': {'name': 'TxStopPoint'}}],