mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Pass timeout to httpx in RESTful Switch (#105364)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
f7a5b14cd6
commit
c5a3e58994
@ -202,22 +202,22 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
||||
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
||||
rendered_params = template.render_complex(self._params)
|
||||
|
||||
async with asyncio.timeout(self._timeout):
|
||||
req: httpx.Response = await getattr(websession, self._method)(
|
||||
self._resource,
|
||||
auth=self._auth,
|
||||
content=bytes(body, "utf-8"),
|
||||
headers=rendered_headers,
|
||||
params=rendered_params,
|
||||
)
|
||||
return req
|
||||
req: httpx.Response = await getattr(websession, self._method)(
|
||||
self._resource,
|
||||
auth=self._auth,
|
||||
content=bytes(body, "utf-8"),
|
||||
headers=rendered_headers,
|
||||
params=rendered_params,
|
||||
timeout=self._timeout,
|
||||
)
|
||||
return req
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Get the current state, catching errors."""
|
||||
req = None
|
||||
try:
|
||||
req = await self.get_device_state(self.hass)
|
||||
except asyncio.TimeoutError:
|
||||
except (asyncio.TimeoutError, httpx.TimeoutException):
|
||||
_LOGGER.exception("Timed out while fetching data")
|
||||
except httpx.RequestError as err:
|
||||
_LOGGER.exception("Error while fetching data: %s", err)
|
||||
@ -233,14 +233,14 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
||||
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
||||
rendered_params = template.render_complex(self._params)
|
||||
|
||||
async with asyncio.timeout(self._timeout):
|
||||
req = await websession.get(
|
||||
self._state_resource,
|
||||
auth=self._auth,
|
||||
headers=rendered_headers,
|
||||
params=rendered_params,
|
||||
)
|
||||
text = req.text
|
||||
req = await websession.get(
|
||||
self._state_resource,
|
||||
auth=self._auth,
|
||||
headers=rendered_headers,
|
||||
params=rendered_params,
|
||||
timeout=self._timeout,
|
||||
)
|
||||
text = req.text
|
||||
|
||||
if self._is_on_template is not None:
|
||||
text = self._is_on_template.async_render_with_possible_json_value(
|
||||
|
@ -1,5 +1,4 @@
|
||||
"""The tests for the REST switch platform."""
|
||||
import asyncio
|
||||
from http import HTTPStatus
|
||||
|
||||
import httpx
|
||||
@ -103,7 +102,7 @@ async def test_setup_failed_connect(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test setup when connection error occurs."""
|
||||
respx.get(RESOURCE).mock(side_effect=asyncio.TimeoutError())
|
||||
respx.get(RESOURCE).mock(side_effect=httpx.ConnectError(""))
|
||||
config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
|
||||
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
@ -117,7 +116,7 @@ async def test_setup_timeout(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test setup when connection timeout occurs."""
|
||||
respx.get(RESOURCE).mock(side_effect=asyncio.TimeoutError())
|
||||
respx.get(RESOURCE).mock(side_effect=httpx.TimeoutException(""))
|
||||
config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
|
||||
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
@ -326,7 +325,7 @@ async def test_turn_on_timeout(hass: HomeAssistant) -> None:
|
||||
"""Test turn_on when timeout occurs."""
|
||||
await _async_setup_test_switch(hass)
|
||||
|
||||
respx.post(RESOURCE) % HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
respx.post(RESOURCE).mock(side_effect=httpx.TimeoutException(""))
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
@ -389,7 +388,7 @@ async def test_turn_off_timeout(hass: HomeAssistant) -> None:
|
||||
"""Test turn_off when timeout occurs."""
|
||||
await _async_setup_test_switch(hass)
|
||||
|
||||
respx.post(RESOURCE).mock(side_effect=asyncio.TimeoutError())
|
||||
respx.post(RESOURCE).mock(side_effect=httpx.TimeoutException(""))
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
@ -442,7 +441,7 @@ async def test_update_timeout(hass: HomeAssistant) -> None:
|
||||
"""Test update when timeout occurs."""
|
||||
await _async_setup_test_switch(hass)
|
||||
|
||||
respx.get(RESOURCE).mock(side_effect=asyncio.TimeoutError())
|
||||
respx.get(RESOURCE).mock(side_effect=httpx.TimeoutException(""))
|
||||
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user