Skip to content

Commit

Permalink
add executor to ssl to avoid blocking (#1435)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
drc38 and lbbrhzn authored Dec 25, 2024
1 parent 1c48807 commit 95b754d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 95b754d

Please sign in to comment.