From 95b754d6dcfbc75283e99bab8a056f027f480ed3 Mon Sep 17 00:00:00 2001 From: drc38 <20024196+drc38@users.noreply.github.com> Date: Thu, 26 Dec 2024 05:47:02 +1300 Subject: [PATCH] add executor to ssl to avoid blocking (#1435) * add executor to ssl avoid blocking * additional funcs to handle await in init * add self to entry * add await to central system instance * add yield * return self from awaitable * move ssl to create method * Update websockets serve --------- Co-authored-by: lbbrhzn <8673442+lbbrhzn@users.noreply.github.com> --- custom_components/ocpp/api.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/custom_components/ocpp/api.py b/custom_components/ocpp/api.py index f1f058cc..eeefba2f 100644 --- a/custom_components/ocpp/api.py +++ b/custom_components/ocpp/api.py @@ -5,6 +5,7 @@ import logging import ssl +from functools import partial from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_OK from homeassistant.core import HomeAssistant @@ -90,26 +91,31 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry): self.config = entry.data self.id = entry.entry_id self.charge_points = {} - if entry.data.get(CONF_SSL, DEFAULT_SSL): + + @staticmethod + async def create(hass: HomeAssistant, entry: ConfigEntry): + """Create instance and start listening for OCPP connections on given port.""" + self = CentralSystem(hass, entry) + + if self.entry.data.get(CONF_SSL, DEFAULT_SSL): self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # see https://community.home-assistant.io/t/certificate-authority-and-self-signed-certificate-for-ssl-tls/196970 - localhost_certfile = entry.data.get( + localhost_certfile = self.entry.data.get( CONF_SSL_CERTFILE_PATH, DEFAULT_SSL_CERTFILE_PATH ) - localhost_keyfile = entry.data.get( + localhost_keyfile = self.entry.data.get( CONF_SSL_KEYFILE_PATH, DEFAULT_SSL_KEYFILE_PATH ) - self.ssl_context.load_cert_chain( - localhost_certfile, keyfile=localhost_keyfile + await self.hass.async_add_executor_job( + partial( + self.ssl_context.load_cert_chain, + localhost_certfile, + keyfile=localhost_keyfile, + ) ) else: self.ssl_context = None - @staticmethod - async def create(hass: HomeAssistant, entry: ConfigEntry): - """Create instance and start listening for OCPP connections on given port.""" - self = CentralSystem(hass, entry) - server = await websockets.serve( self.on_connect, self.host,