-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathtest_charge_point_v16.py
1037 lines (973 loc) · 40.4 KB
/
test_charge_point_v16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Implement a test by a simulating an OCPP 1.6 chargepoint."""
import asyncio
from datetime import datetime, UTC # timedelta,
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry
import websockets
from custom_components.ocpp.button import BUTTONS
from custom_components.ocpp.const import DOMAIN as OCPP_DOMAIN
from custom_components.ocpp.enums import ConfigurationKey, HAChargerServices as csvcs
from custom_components.ocpp.number import NUMBERS
from custom_components.ocpp.switch import SWITCHES
from ocpp.routing import on
from ocpp.v16 import ChargePoint as cpclass, call, call_result
from ocpp.v16.enums import (
Action,
AuthorizationStatus,
AvailabilityStatus,
ChargePointErrorCode,
ChargePointStatus,
ChargingProfileStatus,
ClearChargingProfileStatus,
ConfigurationStatus,
DataTransferStatus,
DiagnosticsStatus,
FirmwareStatus,
RegistrationStatus,
RemoteStartStopStatus,
ResetStatus,
TriggerMessageStatus,
UnlockStatus,
)
from .const import MOCK_CONFIG_DATA, MOCK_CONFIG_DATA_2
from .charge_point_test import set_switch, press_button, set_number
import contextlib
@pytest.mark.timeout(90) # Set timeout for this test
async def test_cms_responses_v16(hass, socket_enabled):
"""Test central system responses to a charger."""
async def test_switches(hass, cs, socket_enabled):
"""Test switch operations."""
for switch in SWITCHES:
await set_switch(hass, cs, switch.key, True)
await asyncio.sleep(1)
await set_switch(hass, cs, switch.key, False)
async def test_buttons(hass, cs, socket_enabled):
"""Test button operations."""
for button in BUTTONS:
await press_button(hass, cs, button.key)
async def test_services(hass, cs, socket_enabled):
"""Test service operations."""
SERVICES = [
csvcs.service_update_firmware,
csvcs.service_configure,
csvcs.service_get_configuration,
csvcs.service_get_diagnostics,
csvcs.service_clear_profile,
csvcs.service_data_transfer,
csvcs.service_set_charge_rate,
]
for service in SERVICES:
data = {}
if service == csvcs.service_update_firmware:
data = {"firmware_url": "http://www.charger.com/firmware.bin"}
if service == csvcs.service_configure:
data = {"ocpp_key": "WebSocketPingInterval", "value": "60"}
if service == csvcs.service_get_configuration:
data = {"ocpp_key": "UnknownKeyTest"}
if service == csvcs.service_get_diagnostics:
data = {"upload_url": "https://webhook.site/abc"}
if service == csvcs.service_data_transfer:
data = {"vendor_id": "ABC"}
if service == csvcs.service_set_charge_rate:
data = {"limit_amps": 30}
await hass.services.async_call(
OCPP_DOMAIN,
service.value,
service_data=data,
blocking=True,
)
# test additional set charge rate options
await hass.services.async_call(
OCPP_DOMAIN,
csvcs.service_set_charge_rate,
service_data={"limit_watts": 3000},
blocking=True,
)
# test custom charge profile for advanced use
prof = {
"chargingProfileId": 8,
"stackLevel": 6,
"chargingProfileKind": "Relative",
"chargingProfilePurpose": "ChargePointMaxProfile",
"chargingSchedule": {
"chargingRateUnit": "A",
"chargingSchedulePeriod": [{"startPeriod": 0, "limit": 16.0}],
},
}
data = {"custom_profile": str(prof)}
await hass.services.async_call(
OCPP_DOMAIN,
csvcs.service_set_charge_rate,
service_data=data,
blocking=True,
)
for number in NUMBERS:
# test setting value of number slider
await set_number(hass, cs, number.key, 10)
# Test MOCK_CONFIG_DATA_2
# Create a mock entry so we don't have to go through config flow
config_entry2 = MockConfigEntry(
domain=OCPP_DOMAIN,
data=MOCK_CONFIG_DATA_2,
entry_id="test_cms2",
title="test_cms2",
)
config_entry2.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry2.entry_id)
await hass.async_block_till_done()
# no subprotocol central system assumes ocpp1.6 charge point
# NB each new config entry will trigger async_update_entry
# if the charger measurands differ from the config entry
# which causes the websocket server to close/restart with a
# ConnectionClosedOK exception, hence it needs to be passed/suppressed
async with websockets.connect(
"ws://127.0.0.1:9002/CP_1_nosub",
) as ws2:
# use a different id for debugging
assert ws2.subprotocol is None
cp2 = ChargePoint("CP_1_no_subprotocol", ws2)
with contextlib.suppress(
asyncio.TimeoutError, websockets.exceptions.ConnectionClosedOK
):
await asyncio.wait_for(
asyncio.gather(
cp2.start(),
cp2.send_boot_notification(),
cp2.send_authorize(),
cp2.send_heartbeat(),
cp2.send_status_notification(),
cp2.send_firmware_status(),
cp2.send_data_transfer(),
cp2.send_start_transaction(),
cp2.send_stop_transaction(),
cp2.send_meter_periodic_data(),
),
timeout=5,
)
await ws2.close()
await asyncio.sleep(1)
if entry := hass.config_entries.async_get_entry(config_entry2.entry_id):
await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
# Create a mock entry so we don't have to go through config flow
config_entry = MockConfigEntry(
domain=OCPP_DOMAIN, data=MOCK_CONFIG_DATA, entry_id="test_cms", title="test_cms"
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
cs = hass.data[OCPP_DOMAIN][config_entry.entry_id]
# unsupported subprotocol raises websockets exception
with pytest.raises(websockets.exceptions.InvalidStatus):
await websockets.connect(
"ws://127.0.0.1:9000/CP_1_unsup",
subprotocols=["ocpp0.0"],
)
# test restore feature of meter_start and active_tranasction_id.
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_res_vals",
subprotocols=["ocpp1.6"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_restore_values", ws)
cp.active_transactionId = None
# send None values
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cp.send_meter_periodic_data(),
),
timeout=3,
)
# check if None
assert cs.get_metric("test_cpid", "Energy.Meter.Start") is None
assert cs.get_metric("test_cpid", "Transaction.Id") is None
# send new data
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.send_start_transaction(12344),
cp.send_meter_periodic_data(),
),
timeout=3,
)
# save for reference the values for meter_start and transaction_id
saved_meter_start = int(cs.get_metric("test_cpid", "Energy.Meter.Start"))
saved_transactionId = int(cs.get_metric("test_cpid", "Transaction.Id"))
# delete current values from api memory
cs.del_metric("test_cpid", "Energy.Meter.Start")
cs.del_metric("test_cpid", "Transaction.Id")
# send new data
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.send_meter_periodic_data(),
),
timeout=3,
)
await ws.close()
# check if restored old values from HA when api have lost the values, i.e. simulated reboot of HA
assert int(cs.get_metric("test_cpid", "Energy.Meter.Start")) == saved_meter_start
assert int(cs.get_metric("test_cpid", "Transaction.Id")) == saved_transactionId
await asyncio.sleep(1)
# test ocpp messages sent from charger to cms
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_norm",
subprotocols=["ocpp1.5", "ocpp1.6"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_normal", ws)
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cp.send_boot_notification(),
cp.send_authorize(),
cp.send_heartbeat(),
cp.send_status_notification(),
cp.send_security_event(),
cp.send_firmware_status(),
cp.send_data_transfer(),
cp.send_start_transaction(12345),
cp.send_meter_err_phases(),
cp.send_meter_line_voltage(),
cp.send_meter_periodic_data(),
# add delay to allow meter data to be processed
cp.send_stop_transaction(1),
),
timeout=5,
)
await ws.close()
assert int(cs.get_metric("test_cpid", "Energy.Active.Import.Register")) == int(
1305570 / 1000
)
assert int(cs.get_metric("test_cpid", "Energy.Session")) == int(
(54321 - 12345) / 1000
)
assert int(cs.get_metric("test_cpid", "Current.Import")) == 0
assert int(cs.get_metric("test_cpid", "Voltage")) == 228
assert cs.get_unit("test_cpid", "Energy.Active.Import.Register") == "kWh"
assert cs.get_metric("unknown_cpid", "Energy.Active.Import.Register") is None
assert cs.get_unit("unknown_cpid", "Energy.Active.Import.Register") is None
assert cs.get_extra_attr("unknown_cpid", "Energy.Active.Import.Register") is None
assert int(cs.get_supported_features("unknown_cpid")) == 0
assert (
await asyncio.wait_for(
cs.set_max_charge_rate_amps("unknown_cpid", 0), timeout=1
)
is False
)
assert (
await asyncio.wait_for(
cs.set_charger_state("unknown_cpid", csvcs.service_clear_profile, False),
timeout=1,
)
is False
)
await asyncio.sleep(1)
# test ocpp messages sent from cms to charger, through HA switches/services
# should reconnect as already started above
# test processing of clock aligned meter data
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_serv",
subprotocols=["ocpp1.6"],
) as ws:
cp = ChargePoint("CP_1_services", ws)
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cs.charge_points[cs.settings.cpid].trigger_boot_notification(),
cs.charge_points[cs.settings.cpid].trigger_status_notification(),
test_switches(hass, cs, socket_enabled),
test_services(hass, cs, socket_enabled),
test_buttons(hass, cs, socket_enabled),
cp.send_meter_clock_data(),
),
timeout=5,
)
await ws.close()
assert int(cs.get_metric("test_cpid", "Frequency")) == 50
assert (
float(cs.get_metric("test_cpid", "Energy.Active.Import.Register")) == 1101.452
)
await asyncio.sleep(1)
# test ocpp messages sent from charger that don't support errata 3.9
# i.e. "Energy.Meter.Start" starts from 0 for each session and "Energy.Active.Import.Register"
# reports starting from 0 Wh for every new transaction id. Total main meter values are without transaction id.
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_non_er_3.9",
subprotocols=["ocpp1.6"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_non_errata_3.9", ws)
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cp.send_start_transaction(0),
cp.send_meter_periodic_data(),
cp.send_main_meter_clock_data(),
# add delay to allow meter data to be processed
cp.send_stop_transaction(1),
),
timeout=5,
)
await ws.close()
# Last sent "Energy.Active.Import.Register" value without transaction id should be here.
assert int(cs.get_metric("test_cpid", "Energy.Active.Import.Register")) == int(
67230012 / 1000
)
assert cs.get_unit("test_cpid", "Energy.Active.Import.Register") == "kWh"
# Last sent "Energy.Active.Import.Register" value with transaction id should be here.
assert int(cs.get_metric("test_cpid", "Energy.Session")) == int(1305570 / 1000)
assert cs.get_unit("test_cpid", "Energy.Session") == "kWh"
await asyncio.sleep(1)
# test ocpp messages sent from charger that don't support errata 3.9 with meter values with kWh as energy unit
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_non_er_3.9",
subprotocols=["ocpp1.6"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_non_errata_3.9", ws)
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cp.send_start_transaction(0),
cp.send_meter_energy_kwh(),
cp.send_meter_clock_data(),
# add delay to allow meter data to be processed
cp.send_stop_transaction(1),
),
timeout=5,
)
await ws.close()
assert int(cs.get_metric("test_cpid", "Energy.Active.Import.Register")) == 1101
assert int(cs.get_metric("test_cpid", "Energy.Session")) == 11
assert cs.get_unit("test_cpid", "Energy.Active.Import.Register") == "kWh"
# test ocpp rejection messages sent from charger to cms
cs.charge_points["test_cpid"].received_boot_notification = False
cs.charge_points["test_cpid"].post_connect_success = False
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1_error",
subprotocols=["ocpp1.6"],
) as ws:
with contextlib.suppress(
asyncio.TimeoutError, websockets.exceptions.ConnectionClosedOK
):
cp = ChargePoint("CP_1_error", ws)
cp.accept = False
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cs.charge_points[cs.settings.cpid].trigger_boot_notification(),
cs.charge_points[cs.settings.cpid].trigger_status_notification(),
test_switches(hass, cs, socket_enabled),
test_services(hass, cs, socket_enabled),
test_buttons(hass, cs, socket_enabled),
),
timeout=3,
)
await ws.close()
await asyncio.sleep(1)
# test services when charger is unavailable
await asyncio.sleep(1)
await test_services(hass, cs, socket_enabled)
if entry := hass.config_entries.async_get_entry(config_entry.entry_id):
await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
class ChargePoint(cpclass):
"""Representation of real client Charge Point."""
def __init__(self, id, connection, response_timeout=30):
"""Init extra variables for testing."""
super().__init__(id, connection)
self.active_transactionId: int = 0
self.accept: bool = True
@on(Action.get_configuration)
def on_get_configuration(self, key, **kwargs):
"""Handle a get configuration requests."""
if key[0] == ConfigurationKey.supported_feature_profiles.value:
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{
"key": key[0],
"readonly": False,
"value": "Core,FirmwareManagement,LocalAuthListManagement,Reservation,SmartCharging,RemoteTrigger,Dummy",
}
]
)
else:
# use to test TypeError handling
return call_result.GetConfiguration(unknown_key=[key[0]])
if key[0] == ConfigurationKey.heartbeat_interval.value:
return call_result.GetConfiguration(
configuration_key=[{"key": key[0], "readonly": False, "value": "300"}]
)
if key[0] == ConfigurationKey.number_of_connectors.value:
return call_result.GetConfiguration(
configuration_key=[{"key": key[0], "readonly": False, "value": "1"}]
)
if key[0] == ConfigurationKey.web_socket_ping_interval.value:
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{"key": key[0], "readonly": False, "value": "60"}
]
)
else:
return call_result.GetConfiguration(unknown_key=[key[0]])
if key[0] == ConfigurationKey.meter_values_sampled_data.value:
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{
"key": key[0],
"readonly": False,
"value": "Energy.Active.Import.Register",
}
]
)
else:
raise Exception
if key[0] == ConfigurationKey.meter_value_sample_interval.value:
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{"key": key[0], "readonly": False, "value": "60"}
]
)
else:
return call_result.GetConfiguration(
configuration_key=[{"key": key[0], "readonly": True, "value": "60"}]
)
if (
key[0]
== ConfigurationKey.charging_schedule_allowed_charging_rate_unit.value
):
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{"key": key[0], "readonly": False, "value": "Current"}
]
)
else:
return call_result.GetConfiguration(unknown_key=[key[0]])
if key[0] == ConfigurationKey.authorize_remote_tx_requests.value:
if self.accept is True:
return call_result.GetConfiguration(
configuration_key=[
{"key": key[0], "readonly": False, "value": "false"}
]
)
else:
return call_result.GetConfiguration(unknown_key=[key[0]])
if key[0] == ConfigurationKey.charge_profile_max_stack_level.value:
return call_result.GetConfiguration(
configuration_key=[{"key": key[0], "readonly": False, "value": "3"}]
)
return call_result.GetConfiguration(
configuration_key=[{"key": key[0], "readonly": False, "value": ""}]
)
@on(Action.change_configuration)
def on_change_configuration(self, key, **kwargs):
"""Handle a get configuration request."""
if self.accept is True:
if key == ConfigurationKey.meter_values_sampled_data.value:
return call_result.ChangeConfiguration(
ConfigurationStatus.reboot_required
)
else:
return call_result.ChangeConfiguration(ConfigurationStatus.accepted)
else:
return call_result.ChangeConfiguration(ConfigurationStatus.rejected)
@on(Action.change_availability)
def on_change_availability(self, **kwargs):
"""Handle change availability request."""
if self.accept is True:
return call_result.ChangeAvailability(AvailabilityStatus.accepted)
else:
return call_result.ChangeAvailability(AvailabilityStatus.rejected)
@on(Action.unlock_connector)
def on_unlock_connector(self, **kwargs):
"""Handle unlock request."""
if self.accept is True:
return call_result.UnlockConnector(UnlockStatus.unlocked)
else:
return call_result.UnlockConnector(UnlockStatus.unlock_failed)
@on(Action.reset)
def on_reset(self, **kwargs):
"""Handle change availability request."""
if self.accept is True:
return call_result.Reset(ResetStatus.accepted)
else:
return call_result.Reset(ResetStatus.rejected)
@on(Action.remote_start_transaction)
def on_remote_start_transaction(self, **kwargs):
"""Handle remote start request."""
if self.accept is True:
self.task = asyncio.create_task(self.send_start_transaction())
return call_result.RemoteStartTransaction(RemoteStartStopStatus.accepted)
else:
return call_result.RemoteStopTransaction(RemoteStartStopStatus.rejected)
@on(Action.remote_stop_transaction)
def on_remote_stop_transaction(self, **kwargs):
"""Handle remote stop request."""
if self.accept is True:
return call_result.RemoteStopTransaction(RemoteStartStopStatus.accepted)
else:
return call_result.RemoteStopTransaction(RemoteStartStopStatus.rejected)
@on(Action.set_charging_profile)
def on_set_charging_profile(self, **kwargs):
"""Handle set charging profile request."""
if self.accept is True:
return call_result.SetChargingProfile(ChargingProfileStatus.accepted)
else:
return call_result.SetChargingProfile(ChargingProfileStatus.rejected)
@on(Action.clear_charging_profile)
def on_clear_charging_profile(self, **kwargs):
"""Handle clear charging profile request."""
if self.accept is True:
return call_result.ClearChargingProfile(ClearChargingProfileStatus.accepted)
else:
return call_result.ClearChargingProfile(ClearChargingProfileStatus.unknown)
@on(Action.trigger_message)
def on_trigger_message(self, **kwargs):
"""Handle trigger message request."""
if self.accept is True:
return call_result.TriggerMessage(TriggerMessageStatus.accepted)
else:
return call_result.TriggerMessage(TriggerMessageStatus.rejected)
@on(Action.update_firmware)
def on_update_firmware(self, **kwargs):
"""Handle update firmware request."""
return call_result.UpdateFirmware()
@on(Action.get_diagnostics)
def on_get_diagnostics(self, **kwargs):
"""Handle get diagnostics request."""
return call_result.GetDiagnostics()
@on(Action.data_transfer)
def on_data_transfer(self, **kwargs):
"""Handle get data transfer request."""
if self.accept is True:
return call_result.DataTransfer(DataTransferStatus.accepted)
else:
return call_result.DataTransfer(DataTransferStatus.rejected)
async def send_boot_notification(self):
"""Send a boot notification."""
request = call.BootNotification(
charge_point_model="Optimus", charge_point_vendor="The Mobility House"
)
resp = await self.call(request)
assert resp.status == RegistrationStatus.accepted
async def send_heartbeat(self):
"""Send a heartbeat."""
request = call.Heartbeat()
resp = await self.call(request)
assert len(resp.current_time) > 0
async def send_authorize(self):
"""Send an authorize request."""
request = call.Authorize(id_tag="test_cp")
resp = await self.call(request)
assert resp.id_tag_info["status"] == AuthorizationStatus.accepted
async def send_firmware_status(self):
"""Send a firmware status notification."""
request = call.FirmwareStatusNotification(status=FirmwareStatus.downloaded)
resp = await self.call(request)
assert resp is not None
async def send_diagnostics_status(self):
"""Send a diagnostics status notification."""
request = call.DiagnosticsStatusNotification(status=DiagnosticsStatus.uploaded)
resp = await self.call(request)
assert resp is not None
async def send_data_transfer(self):
"""Send a data transfer."""
request = call.DataTransfer(
vendor_id="The Mobility House",
message_id="Test123",
data="Test data transfer",
)
resp = await self.call(request)
assert resp.status == DataTransferStatus.accepted
async def send_start_transaction(self, meter_start: int = 12345):
"""Send a start transaction notification."""
request = call.StartTransaction(
connector_id=1,
id_tag="test_cp",
meter_start=meter_start,
timestamp=datetime.now(tz=UTC).isoformat(),
)
resp = await self.call(request)
self.active_transactionId = resp.transaction_id
assert resp.id_tag_info["status"] == AuthorizationStatus.accepted.value
async def send_status_notification(self):
"""Send a status notification."""
request = call.StatusNotification(
connector_id=0,
error_code=ChargePointErrorCode.no_error,
status=ChargePointStatus.suspended_ev,
timestamp=datetime.now(tz=UTC).isoformat(),
info="Test info",
vendor_id="The Mobility House",
vendor_error_code="Test error",
)
resp = await self.call(request)
request = call.StatusNotification(
connector_id=1,
error_code=ChargePointErrorCode.no_error,
status=ChargePointStatus.charging,
timestamp=datetime.now(tz=UTC).isoformat(),
info="Test info",
vendor_id="The Mobility House",
vendor_error_code="Test error",
)
resp = await self.call(request)
request = call.StatusNotification(
connector_id=2,
error_code=ChargePointErrorCode.no_error,
status=ChargePointStatus.available,
timestamp=datetime.now(tz=UTC).isoformat(),
info="Test info",
vendor_id="The Mobility House",
vendor_error_code="Available",
)
resp = await self.call(request)
assert resp is not None
async def send_meter_periodic_data(self):
"""Send periodic meter data notification."""
n = 0
while self.active_transactionId == 0 and n < 2:
await asyncio.sleep(1)
n += 1
request = call.MeterValues(
connector_id=1,
transaction_id=self.active_transactionId,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"value": "1305590.000",
"context": "Sample.Periodic",
"measurand": "Energy.Active.Import.Register",
"location": "Outlet",
"unit": "Wh",
},
{
"value": "20.000",
"context": "Sample.Periodic",
"measurand": "Current.Import",
"location": "Outlet",
"unit": "A",
"phase": "L1",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Current.Import",
"location": "Outlet",
"unit": "A",
"phase": "L2",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Current.Import",
"location": "Outlet",
"unit": "A",
"phase": "L3",
},
{
"value": "16.000",
"context": "Sample.Periodic",
"measurand": "Current.Offered",
"location": "Outlet",
"unit": "A",
},
{
"value": "50.010",
"context": "Sample.Periodic",
"measurand": "Frequency",
"location": "Outlet",
},
{
"value": "",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
"unit": "kW",
},
{
"value": "",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
"unit": "W",
"phase": "L1",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
"unit": "W",
"phase": "L2",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
"unit": "W",
"phase": "L3",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Power.Factor",
"location": "Outlet",
},
{
"value": "38.500",
"context": "Sample.Periodic",
"measurand": "Temperature",
"location": "Body",
"unit": "Celsius",
},
{
"value": "228.000",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L1-N",
},
{
"value": "228.000",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L2-N",
},
{
"value": "0.000",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L3-N",
},
{
"value": "89.00",
"context": "Sample.Periodic",
"measurand": "Power.Reactive.Import",
"unit": "W",
},
{
"value": "0.010",
"context": "Transaction.Begin",
"unit": "kWh",
},
{
"value": "1305570.000",
},
],
}
],
)
resp = await self.call(request)
assert resp is not None
async def send_meter_line_voltage(self):
"""Send line voltages."""
while self.active_transactionId == 0:
await asyncio.sleep(1)
request = call.MeterValues(
connector_id=1,
transaction_id=self.active_transactionId,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"value": "395.900",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L1-L2",
},
{
"value": "396.300",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L2-L3",
},
{
"value": "398.900",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L3-L1",
},
],
}
],
)
resp = await self.call(request)
assert resp is not None
async def send_meter_err_phases(self):
"""Send erroneous voltage phase."""
while self.active_transactionId == 0:
await asyncio.sleep(1)
request = call.MeterValues(
connector_id=1,
transaction_id=self.active_transactionId,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"value": "230",
"context": "Sample.Periodic",
"measurand": "Voltage",
"location": "Outlet",
"unit": "V",
"phase": "L1",
},
{
"value": "23",
"context": "Sample.Periodic",
"measurand": "Current.Import",
"location": "Outlet",
"unit": "A",
"phase": "L1-N",
},
],
}
],
)
resp = await self.call(request)
assert resp is not None
async def send_meter_energy_kwh(self):
"""Send periodic energy meter value with kWh unit."""
while self.active_transactionId == 0:
await asyncio.sleep(1)
request = call.MeterValues(
connector_id=1,
transaction_id=self.active_transactionId,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"unit": "kWh",
"value": "11",
"context": "Sample.Periodic",
"format": "Raw",
"measurand": "Energy.Active.Import.Register",
},
],
}
],
)
resp = await self.call(request)
assert resp is not None
async def send_main_meter_clock_data(self):
"""Send periodic main meter value. Main meter values dont have transaction_id."""
while self.active_transactionId == 0:
await asyncio.sleep(1)
request = call.MeterValues(
connector_id=1,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"value": "67230012",
"context": "Sample.Clock",
"format": "Raw",
"measurand": "Energy.Active.Import.Register",
"location": "Inlet",
},
],
}
],
)
resp = await self.call(request)
assert resp is not None
async def send_meter_clock_data(self):
"""Send periodic meter data notification."""
self.active_transactionId = 0
request = call.MeterValues(
connector_id=1,
transaction_id=self.active_transactionId,
meter_value=[
{
"timestamp": "2021-06-21T16:15:09Z",
"sampledValue": [
{
"measurand": "Voltage",
"context": "Sample.Clock",
"unit": "V",
"value": "228.490",
},
{
"measurand": "Power.Active.Import",
"context": "Sample.Clock",
"unit": "W",
"value": "0.000",
},
{
"measurand": "Energy.Active.Import.Register",
"context": "Sample.Clock",
"unit": "kWh",
"value": "1101.452",
},
{
"measurand": "Current.Import",
"context": "Sample.Clock",
"unit": "A",
"value": "0.054",
},
{