Skip to content

Commit

Permalink
Close old connection before switching to new (#336)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
lbbrhzn authored Jan 31, 2022
1 parent 6df898e commit 77c390b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 5 additions & 0 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ocpp/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 11 additions & 13 deletions custom_components/ocpp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77c390b

Please sign in to comment.