2020-03-23 05:29:45 +00:00
|
|
|
"""Test the NuHeat config flow."""
|
2020-04-05 22:23:20 +00:00
|
|
|
from asynctest import MagicMock, patch
|
2020-03-23 05:29:45 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from homeassistant import config_entries, setup
|
|
|
|
from homeassistant.components.nuheat.const import CONF_SERIAL_NUMBER, DOMAIN
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
|
|
|
|
from .mocks import _get_mock_thermostat_run
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_user(hass):
|
|
|
|
"""Test we get the form with user source."""
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
mock_thermostat = _get_mock_thermostat_run()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.get_thermostat",
|
|
|
|
return_value=mock_thermostat,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.nuheat.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.nuheat.async_setup_entry", return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "Master bathroom"
|
|
|
|
assert result2["data"] == {
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_import(hass):
|
|
|
|
"""Test we get the form with import source."""
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
mock_thermostat = _get_mock_thermostat_run()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.get_thermostat",
|
|
|
|
return_value=mock_thermostat,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.nuheat.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.nuheat.async_setup_entry", return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["title"] == "Master bathroom"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_invalid_auth(hass):
|
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
|
|
|
|
side_effect=Exception,
|
2020-04-05 22:23:20 +00:00
|
|
|
):
|
|
|
|
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),
|
2020-03-23 05:29:45 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_invalid_thermostat(hass):
|
|
|
|
"""Test we handle invalid thermostats."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2020-04-05 22:23:20 +00:00
|
|
|
response_mock = MagicMock()
|
|
|
|
type(response_mock).status_code = 500
|
|
|
|
|
2020-03-23 05:29:45 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
|
|
|
|
return_value=True,
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.get_thermostat",
|
2020-04-05 22:23:20 +00:00
|
|
|
side_effect=requests.HTTPError(response=response_mock),
|
2020-03-23 05:29:45 +00:00
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "invalid_thermostat"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_cannot_connect(hass):
|
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.nuheat.config_flow.nuheat.NuHeat.authenticate",
|
|
|
|
side_effect=requests.exceptions.Timeout,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
CONF_SERIAL_NUMBER: "12345",
|
|
|
|
CONF_USERNAME: "test-username",
|
|
|
|
CONF_PASSWORD: "test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|