"""Test the NuHeat config flow.""" from asynctest import patch 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, ): 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} ) 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, ): 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"}