From 447446c56560aeace5a2ae5327d494339f572134 Mon Sep 17 00:00:00 2001 From: On Freund Date: Mon, 21 Sep 2020 21:27:00 +0300 Subject: [PATCH] Fix handling of empty ws port (#40399) --- homeassistant/components/kodi/config_flow.py | 4 ++ tests/components/kodi/test_config_flow.py | 45 ++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/homeassistant/components/kodi/config_flow.py b/homeassistant/components/kodi/config_flow.py index 067ee8be476..c11255aba87 100644 --- a/homeassistant/components/kodi/config_flow.py +++ b/homeassistant/components/kodi/config_flow.py @@ -202,6 +202,10 @@ class KodiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: self._ws_port = user_input.get(CONF_WS_PORT) + # optional ints return 0 rather than None when empty + if self._ws_port == 0: + self._ws_port = None + try: await validate_ws(self.hass, self._get_data()) except WSCannotConnect: diff --git a/tests/components/kodi/test_config_flow.py b/tests/components/kodi/test_config_flow.py index 4fd61ede8ba..71c2bce1307 100644 --- a/tests/components/kodi/test_config_flow.py +++ b/tests/components/kodi/test_config_flow.py @@ -165,6 +165,51 @@ async def test_form_valid_ws_port(hass, user_flow): assert len(mock_setup_entry.mock_calls) == 1 +async def test_form_empty_ws_port(hass, user_flow): + """Test we handle an empty websocket port input.""" + with patch( + "homeassistant.components.kodi.config_flow.Kodi.ping", + return_value=True, + ), patch.object( + MockWSConnection, + "connect", + AsyncMock(side_effect=CannotConnectError), + ), patch( + "homeassistant.components.kodi.config_flow.get_kodi_connection", + new=get_kodi_connection, + ): + result = await hass.config_entries.flow.async_configure(user_flow, TEST_HOST) + + assert result["type"] == "form" + assert result["step_id"] == "ws_port" + assert result["errors"] == {} + + with patch( + "homeassistant.components.kodi.async_setup", return_value=True + ) as mock_setup, patch( + "homeassistant.components.kodi.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"ws_port": 0} + ) + + assert result["type"] == "create_entry" + assert result["title"] == TEST_HOST["host"] + assert result["data"] == { + **TEST_HOST, + "ws_port": None, + "password": None, + "username": None, + "name": None, + "timeout": DEFAULT_TIMEOUT, + } + + 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, user_flow): """Test we handle invalid auth.""" with patch(