9
9
10
10
from custom_components .ocpp import async_setup_entry , async_unload_entry
11
11
from custom_components .ocpp .const import DOMAIN , SWITCH , SWITCHES
12
- from custom_components .ocpp .enums import ConfigurationKey
12
+ from custom_components .ocpp .enums import ConfigurationKey , HAChargerServices as csvcs
13
13
from ocpp .routing import on
14
14
from ocpp .v16 import ChargePoint as cpclass , call , call_result
15
15
from ocpp .v16 .enums import (
@@ -38,7 +38,6 @@ async def test_cms_responses(hass):
38
38
39
39
async def test_switches (hass ):
40
40
"""Test switch operations."""
41
-
42
41
for switch in SWITCHES :
43
42
result = await hass .services .async_call (
44
43
SWITCH ,
@@ -60,6 +59,31 @@ async def test_switches(hass):
60
59
)
61
60
assert result
62
61
62
+ async def test_services (hass ):
63
+ """Test service operations."""
64
+ SERVICES = [
65
+ csvcs .service_update_firmware ,
66
+ csvcs .service_configure ,
67
+ csvcs .service_get_configuration ,
68
+ csvcs .service_clear_profile ,
69
+ csvcs .service_set_charge_rate ,
70
+ ]
71
+ for service in SERVICES :
72
+ data = {}
73
+ if service == csvcs .service_update_firmware :
74
+ data = {"firmware_url" : "http://www.charger.com/firmware.bin" }
75
+ if service == csvcs .service_configure :
76
+ data = {"ocpp_key" : "WebSocketPingInterval" , "value" : "60" }
77
+ if service == csvcs .service_get_configuration :
78
+ data = {"ocpp_key" : "WebSocketPingInterval" }
79
+ result = await hass .services .async_call (
80
+ DOMAIN ,
81
+ service .value ,
82
+ service_data = data ,
83
+ blocking = True ,
84
+ )
85
+ assert result
86
+
63
87
# Create a mock entry so we don't have to go through config flow
64
88
config_entry = MockConfigEntry (
65
89
domain = DOMAIN , data = MOCK_CONFIG_DATA , entry_id = "test_cms"
@@ -69,6 +93,7 @@ async def test_switches(hass):
69
93
70
94
cs = hass .data [DOMAIN ][config_entry .entry_id ]
71
95
96
+ # test ocpp messages sent from charger to cms
72
97
async with websockets .connect (
73
98
"ws://localhost:9000/CP_1" ,
74
99
subprotocols = ["ocpp1.6" ],
@@ -85,27 +110,36 @@ async def test_switches(hass):
85
110
cp .send_status_notification (),
86
111
cp .send_firmware_status (),
87
112
cp .send_data_transfer (),
88
- cp .send_start_transaction (),
89
113
cp .send_meter_data (),
114
+ cp .send_start_transaction (),
90
115
cp .send_stop_transaction (),
91
- cs .charge_points ["test_cpid" ].start_transaction (),
92
- cs .charge_points ["test_cpid" ].reset (),
93
- cs .charge_points ["test_cpid" ].set_charge_rate (),
94
- cs .charge_points ["test_cpid" ].clear_profile (),
95
- cs .charge_points ["test_cpid" ].update_firmware (
96
- "http://www.charger.com/file.bin"
97
- ),
98
- cs .charge_points ["test_cpid" ].unlock (),
116
+ ),
117
+ timeout = 4 ,
118
+ )
119
+ except asyncio .TimeoutError :
120
+ pass
121
+ assert int (cs .get_metric ("test_cpid" , "Energy.Active.Import.Register" )) == int (
122
+ 1305570 / 1000
123
+ )
124
+ assert cs .get_unit ("test_cpid" , "Energy.Active.Import.Register" ) == "kWh"
125
+
126
+ # test ocpp messages sent from cms to charger, through HA switches/services
127
+ async with websockets .connect (
128
+ "ws://localhost:9000/CP_1" ,
129
+ subprotocols = ["ocpp1.6" ],
130
+ ) as ws :
131
+ cp = ChargePoint ("CP_1_test" , ws )
132
+ try :
133
+ await asyncio .wait_for (
134
+ asyncio .gather (
135
+ cp .start (),
99
136
test_switches (hass ),
137
+ test_services (hass ),
100
138
),
101
- timeout = 5 ,
139
+ timeout = 4 ,
102
140
)
103
141
except asyncio .TimeoutError :
104
142
pass
105
- assert int (cs .get_metric ("test_cpid" , "Energy.Active.Import.Register" )) == int (
106
- 1305570 / 1000
107
- )
108
- assert cs .get_unit ("test_cpid" , "Energy.Active.Import.Register" ) == "kWh"
109
143
await async_unload_entry (hass , config_entry )
110
144
await hass .async_block_till_done ()
111
145
@@ -282,6 +316,16 @@ async def send_start_transaction(self):
282
316
283
317
async def send_status_notification (self ):
284
318
"""Send a status notification."""
319
+ request = call .StatusNotificationPayload (
320
+ connector_id = 1 ,
321
+ error_code = ChargePointErrorCode .no_error ,
322
+ status = ChargePointStatus .suspended_ev ,
323
+ timestamp = datetime .now (tz = timezone .utc ).isoformat (),
324
+ info = "Test info" ,
325
+ vendor_id = "The Mobility House" ,
326
+ vendor_error_code = "Test error" ,
327
+ )
328
+ resp = await self .call (request )
285
329
request = call .StatusNotificationPayload (
286
330
connector_id = 1 ,
287
331
error_code = ChargePointErrorCode .no_error ,
@@ -352,7 +396,7 @@ async def send_meter_data(self):
352
396
"context" : "Sample.Periodic" ,
353
397
"measurand" : "Power.Active.Import" ,
354
398
"location" : "Outlet" ,
355
- "unit" : "W " ,
399
+ "unit" : "kW " ,
356
400
},
357
401
{
358
402
"value" : "0.000" ,
@@ -450,6 +494,9 @@ async def send_meter_data(self):
450
494
"context" : "Transaction.Begin" ,
451
495
"unit" : "kWh" ,
452
496
},
497
+ {
498
+ "value" : "1305570.000" ,
499
+ },
453
500
],
454
501
}
455
502
],
0 commit comments