Fix HomeWizard is not catching RequestError (#73719)

* Fix RequestError was not catched

* Add test for RequestError
This commit is contained in:
Duco Sebel 2022-06-20 10:30:57 +02:00 committed by GitHub
parent 006ea441ad
commit db5e94c93b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View file

@ -5,7 +5,7 @@ import logging
from typing import Any, cast
from homewizard_energy import HomeWizardEnergy
from homewizard_energy.errors import DisabledError, UnsupportedError
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
from voluptuous import Required, Schema
from homeassistant import config_entries
@ -187,6 +187,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_LOGGER.error("API version unsuppored")
raise AbortFlow("unsupported_api_version") from ex
except RequestError as ex:
_LOGGER.error("Unexpected or no response")
raise AbortFlow("unknown_error") from ex
except Exception as ex:
_LOGGER.exception(
"Error connecting with Energy Device at %s",

View file

@ -4,7 +4,7 @@ from __future__ import annotations
import logging
from homewizard_energy import HomeWizardEnergy
from homewizard_energy.errors import DisabledError
from homewizard_energy.errors import DisabledError, RequestError
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -41,6 +41,9 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
"state": await self.api.state(),
}
except RequestError as ex:
raise UpdateFailed("Device did not respond as expected") from ex
except DisabledError as ex:
raise UpdateFailed("API disabled, API must be enabled in the app") from ex

View file

@ -2,7 +2,7 @@
import logging
from unittest.mock import patch
from homewizard_energy.errors import DisabledError, UnsupportedError
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
from homeassistant import config_entries
from homeassistant.components import zeroconf
@ -333,3 +333,31 @@ async def test_check_detects_invalid_api(hass, aioclient_mock):
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "unsupported_api_version"
async def test_check_requesterror(hass, aioclient_mock):
"""Test check detecting device endpoint failed fetching data due to a requesterror."""
def mock_initialize():
raise RequestError
device = get_mock_device()
device.device.side_effect = mock_initialize
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["step_id"] == "user"
with patch(
"homeassistant.components.homewizard.config_flow.HomeWizardEnergy",
return_value=device,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_IP_ADDRESS: "2.2.2.2"}
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "unknown_error"