Accept HTTP 200 through 206 as success for RESTful Switch (#105358)
This commit is contained in:
parent
4e1677e3f0
commit
327016eaeb
2 changed files with 28 additions and 6 deletions
|
@ -171,7 +171,7 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||
try:
|
||||
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
|
||||
else:
|
||||
_LOGGER.error(
|
||||
|
@ -186,7 +186,7 @@ class RestSwitch(ManualTriggerEntity, SwitchEntity):
|
|||
|
||||
try:
|
||||
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
|
||||
else:
|
||||
_LOGGER.error(
|
||||
|
|
|
@ -53,6 +53,22 @@ RESOURCE = "http://localhost/"
|
|||
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(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
|
@ -262,11 +278,14 @@ async def test_is_on_before_update(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
@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."""
|
||||
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)
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
|
@ -320,11 +339,14 @@ async def test_turn_on_timeout(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
@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."""
|
||||
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)
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
|
|
Loading…
Add table
Reference in a new issue