Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
CONF_MONITORED_VARIABLES,
CONF_PORT,
CONF_SUBPROTOCOL,
CONF_WEBSOCKET_CLOSE_TIMEOUT,
CONF_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT,
DEFAULT_CPID,
DEFAULT_CSID,
DEFAULT_ENERGY_UNIT,
Expand All @@ -66,6 +69,9 @@
DEFAULT_PORT,
DEFAULT_POWER_UNIT,
DEFAULT_SUBPROTOCOL,
DEFAULT_WEBSOCKET_CLOSE_TIMEOUT,
DEFAULT_WEBSOCKET_PING_INTERVAL,
DEFAULT_WEBSOCKET_PING_TIMEOUT,
DOMAIN,
HA_ENERGY_UNIT,
HA_POWER_UNIT,
Expand Down Expand Up @@ -129,6 +135,15 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
self.port = entry.data.get(CONF_PORT, DEFAULT_PORT)
self.csid = entry.data.get(CONF_CSID, DEFAULT_CSID)
self.cpid = entry.data.get(CONF_CPID, DEFAULT_CPID)
self.websocket_close_timeout = entry.data.get(
CONF_WEBSOCKET_CLOSE_TIMEOUT, DEFAULT_WEBSOCKET_CLOSE_TIMEOUT
)
self.websocket_ping_interval = entry.data.get(
CONF_WEBSOCKET_PING_INTERVAL, DEFAULT_WEBSOCKET_PING_INTERVAL
)
self.websocket_ping_timeout = entry.data.get(
CONF_WEBSOCKET_PING_TIMEOUT, DEFAULT_WEBSOCKET_PING_TIMEOUT
)

self.subprotocol = entry.data.get(CONF_SUBPROTOCOL, DEFAULT_SUBPROTOCOL)
self._server = None
Expand All @@ -146,9 +161,9 @@ async def create(hass: HomeAssistant, entry: ConfigEntry):
self.host,
self.port,
subprotocols=[self.subprotocol],
ping_interval=None,
ping_interval=None, # ping interval is not used here, because we send pings mamually in ChargePoint.monitor_connection()
ping_timeout=None,
close_timeout=10,
close_timeout=self.websocket_close_timeout,
)
self._server = server
return self
Expand Down Expand Up @@ -781,21 +796,23 @@ async def _get_specific_response(self, unique_id, timeout):

async def monitor_connection(self):
"""Monitor the connection, by measuring the connection latency."""
timeout = 20
self._metrics[cstat.latency_ping.value].unit = "ms"
self._metrics[cstat.latency_pong.value].unit = "ms"

connection = self._connection
try:
while connection.open:
await asyncio.sleep(timeout)
await asyncio.sleep(self.central.websocket_ping_interval)
time0 = time.perf_counter()
latency_ping = timeout * 1000
pong_waiter = await asyncio.wait_for(connection.ping(), timeout=timeout)
latency_ping = self.central.websocket_ping_timeout * 1000
pong_waiter = await asyncio.wait_for(
connection.ping(), timeout=self.central.websocket_ping_timeout
)
time1 = time.perf_counter()
latency_ping = round(time1 - time0, 3) * 1000
latency_pong = timeout * 1000
await asyncio.wait_for(pong_waiter, timeout=timeout)
latency_pong = self.central.websocket_ping_timeout * 1000
await asyncio.wait_for(
pong_waiter, timeout=self.central.websocket_ping_timeout
)
time2 = time.perf_counter()
latency_pong = round(time2 - time1, 3) * 1000
_LOGGER.debug(
Expand Down
15 changes: 15 additions & 0 deletions custom_components/ocpp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
CONF_METER_INTERVAL,
CONF_MONITORED_VARIABLES,
CONF_PORT,
CONF_WEBSOCKET_CLOSE_TIMEOUT,
CONF_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT,
DEFAULT_CPID,
DEFAULT_CSID,
DEFAULT_HOST,
DEFAULT_IDLE_INTERVAL,
DEFAULT_MEASURAND,
DEFAULT_METER_INTERVAL,
DEFAULT_PORT,
DEFAULT_WEBSOCKET_CLOSE_TIMEOUT,
DEFAULT_WEBSOCKET_PING_INTERVAL,
DEFAULT_WEBSOCKET_PING_TIMEOUT,
DOMAIN,
MEASURANDS,
)
Expand All @@ -29,6 +35,15 @@
vol.Required(CONF_CPID, default=DEFAULT_CPID): str,
vol.Required(CONF_METER_INTERVAL, default=DEFAULT_METER_INTERVAL): int,
vol.Required(CONF_IDLE_INTERVAL, default=DEFAULT_IDLE_INTERVAL): int,
vol.Required(
CONF_WEBSOCKET_CLOSE_TIMEOUT, default=DEFAULT_WEBSOCKET_CLOSE_TIMEOUT
): int,
vol.Required(
CONF_WEBSOCKET_PING_INTERVAL, default=DEFAULT_WEBSOCKET_PING_INTERVAL
): int,
vol.Required(
CONF_WEBSOCKET_PING_TIMEOUT, default=DEFAULT_WEBSOCKET_PING_TIMEOUT
): int,
}
)
STEP_USER_MEASURANDS_SCHEMA = vol.Schema(
Expand Down
6 changes: 6 additions & 0 deletions custom_components/ocpp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
CONF_SUBPROTOCOL = "subprotocol"
CONF_UNIT_OF_MEASUREMENT = ha.CONF_UNIT_OF_MEASUREMENT
CONF_USERNAME = ha.CONF_USERNAME
CONF_WEBSOCKET_CLOSE_TIMEOUT = "websocket_close_timeout"
CONF_WEBSOCKET_PING_INTERVAL = "websocket_ping_interval"
CONF_WEBSOCKET_PING_TIMEOUT = "websocket_ping_timeout"
DATA_UPDATED = "ocpp_data_updated"
DEFAULT_CSID = "central"
DEFAULT_CPID = "charger"
Expand All @@ -28,6 +31,9 @@
DEFAULT_SUBPROTOCOL = "ocpp1.6"
DEFAULT_METER_INTERVAL = 60
DEFAULT_IDLE_INTERVAL = 900
DEFAULT_WEBSOCKET_CLOSE_TIMEOUT = 10
DEFAULT_WEBSOCKET_PING_INTERVAL = 20
DEFAULT_WEBSOCKET_PING_TIMEOUT = 20
DOMAIN = "ocpp"
ICON = "mdi:ev-station"
SLEEP_TIME = 60
Expand Down
5 changes: 4 additions & 1 deletion custom_components/ocpp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"csid": "Central system identity",
"cpid": "Charge point identity",
"meter_interval": "Charging sample interval (seconds)",
"idle_interval": "Charger idle sampling interval (seconds)"
"idle_interval": "Charger idle sampling interval (seconds)",
"websocket_close_timeout": "Websocket close timeout (seconds)",
"websocket_ping_interval": "Websocket ping interval (seconds)",
"websocket_ping_timeout": "Websocket ping timeout (seconds)"
}
},
"measurands": {
Expand Down
5 changes: 4 additions & 1 deletion custom_components/ocpp/translations/i-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"csid": "Central system identity",
"cpid": "Charge point identity",
"meter_interval": "Charging sample interval (seconds)",
"idle_interval": "Charger idle sampling interval (seconds)"
"idle_interval": "Charger idle sampling interval (seconds)",
"websocket_close_timeout": "Websocket close timeout (seconds)",
"websocket_ping_interval": "Websocket ping interval (seconds)",
"websocket_ping_timeout": "Websocket ping timeout (seconds)"
}
},
"measurands": {
Expand Down
7 changes: 5 additions & 2 deletions custom_components/ocpp/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
"step": {
"user": {
"title": "OCPP Configuratie",
"description": "Voor hulp bij configuratie kijk op: https://github.com/lbbrhzn/ocpp",
"description": "Voor hulp bij configuratie zie: https://github.com/lbbrhzn/ocpp",
"data": {
"host": "Host",
"port": "Poort",
"csid": "Central system identifier",
"cpid": "Charge point identifier",
"meter_interval": "Meetinterval (secondes)"
"meter_interval": "Meetinterval (secondes)",
"websocket_close_timeout": "Websocket close timeout (secondes)",
"websocket_ping_interval": "Websocket ping interval (secondes)",
"websocket_ping_timeout": "Websocket ping timeout (secondes)"
}
},
"measurands": {
Expand Down
9 changes: 9 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
CONF_METER_INTERVAL,
CONF_MONITORED_VARIABLES,
CONF_PORT,
CONF_WEBSOCKET_CLOSE_TIMEOUT,
CONF_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT,
)
from ocpp.v16.enums import Measurand

Expand All @@ -17,6 +20,9 @@
CONF_CSID: "test_csid",
CONF_IDLE_INTERVAL: 900,
CONF_METER_INTERVAL: 60,
CONF_WEBSOCKET_CLOSE_TIMEOUT: 1,
CONF_WEBSOCKET_PING_INTERVAL: 1,
CONF_WEBSOCKET_PING_TIMEOUT: 1,
}
MOCK_CONFIG_2 = {
Measurand.current_export.value: True,
Expand Down Expand Up @@ -50,6 +56,9 @@
CONF_IDLE_INTERVAL: 900,
CONF_METER_INTERVAL: 60,
CONF_MONITORED_VARIABLES: "Current.Export,Current.Import,Current.Offered,Energy.Active.Export.Register,Energy.Active.Import.Register,Energy.Reactive.Export.Register,Energy.Reactive.Import.Register,Energy.Active.Export.Interval,Energy.Active.Import.Interval,Energy.Reactive.Export.Interval,Energy.Reactive.Import.Interval,Frequency,Power.Active.Export,Power.Active.Import,Power.Factor,Power.Offered,Power.Reactive.Export,Power.Reactive.Import,RPM,SoC,Temperature,Voltage",
CONF_WEBSOCKET_CLOSE_TIMEOUT: 1,
CONF_WEBSOCKET_PING_INTERVAL: 1,
CONF_WEBSOCKET_PING_TIMEOUT: 1,
}
# separate entry for switch so tests can run concurrently
MOCK_CONFIG_SWITCH = {
Expand Down