Remove ipaddress check in AndroidTV config flow (#68630)
This commit is contained in:
parent
88780b4c87
commit
0d8736b82b
2 changed files with 40 additions and 81 deletions
|
@ -2,7 +2,6 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
|
||||||
|
|
||||||
from androidtv import state_detection_rules_validator
|
from androidtv import state_detection_rules_validator
|
||||||
import voluptuous as vol
|
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)
|
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):
|
class AndroidTVFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow."""
|
"""Handle a config flow."""
|
||||||
|
|
||||||
|
@ -137,24 +128,17 @@ class AndroidTVFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
host = user_input[CONF_HOST]
|
host = user_input[CONF_HOST]
|
||||||
adb_key = user_input.get(CONF_ADBKEY)
|
adb_key = user_input.get(CONF_ADBKEY)
|
||||||
adb_server = user_input.get(CONF_ADB_SERVER_IP)
|
if CONF_ADB_SERVER_IP in user_input:
|
||||||
|
if adb_key:
|
||||||
if adb_key and adb_server:
|
return self._show_setup_form(user_input, "key_and_server")
|
||||||
return self._show_setup_form(user_input, "key_and_server")
|
else:
|
||||||
|
user_input.pop(CONF_ADB_SERVER_PORT, None)
|
||||||
|
|
||||||
if adb_key:
|
if adb_key:
|
||||||
isfile = await self.hass.async_add_executor_job(_is_file, adb_key)
|
if not await self.hass.async_add_executor_job(_is_file, adb_key):
|
||||||
if not isfile:
|
|
||||||
return self._show_setup_form(user_input, "adbkey_not_file")
|
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})
|
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)
|
error, unique_id = await self._async_check_connection(user_input)
|
||||||
if error is None:
|
if error is None:
|
||||||
if not unique_id:
|
if not unique_id:
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
"""Tests for the AndroidTV config flow."""
|
"""Tests for the AndroidTV config flow."""
|
||||||
import json
|
import json
|
||||||
from socket import gaierror
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -42,6 +41,7 @@ from tests.components.androidtv.patchers import isfile
|
||||||
ADBKEY = "adbkey"
|
ADBKEY = "adbkey"
|
||||||
ETH_MAC = "a1:b1:c1:d1:e1:f1"
|
ETH_MAC = "a1:b1:c1:d1:e1:f1"
|
||||||
WIFI_MAC = "a2:b2:c2:d2:e2:f2"
|
WIFI_MAC = "a2:b2:c2:d2:e2:f2"
|
||||||
|
INVALID_MAC = "ff:ff:ff:ff:ff:ff"
|
||||||
HOST = "127.0.0.1"
|
HOST = "127.0.0.1"
|
||||||
VALID_DETECT_RULE = [{"paused": {"media_session_state": 3}}]
|
VALID_DETECT_RULE = [{"paused": {"media_session_state": 3}}]
|
||||||
|
|
||||||
|
@ -50,7 +50,6 @@ CONFIG_PYTHON_ADB = {
|
||||||
CONF_HOST: HOST,
|
CONF_HOST: HOST,
|
||||||
CONF_PORT: DEFAULT_PORT,
|
CONF_PORT: DEFAULT_PORT,
|
||||||
CONF_DEVICE_CLASS: "androidtv",
|
CONF_DEVICE_CLASS: "androidtv",
|
||||||
CONF_ADB_SERVER_PORT: DEFAULT_ADB_SERVER_PORT,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Android TV device with ADB server
|
# Android TV device with ADB server
|
||||||
|
@ -68,10 +67,6 @@ CONNECT_METHOD = (
|
||||||
PATCH_ACCESS = patch(
|
PATCH_ACCESS = patch(
|
||||||
"homeassistant.components.androidtv.config_flow.os.access", return_value=True
|
"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(
|
PATCH_ISFILE = patch(
|
||||||
"homeassistant.components.androidtv.config_flow.os.path.isfile", isfile
|
"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(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(eth_mac, wifi_mac), None),
|
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(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
flow_result["flow_id"], user_input=config
|
flow_result["flow_id"], user_input=config
|
||||||
)
|
)
|
||||||
|
@ -138,7 +133,7 @@ async def test_user_adbkey(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(), None),
|
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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -171,7 +166,7 @@ async def test_error_both_key_server(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(), None),
|
return_value=(MockConfigDevice(), None),
|
||||||
), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP:
|
), PATCH_SETUP_ENTRY:
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||||
)
|
)
|
||||||
|
@ -198,7 +193,7 @@ async def test_error_invalid_key(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(), None),
|
return_value=(MockConfigDevice(), None),
|
||||||
), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP:
|
), PATCH_SETUP_ENTRY:
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
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
|
assert result2["data"] == CONFIG_ADB_SERVER
|
||||||
|
|
||||||
|
|
||||||
async def test_error_invalid_host(hass):
|
@pytest.mark.parametrize(
|
||||||
"""Test we abort if host name is invalid."""
|
["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(
|
with patch(
|
||||||
"socket.gethostbyname",
|
CONNECT_METHOD,
|
||||||
side_effect=gaierror,
|
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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
data=CONFIG_ADB_SERVER,
|
data=config,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
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
|
domain=DOMAIN, data=CONFIG_ADB_SERVER, unique_id=ETH_MAC
|
||||||
).add_to_hass(hass)
|
).add_to_hass(hass)
|
||||||
|
|
||||||
config_data = CONFIG_ADB_SERVER.copy()
|
config_data = CONFIG_PYTHON_ADB
|
||||||
config_data[CONF_HOST] = "name"
|
# Should fail, same HOST
|
||||||
# Should fail, same IP Address (by PATCH_GET_HOST_IP)
|
result = await hass.config_entries.flow.async_init(
|
||||||
with PATCH_GET_HOST_IP:
|
DOMAIN,
|
||||||
result = await hass.config_entries.flow.async_init(
|
context={"source": SOURCE_USER},
|
||||||
DOMAIN,
|
data=config_data,
|
||||||
context={"source": SOURCE_USER},
|
)
|
||||||
data=config_data,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_if_unique_exist(hass):
|
async def test_abort_if_unique_exist(hass):
|
||||||
|
@ -286,7 +261,7 @@ async def test_abort_if_unique_exist(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(), None),
|
return_value=(MockConfigDevice(), None),
|
||||||
), PATCH_GET_HOST_IP:
|
):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
|
@ -304,7 +279,7 @@ async def test_on_connect_failed(hass):
|
||||||
context={"source": SOURCE_USER, "show_advanced_options": True},
|
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(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
flow_result["flow_id"], user_input=CONFIG_ADB_SERVER
|
flow_result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||||
)
|
)
|
||||||
|
@ -314,7 +289,7 @@ async def test_on_connect_failed(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
side_effect=TypeError,
|
side_effect=TypeError,
|
||||||
), PATCH_GET_HOST_IP:
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||||
)
|
)
|
||||||
|
@ -324,7 +299,7 @@ async def test_on_connect_failed(hass):
|
||||||
with patch(
|
with patch(
|
||||||
CONNECT_METHOD,
|
CONNECT_METHOD,
|
||||||
return_value=(MockConfigDevice(), None),
|
return_value=(MockConfigDevice(), None),
|
||||||
), PATCH_SETUP_ENTRY, PATCH_GET_HOST_IP:
|
), PATCH_SETUP_ENTRY:
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
result2["flow_id"], user_input=CONFIG_ADB_SERVER
|
result2["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue