Skip to content

Commit

Permalink
fix: Fix blocking SSL calls (#43)
Browse files Browse the repository at this point in the history
* Add ability to pass an SSL Context.

* Change the TLS method calls to pass a context

* Fixed a bug not passing the tls version.

* Revert version number change.
  • Loading branch information
dcmeglio authored Oct 8, 2024
1 parent 37b7f28 commit e675d0a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/pyeconet/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@

ApiType = TypeVar("ApiType", bound="EcoNetApiInterface")

def _create_ssl_context() -> ssl.SSLContext:
"""Create a SSL context for the MQTT connection."""
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.load_default_certs()
return context

_SSL_CONTEXT = _create_ssl_context()


class EcoNetApiInterface:
"""
Expand Down Expand Up @@ -97,14 +105,10 @@ def subscribe(self):
self._user_token, password=CLEAR_BLADE_SYSTEM_KEY
)
self._mqtt_client.enable_logger()
self._mqtt_client.tls_set(
ca_certs=None,
certfile=None,
keyfile=None,
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLS,
ciphers=None,
)

self._mqtt_client.tls_set_context(_SSL_CONTEXT)
self._mqtt_client.tls_insecure_set(False)

self._mqtt_client.on_connect = self._on_connect
self._mqtt_client.on_message = self._on_message
self._mqtt_client.on_disconnect = self._on_disconnect
Expand Down

0 comments on commit e675d0a

Please sign in to comment.