diff --git a/homeassistant/components/nuheat/__init__.py b/homeassistant/components/nuheat/__init__.py index ff90bb26530..ca47f831370 100644 --- a/homeassistant/components/nuheat/__init__.py +++ b/homeassistant/components/nuheat/__init__.py @@ -78,7 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): except requests.exceptions.Timeout: raise ConfigEntryNotReady except requests.exceptions.HTTPError as ex: - if ex.request.status_code > 400 and ex.request.status_code < 500: + if ex.response.status_code > 400 and ex.response.status_code < 500: _LOGGER.error("Failed to login to nuheat: %s", ex) return False raise ConfigEntryNotReady diff --git a/homeassistant/components/nuheat/config_flow.py b/homeassistant/components/nuheat/config_flow.py index 082cb899ec5..4f12f590057 100644 --- a/homeassistant/components/nuheat/config_flow.py +++ b/homeassistant/components/nuheat/config_flow.py @@ -34,7 +34,7 @@ async def validate_input(hass: core.HomeAssistant, data): except requests.exceptions.Timeout: raise CannotConnect except requests.exceptions.HTTPError as ex: - if ex.request.status_code > 400 and ex.request.status_code < 500: + if ex.response.status_code > 400 and ex.response.status_code < 500: raise InvalidAuth raise CannotConnect # diff --git a/tests/components/nuheat/test_config_flow.py b/tests/components/nuheat/test_config_flow.py index 95987404e44..d6e10e1dc7c 100644 --- a/tests/components/nuheat/test_config_flow.py +++ b/tests/components/nuheat/test_config_flow.py @@ -1,5 +1,5 @@ """Test the NuHeat config flow.""" -from asynctest import patch +from asynctest import MagicMock, patch import requests from homeassistant import config_entries, setup @@ -100,6 +100,24 @@ async def test_form_invalid_auth(hass): with patch( "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate", side_effect=Exception, + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_SERIAL_NUMBER: "12345", + CONF_USERNAME: "test-username", + CONF_PASSWORD: "test-password", + }, + ) + + assert result["type"] == "form" + assert result["errors"] == {"base": "invalid_auth"} + + response_mock = MagicMock() + type(response_mock).status_code = 401 + with patch( + "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate", + side_effect=requests.HTTPError(response=response_mock), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -120,12 +138,15 @@ async def test_form_invalid_thermostat(hass): DOMAIN, context={"source": config_entries.SOURCE_USER} ) + response_mock = MagicMock() + type(response_mock).status_code = 500 + with patch( "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate", return_value=True, ), patch( "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.get_thermostat", - side_effect=requests.exceptions.HTTPError, + side_effect=requests.HTTPError(response=response_mock), ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"],