Improve Xioami Aqara zeroconf discovery handling (#37469)
Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
485752a033
commit
e43fb649a4
5 files changed
+209
-73
No files matched your search
@@ -34,13 +34,22 @@ def xiaomi_aqara_fixture():
|
||||
with patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGatewayDiscovery",
|
||||
return_value=mock_gateway_discovery,
|
||||
), patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGateway",
|
||||
return_value=mock_gateway_discovery.gateways[TEST_HOST],
|
||||
), patch(
|
||||
"homeassistant.components.xiaomi_aqara.async_setup_entry", return_value=True
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
def get_mock_discovery(host_list, invalid_interface=False, invalid_key=False):
|
||||
def get_mock_discovery(
|
||||
host_list,
|
||||
invalid_interface=False,
|
||||
invalid_key=False,
|
||||
invalid_host=False,
|
||||
invalid_mac=False,
|
||||
):
|
||||
"""Return a mock gateway info instance."""
|
||||
gateway_discovery = Mock()
|
||||
|
||||
@@ -52,6 +61,8 @@ def get_mock_discovery(host_list, invalid_interface=False, invalid_key=False):
|
||||
gateway.port = TEST_PORT
|
||||
gateway.sid = TEST_SID
|
||||
gateway.proto = TEST_PROTOCOL
|
||||
gateway.connection_error = invalid_host
|
||||
gateway.mac_error = invalid_mac
|
||||
|
||||
if invalid_key:
|
||||
gateway.write_to_hub = Mock(return_value=False)
|
||||
@@ -185,6 +196,52 @@ async def test_config_flow_user_no_key_success(hass):
|
||||
}
|
||||
|
||||
|
||||
async def test_config_flow_user_host_mac_success(hass):
|
||||
"""Test a successful config flow initialized by the user with a host and mac specified."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_gateway_discovery = get_mock_discovery([])
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGatewayDiscovery",
|
||||
return_value=mock_gateway_discovery,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
const.CONF_INTERFACE: config_flow.DEFAULT_INTERFACE,
|
||||
CONF_HOST: TEST_HOST,
|
||||
CONF_MAC: TEST_MAC,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "settings"
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_NAME: TEST_NAME},
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == TEST_NAME
|
||||
assert result["data"] == {
|
||||
CONF_HOST: TEST_HOST,
|
||||
CONF_PORT: TEST_PORT,
|
||||
CONF_MAC: TEST_MAC,
|
||||
const.CONF_INTERFACE: config_flow.DEFAULT_INTERFACE,
|
||||
const.CONF_PROTOCOL: TEST_PROTOCOL,
|
||||
const.CONF_KEY: None,
|
||||
const.CONF_SID: TEST_SID,
|
||||
}
|
||||
|
||||
|
||||
async def test_config_flow_user_discovery_error(hass):
|
||||
"""Test a failed config flow initialized by the user with no gateways discoverd."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -235,6 +292,66 @@ async def test_config_flow_user_invalid_interface(hass):
|
||||
assert result["errors"] == {const.CONF_INTERFACE: "invalid_interface"}
|
||||
|
||||
|
||||
async def test_config_flow_user_invalid_host(hass):
|
||||
"""Test a failed config flow initialized by the user with an invalid host."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_gateway_discovery = get_mock_discovery([TEST_HOST], invalid_host=True)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGateway",
|
||||
return_value=mock_gateway_discovery.gateways[TEST_HOST],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
const.CONF_INTERFACE: config_flow.DEFAULT_INTERFACE,
|
||||
CONF_HOST: "0.0.0.0",
|
||||
CONF_MAC: TEST_MAC,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"host": "invalid_host"}
|
||||
|
||||
|
||||
async def test_config_flow_user_invalid_mac(hass):
|
||||
"""Test a failed config flow initialized by the user with an invalid mac."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
const.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_gateway_discovery = get_mock_discovery([TEST_HOST], invalid_mac=True)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGateway",
|
||||
return_value=mock_gateway_discovery.gateways[TEST_HOST],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
const.CONF_INTERFACE: config_flow.DEFAULT_INTERFACE,
|
||||
CONF_HOST: TEST_HOST,
|
||||
CONF_MAC: "in:va:li:d0:0m:ac",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"mac": "invalid_mac"}
|
||||
|
||||
|
||||
async def test_config_flow_user_invalid_key(hass):
|
||||
"""Test a failed config flow initialized by the user with an invalid key."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -335,34 +452,3 @@ async def test_zeroconf_unknown_device(hass):
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_xiaomi_aqara"
|
||||
|
||||
|
||||
async def test_zeroconf_not_found_error(hass):
|
||||
"""Test a failed zeroconf discovery because the correct gateway could not be found."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
const.DOMAIN,
|
||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||
data={
|
||||
zeroconf.ATTR_HOST: TEST_HOST,
|
||||
ZEROCONF_NAME: TEST_ZEROCONF_NAME,
|
||||
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_gateway_discovery = get_mock_discovery([TEST_HOST_2])
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.xiaomi_aqara.config_flow.XiaomiGatewayDiscovery",
|
||||
return_value=mock_gateway_discovery,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {const.CONF_INTERFACE: config_flow.DEFAULT_INTERFACE},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "not_found_error"}
|
||||
Reference in new issue
Block a user