Fix nuheat response error checking (#33712)

This integration was checking request instead
of response for the error code.
This commit is contained in:
J. Nick Koston 2020-04-05 17:23:20 -05:00 committed by GitHub
parent a16e742107
commit d28b477f9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View file

@ -78,7 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
raise ConfigEntryNotReady raise ConfigEntryNotReady
except requests.exceptions.HTTPError as ex: 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) _LOGGER.error("Failed to login to nuheat: %s", ex)
return False return False
raise ConfigEntryNotReady raise ConfigEntryNotReady

View file

@ -34,7 +34,7 @@ async def validate_input(hass: core.HomeAssistant, data):
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
raise CannotConnect raise CannotConnect
except requests.exceptions.HTTPError as ex: 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 InvalidAuth
raise CannotConnect raise CannotConnect
# #

View file

@ -1,5 +1,5 @@
"""Test the NuHeat config flow.""" """Test the NuHeat config flow."""
from asynctest import patch from asynctest import MagicMock, patch
import requests import requests
from homeassistant import config_entries, setup from homeassistant import config_entries, setup
@ -100,6 +100,24 @@ async def test_form_invalid_auth(hass):
with patch( with patch(
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate", "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
side_effect=Exception, 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -120,12 +138,15 @@ async def test_form_invalid_thermostat(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
response_mock = MagicMock()
type(response_mock).status_code = 500
with patch( with patch(
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate", "homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
return_value=True, return_value=True,
), patch( ), patch(
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.get_thermostat", "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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],