Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking IO issue by using SSL context provided by HA, #116 #149

Merged
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
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"
}