From db5e94c93ba9fb97722aeb835468696471bfb0b6 Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Mon, 20 Jun 2022 10:30:57 +0200 Subject: [PATCH] Fix HomeWizard is not catching RequestError (#73719) * Fix RequestError was not catched * Add test for RequestError --- .../components/homewizard/config_flow.py | 6 +++- .../components/homewizard/coordinator.py | 5 +++- .../components/homewizard/test_config_flow.py | 30 ++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homewizard/config_flow.py b/homeassistant/components/homewizard/config_flow.py index 7d06f08ce74..6f164637a7c 100644 --- a/homeassistant/components/homewizard/config_flow.py +++ b/homeassistant/components/homewizard/config_flow.py @@ -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", diff --git a/homeassistant/components/homewizard/coordinator.py b/homeassistant/components/homewizard/coordinator.py index e12edda63ae..bab7b5d3ba3 100644 --- a/homeassistant/components/homewizard/coordinator.py +++ b/homeassistant/components/homewizard/coordinator.py @@ -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 diff --git a/tests/components/homewizard/test_config_flow.py b/tests/components/homewizard/test_config_flow.py index d2e7d4c58ae..fca00b71892 100644 --- a/tests/components/homewizard/test_config_flow.py +++ b/tests/components/homewizard/test_config_flow.py @@ -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"