From 77c390b64b4b8e0ff349a26acfdc7afe1efcadf4 Mon Sep 17 00:00:00 2001 From: lbbrhzn Date: Mon, 31 Jan 2022 09:23:59 +0100 Subject: [PATCH] Close old connection before switching to new (#336) * monitor connection, close on timeout * solve incorrect merge * improve reconnect * fix type in latency_pong enum value * iselect measurement class for latency_ping and latency_pong * trigger boot notification when supported Co-authored-by: lbbrhzn <@lbbrhzn> --- custom_components/ocpp/api.py | 5 +++++ custom_components/ocpp/enums.py | 2 +- custom_components/ocpp/sensor.py | 24 +++++++++++------------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/custom_components/ocpp/api.py b/custom_components/ocpp/api.py index 9af0cd66..47ecfcba 100644 --- a/custom_components/ocpp/api.py +++ b/custom_components/ocpp/api.py @@ -367,6 +367,7 @@ async def handle_data_transfer(call): await asyncio.sleep(2) await self.get_supported_features() if prof.REM in self._attr_supported_features: + await self.trigger_boot_notification() await self.trigger_status_notification() await self.become_operative() await self.get_configuration(ckey.heartbeat_interval.value) @@ -839,6 +840,10 @@ async def start(self): async def reconnect(self, connection: websockets.connection): """Reconnect charge point.""" + # close old connection, if needed + if self._connection is not None: + await self._connection.close() + # use the new connection self._connection = connection self._metrics[cstat.reconnects.value].value += 1 try: diff --git a/custom_components/ocpp/enums.py b/custom_components/ocpp/enums.py index 3a9a207b..e850a9c6 100644 --- a/custom_components/ocpp/enums.py +++ b/custom_components/ocpp/enums.py @@ -26,7 +26,7 @@ class HAChargerStatuses(str, Enum): status = "Status" heartbeat = "Heartbeat" latency_ping = "Latency.Ping" - latency_pong = "Latency.Ping" + latency_pong = "Latency.Pong" error_code = "Error.Code" stop_reason = "Stop.Reason" firmware_status = "Status.Firmware" diff --git a/custom_components/ocpp/sensor.py b/custom_components/ocpp/sensor.py index 7461dca9..d64fd3db 100644 --- a/custom_components/ocpp/sensor.py +++ b/custom_components/ocpp/sensor.py @@ -117,19 +117,17 @@ def state_class(self): state_class = None if self.device_class is SensorDeviceClass.ENERGY: state_class = SensorStateClass.TOTAL_INCREASING - elif ( - self.device_class - in [ - SensorDeviceClass.CURRENT, - SensorDeviceClass.VOLTAGE, - SensorDeviceClass.POWER, - SensorDeviceClass.TEMPERATURE, - SensorDeviceClass.BATTERY, - SensorDeviceClass.FREQUENCY, - ] - or self.metric == HAChargerStatuses.latency_ping - or self.metric == HAChargerStatuses.latency_pong - ): + elif self.device_class in [ + SensorDeviceClass.CURRENT, + SensorDeviceClass.VOLTAGE, + SensorDeviceClass.POWER, + SensorDeviceClass.TEMPERATURE, + SensorDeviceClass.BATTERY, + SensorDeviceClass.FREQUENCY, + ] or self.metric in [ + HAChargerStatuses.latency_ping.value, + HAChargerStatuses.latency_pong.value, + ]: state_class = SensorStateClass.MEASUREMENT return state_class