diff --git a/custom_components/zaptec/__init__.py b/custom_components/zaptec/__init__.py index 4110e13..2d17eb6 100644 --- a/custom_components/zaptec/__init__.py +++ b/custom_components/zaptec/__init__.py @@ -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 ( @@ -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() diff --git a/custom_components/zaptec/api.py b/custom_components/zaptec/api.py index c935091..0cfd5cf 100644 --- a/custom_components/zaptec/api.py +++ b/custom_components/zaptec/api.py @@ -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" @@ -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 diff --git a/custom_components/zaptec/manifest.json b/custom_components/zaptec/manifest.json index ff6d5cc..d56c419 100644 --- a/custom_components/zaptec/manifest.json +++ b/custom_components/zaptec/manifest.json @@ -9,6 +9,6 @@ "sveinse" ], "iot_class": "cloud_polling", - "requirements": ["azure-servicebus", "pydantic"], + "requirements": ["azure-servicebus==7.13.0", "pydantic"], "version": "0.7.3b1" }