From bb762d5b0f13781385c432c7e93c7593332a0823 Mon Sep 17 00:00:00 2001 From: rhpijnacker Date: Mon, 7 Feb 2022 10:48:33 +0100 Subject: [PATCH] 100% code coverage for config_flow of dsmr component (#65238) --- homeassistant/components/dsmr/config_flow.py | 30 ------- tests/components/dsmr/test_config_flow.py | 82 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/dsmr/config_flow.py b/homeassistant/components/dsmr/config_flow.py index e4c7ac4e658..8bbf6197a10 100644 --- a/homeassistant/components/dsmr/config_flow.py +++ b/homeassistant/components/dsmr/config_flow.py @@ -147,36 +147,6 @@ class DSMRFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Get the options flow for this handler.""" return DSMROptionFlowHandler(config_entry) - def _abort_if_host_port_configured( - self, - port: str, - host: str | None = None, - updates: dict[Any, Any] | None = None, - reload_on_update: bool = True, - ) -> FlowResult | None: - """Test if host and port are already configured.""" - for entry in self._async_current_entries(): - if entry.data.get(CONF_HOST) == host and entry.data[CONF_PORT] == port: - if updates is not None: - changed = self.hass.config_entries.async_update_entry( - entry, data={**entry.data, **updates} - ) - if ( - changed - and reload_on_update - and entry.state - in ( - config_entries.ConfigEntryState.LOADED, - config_entries.ConfigEntryState.SETUP_RETRY, - ) - ): - self.hass.async_create_task( - self.hass.config_entries.async_reload(entry.entry_id) - ) - return self.async_abort(reason="already_configured") - - return None - async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: diff --git a/tests/components/dsmr/test_config_flow.py b/tests/components/dsmr/test_config_flow.py index 692870d7037..669fcfac386 100644 --- a/tests/components/dsmr/test_config_flow.py +++ b/tests/components/dsmr/test_config_flow.py @@ -1,4 +1,5 @@ """Test the DSMR config flow.""" +import asyncio from itertools import chain, repeat import os from unittest.mock import DEFAULT, AsyncMock, MagicMock, patch, sentinel @@ -138,6 +139,45 @@ async def test_setup_5L(com_mock, hass, dsmr_connection_send_validate_fixture): assert result["data"] == entry_data +@patch("serial.tools.list_ports.comports", return_value=[com_port()]) +async def test_setup_5S(com_mock, hass, dsmr_connection_send_validate_fixture): + """Test we can setup serial.""" + port = com_port() + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + assert result["type"] == "form" + assert result["step_id"] == "user" + assert result["errors"] is None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"type": "Serial"}, + ) + + assert result["type"] == "form" + assert result["step_id"] == "setup_serial" + assert result["errors"] == {} + + with patch("homeassistant.components.dsmr.async_setup_entry", return_value=True): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"port": port.device, "dsmr_version": "5S"} + ) + + entry_data = { + "port": port.device, + "dsmr_version": "5S", + "serial_id": None, + "serial_id_gas": None, + } + + assert result["type"] == "create_entry" + assert result["title"] == port.device + assert result["data"] == entry_data + + @patch("serial.tools.list_ports.comports", return_value=[com_port()]) async def test_setup_Q3D(com_mock, hass, dsmr_connection_send_validate_fixture): """Test we can setup serial.""" @@ -265,6 +305,48 @@ async def test_setup_serial_fail(com_mock, hass, dsmr_connection_send_validate_f assert result["errors"] == {"base": "cannot_connect"} +@patch("serial.tools.list_ports.comports", return_value=[com_port()]) +async def test_setup_serial_timeout( + com_mock, hass, dsmr_connection_send_validate_fixture +): + """Test failed serial connection.""" + (connection_factory, transport, protocol) = dsmr_connection_send_validate_fixture + + port = com_port() + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + first_timeout_wait_closed = AsyncMock( + return_value=True, + side_effect=chain([asyncio.TimeoutError], repeat(DEFAULT)), + ) + protocol.wait_closed = first_timeout_wait_closed + + assert result["type"] == "form" + assert result["step_id"] == "user" + assert result["errors"] is None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"type": "Serial"}, + ) + + assert result["type"] == "form" + assert result["step_id"] == "setup_serial" + assert result["errors"] == {} + + with patch("homeassistant.components.dsmr.async_setup_entry", return_value=True): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"port": port.device, "dsmr_version": "2.2"} + ) + + assert result["type"] == "form" + assert result["step_id"] == "setup_serial" + assert result["errors"] == {"base": "cannot_communicate"} + + @patch("serial.tools.list_ports.comports", return_value=[com_port()]) async def test_setup_serial_wrong_telegram( com_mock, hass, dsmr_connection_send_validate_fixture