Skip to content

Commit

Permalink
Allow custom httpx client session in connection initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-th committed Feb 23, 2025
1 parent ae70998 commit 6bf19e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/homematicip/async_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ async def init_async(self, access_point_id, auth_token: str | None = None, looku
auth_token=auth_token)
self._connection = ConnectionFactory.create_connection(self._connection_context, use_rate_limiting)

def init_with_context(self, context: ConnectionContext, use_rate_limiting=True):
def init_with_context(self, context: ConnectionContext, use_rate_limiting=True, httpx_client_session = None):
self._connection_context = context
self._connection = ConnectionFactory.create_connection(self._connection_context, use_rate_limiting)
self._connection = ConnectionFactory.create_connection(self._connection_context, use_rate_limiting, httpx_client_session)

def set_auth_token(self, auth_token):
"""Sets the auth token for the connection. This is only necessary, if not already set in init function"""
Expand Down
6 changes: 3 additions & 3 deletions src/homematicip/connection/connection_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ConnectionFactory:
"""factory class for creating connections"""

@staticmethod
def create_connection(context: ConnectionContext, use_rate_limited_connection: bool = True) -> RestConnection:
def create_connection(context: ConnectionContext, use_rate_limited_connection: bool = True, httpx_client_session = None) -> RestConnection:
"""creates a connection object with the given context"""
if use_rate_limited_connection:
return RateLimitedRestConnection(context)
return RestConnection(context)
return RateLimitedRestConnection(context, httpx_client_session=httpx_client_session)
return RestConnection(context, httpx_client_session=httpx_client_session)
7 changes: 5 additions & 2 deletions tests/test_home.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta
from unittest.mock import Mock, patch, AsyncMock

import httpx
import pytest

from conftest import utc_offset
Expand All @@ -27,11 +28,13 @@ def test_init():
assert home._connection is not None


def test_init_with_context(fake_connection_context_with_ssl):
def test_init_with_context(mocker, fake_connection_context_with_ssl):
httpx_client_session = mocker.Mock(spec=httpx.AsyncClient)
home = Home()
home.init_with_context(fake_connection_context_with_ssl)
home.init_with_context(fake_connection_context_with_ssl, httpx_client_session=httpx_client_session)
assert home._connection_context == fake_connection_context_with_ssl
assert home._connection is not None
assert home._connection._httpx_client_session is not None


def test_update_event(fake_home: Home):
Expand Down

0 comments on commit 6bf19e4

Please sign in to comment.