Browse Source

Allow messages from the VTN to not have an EiResponse element

This would cause a KeyError since version 0.5.12. This fixes #33.

Signed-off-by: Stan Janssen <stan.janssen@elaad.nl>
Stan Janssen 4 years ago
parent
commit
eda1110417
2 changed files with 60 additions and 4 deletions
  1. 5 4
      openleadr/client.py
  2. 55 0
      test/test_poll_responses.py

+ 5 - 4
openleadr/client.py

@@ -706,10 +706,11 @@ class OpenADRClient:
         except Exception as err:
             logger.error(f"The incoming message could not be parsed or validated: {err}")
             return None, {}
-        if message_payload['response']['response_code'] != 200:
-            logger.warning("We got a non-OK OpenADR response from the server: "
-                           f"{message_payload['response']['response_code']}: "
-                           f"{message_payload['response']['response_description']}")
+        if 'response' in message_payload and 'response_code' in message_payload['response']:
+            if message_payload['response']['response_code'] != 200:
+                logger.warning("We got a non-OK OpenADR response from the server: "
+                               f"{message_payload['response']['response_code']}: "
+                               f"{message_payload['response']['response_description']}")
         return message_type, message_payload
 
     async def _on_event(self, message):

+ 55 - 0
test/test_poll_responses.py

@@ -0,0 +1,55 @@
+from openleadr import OpenADRClient, OpenADRServer, objects, utils
+from functools import partial
+from dataclasses import asdict
+from datetime import datetime, timezone, timedelta
+import pytest
+
+def on_create_party_registration(registration_info):
+    return 'ven123', 'reg123'
+
+def poll_responder(ven_id, message_type, message_payload):
+    return message_type, message_payload
+
+
+event = objects.Event(event_descriptor=objects.EventDescriptor(event_id='event123',
+                                                               event_status='far',
+                                                               modification_number='1',
+                                                               market_context='http://marketcontext01'),
+                      event_signals=[objects.EventSignal(signal_name='simple',
+                                                         signal_type='level',
+                                                         signal_id=utils.generate_id(),
+                                                         intervals=[objects.Interval(dtstart=datetime.now(timezone.utc),
+                                                                                     duration=timedelta(minutes=10),
+                                                                                     signal_payload=1)])],
+                      targets=[{'ven_id': 'ven123'}])
+
+poll_responses = [('oadrResponse', {}),
+                  ('oadrDistributeEvent', {'events': [asdict(event)]}),
+                  ('oadrCreateReport', {'report_requests': [{'report_request_id': 'req123',
+                                                            'report_specifier': {'report_specifier_id': 'rsi123',
+                                                                                 'granularity': timedelta(seconds=10),
+                                                                                 'report_back_duration': timedelta(seconds=10),
+                                                                                 'specifier_payloads': [{'r_id': 'rid123',
+                                                                                                         'reading_type': 'Direct Read'}]}}]}),
+                  ('oadrCancelReport', {'report_request_id': 'report123',
+                                        'report_to_follow': False,
+                                        'request_id': 'request123'}),
+                  ('oadrRegisterReport', {'ven_id': 'ven123', 'reports': []}),
+                  ('oadrUpdateReport', {'ven_id': 'ven123'}),
+                  ('oadrCancelPartyRegistration', {'registration_id': 'reg123',
+                                                   'ven_id': 'ven123'}),
+                  ('oadrRequestReregistration', {'ven_id': 'ven123'})]
+
+@pytest.mark.parametrize('message_type,message_payload', poll_responses)
+@pytest.mark.asyncio
+async def test_message(message_type, message_payload):
+    server = OpenADRServer(vtn_id='myvtn')
+    server.add_handler('on_create_party_registration', on_create_party_registration)
+    server.add_handler('on_poll', partial(poll_responder, message_type=message_type, message_payload=message_payload))
+    client = OpenADRClient(ven_name='myven',
+                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
+    await server.run_async()
+    await client.create_party_registration()
+    response_type, response_payload = await client.poll()
+    await server.stop()
+    assert response_type == message_type