Fix nuheat response error checking (#33712)

This integration was checking request instead
of response for the error code.
pull/33814/head
J. Nick Koston 2020-04-05 17:23:20 -05:00 committed by Paulus Schoutsen
parent 5fd8763c3c
commit 49dc7ffb5b
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:
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

View File

@ -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
#

View File

@ -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"],