File tree 5 files changed +28
-4
lines changed
5 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,17 @@ def get_async_client(
39
39
return client
40
40
41
41
42
+ class HassHttpXAsyncClient (httpx .AsyncClient ):
43
+ """httpx AsyncClient that suppresses context management."""
44
+
45
+ async def __aenter__ (self : HassHttpXAsyncClient ) -> HassHttpXAsyncClient :
46
+ """Prevent an integration from reopen of the client via context manager."""
47
+ return self
48
+
49
+ async def __aexit__ (self , * args : Any ) -> None : # pylint: disable=signature-differs
50
+ """Prevent an integration from close of the client via context manager."""
51
+
52
+
42
53
@callback
43
54
def create_async_httpx_client (
44
55
hass : HomeAssistantType ,
@@ -53,7 +64,7 @@ def create_async_httpx_client(
53
64
54
65
This method must be run in the event loop.
55
66
"""
56
- client = httpx . AsyncClient (
67
+ client = HassHttpXAsyncClient (
57
68
verify = verify_ssl ,
58
69
headers = {USER_AGENT : SERVER_SOFTWARE },
59
70
** kwargs ,
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ distro==1.5.0
16
16
emoji==1.2.0
17
17
hass-nabucasa==0.42.0
18
18
home-assistant-frontend==20210324.0
19
- httpx==0.16 .1
19
+ httpx==0.17 .1
20
20
jinja2>=2.11.3
21
21
netdisco==2.8.2
22
22
paho-mqtt==1.5.1
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ awesomeversion==21.2.3
9
9
bcrypt == 3.1.7
10
10
certifi >= 2020.12.5
11
11
ciso8601 == 2.1.3
12
- httpx == 0.16 .1
12
+ httpx == 0.17 .1
13
13
jinja2 >= 2.11.3
14
14
PyJWT == 1.7.1
15
15
cryptography == 3.3.2
Original file line number Diff line number Diff line change 40
40
"bcrypt==3.1.7" ,
41
41
"certifi>=2020.12.5" ,
42
42
"ciso8601==2.1.3" ,
43
- "httpx==0.16 .1" ,
43
+ "httpx==0.17 .1" ,
44
44
"jinja2>=2.11.3" ,
45
45
"PyJWT==1.7.1" ,
46
46
# PyJWT has loose dependency. We want the latest one.
Original file line number Diff line number Diff line change @@ -80,6 +80,19 @@ async def test_get_async_client_patched_close(hass):
80
80
assert mock_aclose .call_count == 0
81
81
82
82
83
+ async def test_get_async_client_context_manager (hass ):
84
+ """Test using the async client with a context manager does not close the session."""
85
+
86
+ with patch ("httpx.AsyncClient.aclose" ) as mock_aclose :
87
+ httpx_session = client .get_async_client (hass )
88
+ assert isinstance (hass .data [client .DATA_ASYNC_CLIENT ], httpx .AsyncClient )
89
+
90
+ async with httpx_session :
91
+ pass
92
+
93
+ assert mock_aclose .call_count == 0
94
+
95
+
83
96
async def test_warning_close_session_integration (hass , caplog ):
84
97
"""Test log warning message when closing the session from integration context."""
85
98
with patch (
You can’t perform that action at this time.
0 commit comments