mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Accept HTTP 200 through 206 as success for RESTful Switch (#105358)
This commit is contained in:
parent
4e1677e3f0
commit
327016eaeb
@ -171,7 +171,7 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||||||
try:
|
try:
|
||||||
req = await self.set_device_state(body_on_t)
|
req = await self.set_device_state(body_on_t)
|
||||||
|
|
||||||
if req.status_code == HTTPStatus.OK:
|
if HTTPStatus.OK <= req.status_code < HTTPStatus.MULTIPLE_CHOICES:
|
||||||
self._attr_is_on = True
|
self._attr_is_on = True
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
@ -186,7 +186,7 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
req = await self.set_device_state(body_off_t)
|
req = await self.set_device_state(body_off_t)
|
||||||
if req.status_code == HTTPStatus.OK:
|
if HTTPStatus.OK <= req.status_code < HTTPStatus.MULTIPLE_CHOICES:
|
||||||
self._attr_is_on = False
|
self._attr_is_on = False
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
@ -53,6 +53,22 @@ RESOURCE = "http://localhost/"
|
|||||||
STATE_RESOURCE = RESOURCE
|
STATE_RESOURCE = RESOURCE
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(
|
||||||
|
params=(
|
||||||
|
HTTPStatus.OK,
|
||||||
|
HTTPStatus.CREATED,
|
||||||
|
HTTPStatus.ACCEPTED,
|
||||||
|
HTTPStatus.NON_AUTHORITATIVE_INFORMATION,
|
||||||
|
HTTPStatus.NO_CONTENT,
|
||||||
|
HTTPStatus.RESET_CONTENT,
|
||||||
|
HTTPStatus.PARTIAL_CONTENT,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def http_success_code(request: pytest.FixtureRequest) -> HTTPStatus:
|
||||||
|
"""Fixture providing different successful HTTP response code."""
|
||||||
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_missing_config(
|
async def test_setup_missing_config(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -262,11 +278,14 @@ async def test_is_on_before_update(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
async def test_turn_on_success(hass: HomeAssistant) -> None:
|
async def test_turn_on_success(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
http_success_code: HTTPStatus,
|
||||||
|
) -> None:
|
||||||
"""Test turn_on."""
|
"""Test turn_on."""
|
||||||
await _async_setup_test_switch(hass)
|
await _async_setup_test_switch(hass)
|
||||||
|
|
||||||
route = respx.post(RESOURCE) % HTTPStatus.OK
|
route = respx.post(RESOURCE) % http_success_code
|
||||||
respx.get(RESOURCE).mock(side_effect=httpx.RequestError)
|
respx.get(RESOURCE).mock(side_effect=httpx.RequestError)
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SWITCH_DOMAIN,
|
SWITCH_DOMAIN,
|
||||||
@ -320,11 +339,14 @@ async def test_turn_on_timeout(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
async def test_turn_off_success(hass: HomeAssistant) -> None:
|
async def test_turn_off_success(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
http_success_code: HTTPStatus,
|
||||||
|
) -> None:
|
||||||
"""Test turn_off."""
|
"""Test turn_off."""
|
||||||
await _async_setup_test_switch(hass)
|
await _async_setup_test_switch(hass)
|
||||||
|
|
||||||
route = respx.post(RESOURCE) % HTTPStatus.OK
|
route = respx.post(RESOURCE) % http_success_code
|
||||||
respx.get(RESOURCE).mock(side_effect=httpx.RequestError)
|
respx.get(RESOURCE).mock(side_effect=httpx.RequestError)
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SWITCH_DOMAIN,
|
SWITCH_DOMAIN,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user