Skip to content

Commit 2cbcfb5

Browse files
authored
Improve post_connect logic (#392)
* Improve post_connect logic If post_connect always triggers a charger issue eg disconnect this may cause continuous reconnects * Move triggers to end of post_connect * Add debug message * add test for noot notification * fix linting * extra test * fix tests * try again * update cpid * update cpid to mock config * revert * test change in api * Revert "test change in api" This reverts commit e240bff.
1 parent b6ad57f commit 2cbcfb5

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

custom_components/ocpp/api.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def __init__(
323323
self.active_transaction_id: int = 0
324324
self.triggered_boot_notification = False
325325
self.received_boot_notification = False
326+
self.post_connect_success = False
326327
self.tasks = None
327328
self._metrics = defaultdict(lambda: Metric(None, None))
328329
self._metrics[cdet.identifier.value].value = id
@@ -392,11 +393,7 @@ async def handle_data_transfer(call):
392393
await self.get_supported_features()
393394
resp = await self.get_configuration(ckey.number_of_connectors.value)
394395
self._metrics[cdet.connectors.value].value = resp
395-
if prof.REM in self._attr_supported_features:
396-
if self.received_boot_notification is False:
397-
await self.trigger_boot_notification()
398-
await self.trigger_status_notification()
399-
await self.become_operative()
396+
await self.set_availability()
400397
await self.get_configuration(ckey.heartbeat_interval.value)
401398
await self.configure(ckey.web_socket_ping_interval.value, "60")
402399
await self.configure(
@@ -452,6 +449,15 @@ async def handle_data_transfer(call):
452449
handle_get_diagnostics,
453450
GDIAG_SERVICE_DATA_SCHEMA,
454451
)
452+
self.post_connect_success = True
453+
_LOGGER.debug(f"'{self.id}' post connection setup completed successfully")
454+
455+
# nice to have, but not needed for integration to function
456+
# and can cause issues with some chargers
457+
if prof.REM in self._attr_supported_features:
458+
if self.received_boot_notification is False:
459+
await self.trigger_boot_notification()
460+
await self.trigger_status_notification()
455461
except (NotImplementedError) as e:
456462
_LOGGER.error("Configuration of the charger failed: %s", e)
457463

@@ -505,11 +511,6 @@ async def trigger_status_notification(self):
505511
return_value = False
506512
return return_value
507513

508-
async def become_operative(self):
509-
"""Become operative."""
510-
resp = await self.set_availability()
511-
return resp
512-
513514
async def clear_profile(self):
514515
"""Clear all charging profiles."""
515516
req = call.ClearChargingProfilePayload()
@@ -906,7 +907,12 @@ async def reconnect(self, connection: websockets.server.WebSocketServerProtocol)
906907
self.status = STATE_OK
907908
self._connection = connection
908909
self._metrics[cstat.reconnects.value].value += 1
909-
await self.run([super().start(), self.monitor_connection()])
910+
if self.post_connect_success is True:
911+
await self.run([super().start(), self.monitor_connection()])
912+
else:
913+
await self.run(
914+
[super().start(), self.post_connect(), self.monitor_connection()]
915+
)
910916

911917
async def async_update_device_info(self, boot_info: dict):
912918
"""Update device info asynchronuously."""

tests/test_charge_point.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ async def test_services(hass, socket_enabled):
268268
# use same id to ensure metrics populated
269269
cp = ChargePoint("CP_1_test", ws)
270270
cp.accept = False
271+
cs.charge_points[cs.cpid].received_boot_notification = False
272+
cs.charge_points[cs.cpid].post_connect_success = False
271273
try:
272274
await asyncio.wait_for(
273275
asyncio.gather(

0 commit comments

Comments
 (0)