From 0d8736b82b231760b92c5bca3dc9923f36ae51dd Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:44:33 +0200 Subject: [PATCH] Remove ipaddress check in AndroidTV config flow (#68630) --- .../components/androidtv/config_flow.py | 28 ++---- .../components/androidtv/test_config_flow.py | 93 +++++++------------ 2 files changed, 40 insertions(+), 81 deletions(-) diff --git a/homeassistant/components/androidtv/config_flow.py b/homeassistant/components/androidtv/config_flow.py index 520c0eccbeb2..9df87eaffe2e 100644 --- a/homeassistant/components/androidtv/config_flow.py +++ b/homeassistant/components/androidtv/config_flow.py @@ -2,7 +2,6 @@ import json import logging import os -import socket from androidtv import state_detection_rules_validator import voluptuous as vol @@ -58,14 +57,6 @@ def _is_file(value): return os.path.isfile(file_in) and os.access(file_in, os.R_OK) -def _get_ip(host): - """Get the ip address from the host name.""" - try: - return socket.gethostbyname(host) - except socket.gaierror: - return None - - class AndroidTVFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow.""" @@ -137,24 +128,17 @@ class AndroidTVFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: host = user_input[CONF_HOST] adb_key = user_input.get(CONF_ADBKEY) - adb_server = user_input.get(CONF_ADB_SERVER_IP) - - if adb_key and adb_server: - return self._show_setup_form(user_input, "key_and_server") + if CONF_ADB_SERVER_IP in user_input: + if adb_key: + return self._show_setup_form(user_input, "key_and_server") + else: + user_input.pop(CONF_ADB_SERVER_PORT, None) if adb_key: - isfile = await self.hass.async_add_executor_job(_is_file, adb_key) - if not isfile: + if not await self.hass.async_add_executor_job(_is_file, adb_key): return self._show_setup_form(user_input, "adbkey_not_file") - ip_address = await self.hass.async_add_executor_job(_get_ip, host) - if not ip_address: - return self._show_setup_form(user_input, "invalid_host") - self._async_abort_entries_match({CONF_HOST: host}) - if ip_address != host: - self._async_abort_entries_match({CONF_HOST: ip_address}) - error, unique_id = await self._async_check_connection(user_input) if error is None: if not unique_id: diff --git a/tests/components/androidtv/test_config_flow.py b/tests/components/androidtv/test_config_flow.py index 3bef7afb1f6c..a6541e53e18e 100644 --- a/tests/components/androidtv/test_config_flow.py +++ b/tests/components/androidtv/test_config_flow.py @@ -1,6 +1,5 @@ """Tests for the AndroidTV config flow.""" import json -from socket import gaierror from unittest.mock import patch import pytest @@ -42,6 +41,7 @@ from tests.components.androidtv.patchers import isfile ADBKEY = "adbkey" ETH_MAC = "a1:b1:c1:d1:e1:f1" WIFI_MAC = "a2:b2:c2:d2:e2:f2" +INVALID_MAC = "ff:ff:ff:ff:ff:ff" HOST = "127.0.0.1" VALID_DETECT_RULE = [{"paused": {"media_session_state": 3}}] @@ -50,7 +50,6 @@ CONFIG_PYTHON_ADB = { CONF_HOST: HOST, CONF_PORT: DEFAULT_PORT, CONF_DEVICE_CLASS: "androidtv", - CONF_ADB_SERVER_PORT: DEFAULT_ADB_SERVER_PORT, } # Android TV device with ADB server @@ -68,10 +67,6 @@ CONNECT_METHOD = ( PATCH_ACCESS = patch( "homeassistant.components.androidtv.config_flow.os.access", return_value=True ) -PATCH_GET_HOST_IP = patch( - "homeassistant.components.androidtv.config_flow.socket.gethostbyname", - return_value=HOST, -) PATCH_ISFILE = patch( "homeassistant.components.androidtv.config_flow.os.path.isfile", isfile ) @@ -117,7 +112,7 @@ async def test_user(hass, config, eth_mac, wifi_mac): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(eth_mac, wifi_mac), None), - ), PATCH_SETUP_ENTRY as mock_setup_entry, PATCH_GET_HOST_IP: + ), PATCH_SETUP_ENTRY as mock_setup_entry: result = await hass.config_entries.flow.async_configure( flow_result["flow_id"], user_input=config ) @@ -138,7 +133,7 @@ async def test_user_adbkey(hass): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(), None), - ), PATCH_SETUP_ENTRY as mock_setup_entry, PATCH_GET_HOST_IP, PATCH_ISFILE, PATCH_ACCESS: + ), PATCH_SETUP_ENTRY as mock_setup_entry, PATCH_ISFILE, PATCH_ACCESS: result = await hass.config_entries.flow.async_init( DOMAIN, @@ -171,7 +166,7 @@ async def test_error_both_key_server(hass): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(), None), - ), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP: + ), PATCH_SETUP_ENTRY: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CONFIG_ADB_SERVER ) @@ -198,7 +193,7 @@ async def test_error_invalid_key(hass): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(), None), - ), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP: + ), PATCH_SETUP_ENTRY: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CONFIG_ADB_SERVER ) @@ -209,45 +204,27 @@ async def test_error_invalid_key(hass): assert result2["data"] == CONFIG_ADB_SERVER -async def test_error_invalid_host(hass): - """Test we abort if host name is invalid.""" +@pytest.mark.parametrize( + ["config", "eth_mac", "wifi_mac"], + [ + (CONFIG_ADB_SERVER, None, None), + (CONFIG_PYTHON_ADB, None, None), + (CONFIG_ADB_SERVER, INVALID_MAC, None), + (CONFIG_PYTHON_ADB, INVALID_MAC, None), + (CONFIG_ADB_SERVER, None, INVALID_MAC), + (CONFIG_PYTHON_ADB, None, INVALID_MAC), + ], +) +async def test_invalid_mac(hass, config, eth_mac, wifi_mac): + """Test for invalid mac address.""" with patch( - "socket.gethostbyname", - side_effect=gaierror, + CONNECT_METHOD, + return_value=(MockConfigDevice(eth_mac, wifi_mac), None), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER, "show_advanced_options": True}, - data=CONFIG_ADB_SERVER, - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["errors"] == {"base": "invalid_host"} - - with patch( - CONNECT_METHOD, - return_value=(MockConfigDevice(), None), - ), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP: - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=CONFIG_ADB_SERVER - ) - await hass.async_block_till_done() - - assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result2["title"] == HOST - assert result2["data"] == CONFIG_ADB_SERVER - - -async def test_invalid_serial(hass): - """Test for invalid serialno.""" - with patch( - CONNECT_METHOD, - return_value=(MockConfigDevice(eth_mac=None), None), - ), PATCH_GET_HOST_IP: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, - data=CONFIG_ADB_SERVER, + data=config, ) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT @@ -260,18 +237,16 @@ async def test_abort_if_host_exist(hass): domain=DOMAIN, data=CONFIG_ADB_SERVER, unique_id=ETH_MAC ).add_to_hass(hass) - config_data = CONFIG_ADB_SERVER.copy() - config_data[CONF_HOST] = "name" - # Should fail, same IP Address (by PATCH_GET_HOST_IP) - with PATCH_GET_HOST_IP: - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data=config_data, - ) + config_data = CONFIG_PYTHON_ADB + # Should fail, same HOST + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data=config_data, + ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT - assert result["reason"] == "already_configured" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" async def test_abort_if_unique_exist(hass): @@ -286,7 +261,7 @@ async def test_abort_if_unique_exist(hass): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(), None), - ), PATCH_GET_HOST_IP: + ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, @@ -304,7 +279,7 @@ async def test_on_connect_failed(hass): context={"source": SOURCE_USER, "show_advanced_options": True}, ) - with patch(CONNECT_METHOD, return_value=(None, "Error")), PATCH_GET_HOST_IP: + with patch(CONNECT_METHOD, return_value=(None, "Error")): result = await hass.config_entries.flow.async_configure( flow_result["flow_id"], user_input=CONFIG_ADB_SERVER ) @@ -314,7 +289,7 @@ async def test_on_connect_failed(hass): with patch( CONNECT_METHOD, side_effect=TypeError, - ), PATCH_GET_HOST_IP: + ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CONFIG_ADB_SERVER ) @@ -324,7 +299,7 @@ async def test_on_connect_failed(hass): with patch( CONNECT_METHOD, return_value=(MockConfigDevice(), None), - ), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP: + ), PATCH_SETUP_ENTRY: result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input=CONFIG_ADB_SERVER )