mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +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
07667a6aee
commit
6720580a9e
@ -202,22 +202,22 @@ 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
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Get the current state, catching errors."""
|
"""Get the current state, catching errors."""
|
||||||
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,14 +233,14 @@ 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
|
||||||
|
|
||||||
if self._is_on_template is not None:
|
if self._is_on_template is not None:
|
||||||
text = self._is_on_template.async_render_with_possible_json_value(
|
text = self._is_on_template.async_render_with_possible_json_value(
|
||||||
|
@ -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
|
||||||
@ -84,7 +83,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()
|
||||||
@ -98,7 +97,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()
|
||||||
@ -304,7 +303,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,
|
||||||
@ -364,7 +363,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,
|
||||||
@ -417,7 +416,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