"""Test the Antifurto365 iAlarm config flow.""" from unittest.mock import patch from homeassistant import config_entries from homeassistant.components.ialarm.const import DOMAIN from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry TEST_DATA = {CONF_HOST: "1.1.1.1", CONF_PORT: 18034} TEST_MAC = "00:00:54:12:34:56" async def test_form(hass: HomeAssistant) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] is None with ( patch( "homeassistant.components.ialarm.config_flow.IAlarm.get_status", return_value=1, ), patch( "homeassistant.components.ialarm.config_flow.IAlarm.get_mac", return_value=TEST_MAC, ), patch( "homeassistant.components.ialarm.async_setup_entry", return_value=True, ) as mock_setup_entry, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_DATA ) await hass.async_block_till_done() assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["title"] == TEST_DATA["host"] assert result2["data"] == TEST_DATA assert len(mock_setup_entry.mock_calls) == 1 async def test_form_cannot_connect(hass: HomeAssistant) -> None: """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.ialarm.config_flow.IAlarm.get_mac", side_effect=ConnectionError, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_DATA ) assert result2["type"] is FlowResultType.FORM assert result2["errors"] == {"base": "cannot_connect"} async def test_form_exception(hass: HomeAssistant) -> None: """Test we handle unknown exception.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( "homeassistant.components.ialarm.config_flow.IAlarm.get_mac", side_effect=Exception, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_DATA ) assert result2["type"] is FlowResultType.FORM assert result2["errors"] == {"base": "unknown"} async def test_form_already_exists(hass: HomeAssistant) -> None: """Test that a flow with an existing host aborts.""" entry = MockConfigEntry( domain=DOMAIN, unique_id=TEST_MAC, data=TEST_DATA, ) entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) with patch( "homeassistant.components.ialarm.config_flow.IAlarm.get_mac", return_value=TEST_MAC, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], TEST_DATA ) assert result2["type"] is FlowResultType.ABORT assert result2["reason"] == "already_configured"