Skip to content

Commit

Permalink
Fix blocking IO issue by using SSL context provided by HA, #116
Browse files Browse the repository at this point in the history
* Added new optional `ssl_context` argument to `Installation.stream()`
  • Loading branch information
sveinse committed Nov 24, 2024
1 parent 932c961 commit 236f253
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion custom_components/zaptec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.util.ssl import get_default_context

from .api import Account, Charger, Circuit, Installation, ZaptecApiError, ZaptecBase
from .const import (
Expand Down Expand Up @@ -255,7 +256,8 @@ async def _async_update_data(self) -> None:
# Setup the stream subscription
for install in self.account.installations:
if install.id in self.account.map:
await install.stream(cb=self._stream_update)
await install.stream(cb=self._stream_update,
ssl_context=get_default_context())

# Fetch updates
await self.account.update_states()
Expand Down
14 changes: 8 additions & 6 deletions custom_components/zaptec/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,24 @@ async def live_stream_connection_details(self):
self.connection_details = data
return data

async def stream(self, cb=None) -> asyncio.Task:
async def stream(self, cb=None, ssl_context=None) -> asyncio.Task|None:
"""Kickoff the steam in the background."""
try:
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus.exceptions import ServiceBusError
except ImportError:
_LOGGER.debug("Azure Service bus is not available. Resolving to polling")
return
return None

await self.cancel_stream()
self._stream_task = asyncio.create_task(self._stream(cb=cb))
self._stream_task = asyncio.create_task(self._stream(cb=cb, ssl_context=ssl_context))
return self._stream_task

async def _stream(self, cb=None):
async def _stream(self, cb=None, ssl_context=None):
"""Main stream handler"""
try:
try:
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus.exceptions import ServiceBusError
except ImportError:
_LOGGER.warning(
"Azure Service bus is not available. Resolving to polling"
Expand All @@ -316,7 +315,10 @@ async def _stream(self, cb=None):
f'SharedAccessKeyName={conf["Username"]};'
f'SharedAccessKey={conf["Password"]}'
)
servicebus_client = ServiceBusClient.from_connection_string(conn_str=constr)
kw = {}
if ssl_context:
kw["ssl_context"] = ssl_context
servicebus_client = ServiceBusClient.from_connection_string(conn_str=constr, **kw)
_LOGGER.debug("Connecting to servicebus using %s", constr)

self._stream_receiver = None
Expand Down
2 changes: 1 addition & 1 deletion custom_components/zaptec/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"sveinse"
],
"iot_class": "cloud_polling",
"requirements": ["azure-servicebus", "pydantic"],
"requirements": ["azure-servicebus==7.13.0", "pydantic"],
"version": "0.7.3b1"
}

0 comments on commit 236f253

Please sign in to comment.