mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +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,13 +202,13 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||||||
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
||||||
rendered_params = template.render_complex(self._params)
|
rendered_params = template.render_complex(self._params)
|
||||||
|
|
||||||
async with asyncio.timeout(self._timeout):
|
|
||||||
req: httpx.Response = await getattr(websession, self._method)(
|
req: httpx.Response = await getattr(websession, self._method)(
|
||||||
self._resource,
|
self._resource,
|
||||||
auth=self._auth,
|
auth=self._auth,
|
||||||
content=bytes(body, "utf-8"),
|
content=bytes(body, "utf-8"),
|
||||||
headers=rendered_headers,
|
headers=rendered_headers,
|
||||||
params=rendered_params,
|
params=rendered_params,
|
||||||
|
timeout=self._timeout,
|
||||||
)
|
)
|
||||||
return req
|
return req
|
||||||
|
|
||||||
@ -217,7 +217,7 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||||||
req = None
|
req = None
|
||||||
try:
|
try:
|
||||||
req = await self.get_device_state(self.hass)
|
req = await self.get_device_state(self.hass)
|
||||||
except asyncio.TimeoutError:
|
except (asyncio.TimeoutError, httpx.TimeoutException):
|
||||||
_LOGGER.exception("Timed out while fetching data")
|
_LOGGER.exception("Timed out while fetching data")
|
||||||
except httpx.RequestError as err:
|
except httpx.RequestError as err:
|
||||||
_LOGGER.exception("Error while fetching data: %s", err)
|
_LOGGER.exception("Error while fetching data: %s", err)
|
||||||
@ -233,12 +233,12 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||||||
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
rendered_headers = template.render_complex(self._headers, parse_result=False)
|
||||||
rendered_params = template.render_complex(self._params)
|
rendered_params = template.render_complex(self._params)
|
||||||
|
|
||||||
async with asyncio.timeout(self._timeout):
|
|
||||||
req = await websession.get(
|
req = await websession.get(
|
||||||
self._state_resource,
|
self._state_resource,
|
||||||
auth=self._auth,
|
auth=self._auth,
|
||||||
headers=rendered_headers,
|
headers=rendered_headers,
|
||||||
params=rendered_params,
|
params=rendered_params,
|
||||||
|
timeout=self._timeout,
|
||||||
)
|
)
|
||||||
text = req.text
|
text = req.text
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""The tests for the REST switch platform."""
|
"""The tests for the REST switch platform."""
|
||||||
import asyncio
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@ -103,7 +102,7 @@ async def test_setup_failed_connect(
|
|||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test setup when connection error occurs."""
|
"""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}}
|
config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
|
||||||
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -117,7 +116,7 @@ async def test_setup_timeout(
|
|||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test setup when connection timeout occurs."""
|
"""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}}
|
config = {SWITCH_DOMAIN: {CONF_PLATFORM: DOMAIN, CONF_RESOURCE: RESOURCE}}
|
||||||
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
|
||||||
await hass.async_block_till_done()
|
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."""
|
"""Test turn_on when timeout occurs."""
|
||||||
await _async_setup_test_switch(hass)
|
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(
|
await hass.services.async_call(
|
||||||
SWITCH_DOMAIN,
|
SWITCH_DOMAIN,
|
||||||
SERVICE_TURN_ON,
|
SERVICE_TURN_ON,
|
||||||
@ -389,7 +388,7 @@ async def test_turn_off_timeout(hass: HomeAssistant) -> None:
|
|||||||
"""Test turn_off when timeout occurs."""
|
"""Test turn_off when timeout occurs."""
|
||||||
await _async_setup_test_switch(hass)
|
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(
|
await hass.services.async_call(
|
||||||
SWITCH_DOMAIN,
|
SWITCH_DOMAIN,
|
||||||
SERVICE_TURN_OFF,
|
SERVICE_TURN_OFF,
|
||||||
@ -442,7 +441,7 @@ async def test_update_timeout(hass: HomeAssistant) -> None:
|
|||||||
"""Test update when timeout occurs."""
|
"""Test update when timeout occurs."""
|
||||||
await _async_setup_test_switch(hass)
|
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)
|
async_fire_time_changed(hass, utcnow() + SCAN_INTERVAL)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user