2018-12-17 00:29:32 +00:00
|
|
|
"""Test config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2023-09-19 16:58:46 +00:00
|
|
|
from ipaddress import ip_address
|
2023-07-03 02:47:25 +00:00
|
|
|
import json
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
from aioesphomeapi import (
|
2023-06-26 01:18:21 +00:00
|
|
|
APIClient,
|
2021-09-20 07:02:17 +00:00
|
|
|
APIConnectionError,
|
2022-12-11 03:26:42 +00:00
|
|
|
DeviceInfo,
|
2021-09-20 07:02:17 +00:00
|
|
|
InvalidAuthAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError,
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
ResolveAPIError,
|
|
|
|
)
|
2023-07-02 14:29:45 +00:00
|
|
|
import aiohttp
|
2018-12-17 00:29:32 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
from homeassistant import config_entries
|
2022-06-30 17:07:03 +00:00
|
|
|
from homeassistant.components import dhcp, zeroconf
|
2023-08-10 12:27:03 +00:00
|
|
|
from homeassistant.components.esphome import DomainData, dashboard
|
2023-06-26 01:18:21 +00:00
|
|
|
from homeassistant.components.esphome.const import (
|
|
|
|
CONF_ALLOW_SERVICE_CALLS,
|
2023-07-08 07:19:44 +00:00
|
|
|
CONF_DEVICE_NAME,
|
|
|
|
CONF_NOISE_PSK,
|
2023-06-26 01:18:21 +00:00
|
|
|
DEFAULT_NEW_CONFIG_ALLOW_ALLOW_SERVICE_CALLS,
|
2023-07-08 07:19:44 +00:00
|
|
|
DOMAIN,
|
2023-06-26 01:18:21 +00:00
|
|
|
)
|
2023-01-11 13:30:52 +00:00
|
|
|
from homeassistant.components.hassio import HassioServiceInfo
|
2020-04-30 23:24:47 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
|
2023-02-08 12:01:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-07 19:28:18 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2023-01-31 02:32:52 +00:00
|
|
|
from . import VALID_NOISE_PSK
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
INVALID_NOISE_PSK = "lSYBYEjQI1bVL8s2Vask4YytGMj1f1epNtmoim2yuTM="
|
2023-07-02 14:29:45 +00:00
|
|
|
WRONG_NOISE_PSK = "GP+ciK+nVfTQ/gcz6uOdS+oKEdJgesU+jeu8Ssj2how="
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
@pytest.fixture(autouse=False)
|
2021-07-12 20:56:10 +00:00
|
|
|
def mock_setup_entry():
|
|
|
|
"""Mock setting up a config entry."""
|
|
|
|
with patch("homeassistant.components.esphome.async_setup_entry", return_value=True):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_connection_works(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2019-05-26 11:48:05 +00:00
|
|
|
"""Test we can finish a config flow."""
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-08-27 11:56:20 +00:00
|
|
|
data=None,
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["step_id"] == "user"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 80},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 80,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: "",
|
2023-01-11 20:28:31 +00:00
|
|
|
CONF_DEVICE_NAME: "test",
|
2021-09-20 07:02:17 +00:00
|
|
|
}
|
2023-06-26 01:18:21 +00:00
|
|
|
assert result["options"] == {
|
|
|
|
CONF_ALLOW_SERVICE_CALLS: DEFAULT_NEW_CONFIG_ALLOW_ALLOW_SERVICE_CALLS
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["title"] == "test"
|
2023-01-11 20:28:31 +00:00
|
|
|
assert result["result"].unique_id == "11:22:33:44:55:aa"
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.host == "127.0.0.1"
|
2018-12-17 00:29:32 +00:00
|
|
|
assert mock_client.port == 80
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.password == ""
|
2021-09-20 07:02:17 +00:00
|
|
|
assert mock_client.noise_psk is None
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_connection_updates_host(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2022-09-01 18:10:20 +00:00
|
|
|
"""Test setup up the same name updates the host."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "test.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
2023-01-11 20:28:31 +00:00
|
|
|
unique_id="11:22:33:44:55:aa",
|
2022-09-01 18:10:20 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data=None,
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-09-01 18:10:20 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 80},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-09-01 18:10:20 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_HOST] == "127.0.0.1"
|
|
|
|
|
|
|
|
|
2023-07-02 14:29:45 +00:00
|
|
|
async def test_user_sets_unique_id(
|
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
|
|
|
) -> None:
|
|
|
|
"""Test that the user flow sets the unique id."""
|
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-07-02 14:29:45 +00:00
|
|
|
hostname="test8266.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
},
|
|
|
|
type="mock_type",
|
|
|
|
)
|
|
|
|
discovery_result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert discovery_result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert discovery_result["step_id"] == "discovery_confirm"
|
|
|
|
|
|
|
|
discovery_result = await hass.config_entries.flow.async_configure(
|
|
|
|
discovery_result["flow_id"],
|
|
|
|
{},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert discovery_result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert discovery_result["data"] == {
|
|
|
|
CONF_HOST: "192.168.43.183",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: "",
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data=None,
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_resolve_error(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with IP resolve error."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-11-26 02:00:58 +00:00
|
|
|
"homeassistant.components.esphome.config_flow.APIConnectionError",
|
2021-09-20 07:02:17 +00:00
|
|
|
new_callable=lambda: ResolveAPIError,
|
2019-07-31 19:25:30 +00:00
|
|
|
) as exc:
|
2018-12-17 00:29:32 +00:00
|
|
|
mock_client.device_info.side_effect = exc
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "resolve_error"}
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2023-07-02 14:29:45 +00:00
|
|
|
async def test_user_causes_zeroconf_to_abort(
|
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
|
|
|
) -> None:
|
|
|
|
"""Test that the user flow sets the unique id and aborts the zeroconf flow."""
|
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-07-02 14:29:45 +00:00
|
|
|
hostname="test8266.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
},
|
|
|
|
type="mock_type",
|
|
|
|
)
|
|
|
|
discovery_result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert discovery_result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert discovery_result["step_id"] == "discovery_confirm"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data=None,
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: "",
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert not hass.config_entries.flow.async_progress_by_handler(DOMAIN)
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_connection_error(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with connection error."""
|
2021-09-20 07:02:17 +00:00
|
|
|
mock_client.device_info.side_effect = APIConnectionError
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "connection_error"}
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_with_password(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with password."""
|
2023-01-11 20:28:31 +00:00
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=True, name="test")
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "password1"}
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["data"] == {
|
2020-04-30 23:24:47 +00:00
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "password1",
|
2021-09-20 07:02:17 +00:00
|
|
|
CONF_NOISE_PSK: "",
|
2023-01-11 20:28:31 +00:00
|
|
|
CONF_DEVICE_NAME: "test",
|
2018-12-17 00:29:32 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.password == "password1"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_invalid_password(
|
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None
|
|
|
|
) -> None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with invalid password."""
|
2023-01-11 20:28:31 +00:00
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=True, name="test")
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
mock_client.connect.side_effect = InvalidAuthAPIError
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "invalid"}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
2020-10-09 12:28:54 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2019-01-05 15:00:07 +00:00
|
|
|
|
|
|
|
|
2023-07-02 14:29:45 +00:00
|
|
|
async def test_user_dashboard_has_wrong_key(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test user step with key from dashboard that is incorrect."""
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError,
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test",
|
|
|
|
mac_address="11:22:33:44:55:AA",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
|
|
|
return_value=WRONG_NOISE_PSK,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user_discovers_name_and_gets_key_from_dashboard(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test user step can discover the name and get the key from the dashboard."""
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError("Wrong key", "test"),
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test",
|
|
|
|
mac_address="11:22:33:44:55:AA",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-07-03 02:47:25 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"dashboard_exception",
|
|
|
|
[aiohttp.ClientError(), json.JSONDecodeError("test", "test", 0)],
|
|
|
|
)
|
2023-07-02 14:29:45 +00:00
|
|
|
async def test_user_discovers_name_and_gets_key_from_dashboard_fails(
|
|
|
|
hass: HomeAssistant,
|
2023-07-03 02:47:25 +00:00
|
|
|
dashboard_exception: Exception,
|
2023-07-02 14:29:45 +00:00
|
|
|
mock_client,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test user step can discover the name and get the key from the dashboard."""
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError("Wrong key", "test"),
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test",
|
2023-12-21 04:33:43 +00:00
|
|
|
mac_address="11:22:33:44:55:AA",
|
2023-07-02 14:29:45 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
2023-07-03 02:47:25 +00:00
|
|
|
side_effect=dashboard_exception,
|
2023-07-02 14:29:45 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user_discovers_name_and_dashboard_is_unavailable(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test user step can discover the name but the dashboard is unavailable."""
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError("Wrong key", "test"),
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test",
|
|
|
|
mac_address="11:22:33:44:55:AA",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"esphome_dashboard_api.ESPHomeDashboardAPI.get_devices",
|
2024-02-05 11:00:37 +00:00
|
|
|
side_effect=TimeoutError,
|
2023-07-02 14:29:45 +00:00
|
|
|
):
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-02 14:29:45 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_login_connection_error(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test user step with connection error on login attempt."""
|
2023-01-11 20:28:31 +00:00
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=True, name="test")
|
2021-09-20 07:02:17 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
|
|
|
|
mock_client.connect.side_effect = APIConnectionError
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "valid"}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
assert result["errors"] == {"base": "connection_error"}
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_discovery_initiation(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2019-01-05 15:00:07 +00:00
|
|
|
"""Test discovery importing works."""
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-01-11 20:28:31 +00:00
|
|
|
hostname="test.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
2022-12-11 03:26:42 +00:00
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:26:50 +00:00
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
flow = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow["flow_id"], user_input={}
|
|
|
|
)
|
2019-02-26 20:35:25 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-01-11 20:28:31 +00:00
|
|
|
assert result["title"] == "test"
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["data"][CONF_HOST] == "192.168.43.183"
|
|
|
|
assert result["data"][CONF_PORT] == 6053
|
|
|
|
|
|
|
|
assert result["result"]
|
2022-12-11 03:26:42 +00:00
|
|
|
assert result["result"].unique_id == "11:22:33:44:55:aa"
|
2019-01-05 15:00:07 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_discovery_no_mac(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2022-12-11 03:26:42 +00:00
|
|
|
"""Test discovery aborted if old ESPHome without mac in zeroconf."""
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2021-11-15 19:26:50 +00:00
|
|
|
hostname="test8266.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
2021-11-15 19:26:50 +00:00
|
|
|
properties={},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:26:50 +00:00
|
|
|
)
|
2022-12-11 03:26:42 +00:00
|
|
|
flow = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert flow["type"] is FlowResultType.ABORT
|
2022-12-11 03:26:42 +00:00
|
|
|
assert flow["reason"] == "mdns_missing_mac"
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
async def test_discovery_already_configured(
|
|
|
|
hass: HomeAssistant, mock_client: APIClient, mock_setup_entry: None
|
|
|
|
) -> None:
|
2022-12-11 03:26:42 +00:00
|
|
|
"""Test discovery aborts if already configured via hostname."""
|
2020-04-30 23:24:47 +00:00
|
|
|
entry = MockConfigEntry(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2022-12-11 03:26:42 +00:00
|
|
|
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
unique_id="11:22:33:44:55:aa",
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2021-11-15 19:26:50 +00:00
|
|
|
hostname="test8266.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
2022-12-11 03:26:42 +00:00
|
|
|
properties={"mac": "1122334455aa"},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:26:50 +00:00
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2019-05-30 16:48:58 +00:00
|
|
|
|
2019-06-17 16:19:40 +00:00
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
async def test_discovery_duplicate_data(
|
|
|
|
hass: HomeAssistant, mock_client: APIClient, mock_setup_entry: None
|
|
|
|
) -> None:
|
2019-06-17 16:19:40 +00:00
|
|
|
"""Test discovery aborts if same mDNS packet arrives."""
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-01-11 20:28:31 +00:00
|
|
|
hostname="test.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
2023-01-11 20:28:31 +00:00
|
|
|
properties={"address": "test.local", "mac": "1122334455aa"},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:26:50 +00:00
|
|
|
)
|
2019-06-17 16:19:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
|
2019-06-17 16:19:40 +00:00
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "discovery_confirm"
|
2019-06-17 16:19:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
|
2019-06-17 16:19:40 +00:00
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["reason"] == "already_in_progress"
|
|
|
|
|
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
async def test_discovery_updates_unique_id(
|
|
|
|
hass: HomeAssistant, mock_client: APIClient, mock_setup_entry: None
|
|
|
|
) -> None:
|
2020-04-30 23:24:47 +00:00
|
|
|
"""Test a duplicate discovery host aborts and updates existing entry."""
|
|
|
|
entry = MockConfigEntry(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
2022-12-11 03:26:42 +00:00
|
|
|
unique_id="11:22:33:44:55:aa",
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2021-11-15 19:26:50 +00:00
|
|
|
hostname="test8266.local.",
|
2021-11-23 15:32:58 +00:00
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
2022-12-11 03:26:42 +00:00
|
|
|
properties={"address": "test8266.local", "mac": "1122334455aa"},
|
2021-11-23 15:32:58 +00:00
|
|
|
type="mock_type",
|
2021-11-15 19:26:50 +00:00
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2022-12-11 03:26:42 +00:00
|
|
|
assert entry.unique_id == "11:22:33:44:55:aa"
|
2021-09-20 07:02:17 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_user_requires_psk(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test user step with requiring encryption key."""
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2023-07-02 14:29:45 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 2
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 2
|
|
|
|
assert len(mock_client.disconnect.mock_calls) == 2
|
2021-09-20 07:02:17 +00:00
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_encryption_key_valid_psk(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test encryption key step with valid key."""
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
2022-12-11 03:26:42 +00:00
|
|
|
mock_client.device_info = AsyncMock(
|
|
|
|
return_value=DeviceInfo(uses_password=False, name="test")
|
|
|
|
)
|
2021-09-20 07:02:17 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
2023-01-11 20:28:31 +00:00
|
|
|
CONF_DEVICE_NAME: "test",
|
2021-09-20 07:02:17 +00:00
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_encryption_key_invalid_psk(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test encryption key step with invalid key."""
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: INVALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
assert result["errors"] == {"base": "invalid_psk"}
|
|
|
|
assert mock_client.noise_psk == INVALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_reauth_initiation(
|
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None
|
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test reauth initiation shows form."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_reauth_confirm_valid(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test reauth initiation with valid PSK."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-01-11 20:28:31 +00:00
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test")
|
2021-09-20 07:02:17 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-01-18 16:59:55 +00:00
|
|
|
async def test_reauth_fixed_via_dashboard(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-11 20:28:31 +00:00
|
|
|
"""Test reauth fixed automatically via dashboard."""
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test")
|
|
|
|
|
2023-01-18 16:59:55 +00:00
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-01-18 20:15:37 +00:00
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
2023-01-18 16:59:55 +00:00
|
|
|
|
2023-01-11 20:28:31 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
) as mock_get_encryption_key:
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT, result
|
2023-01-11 20:28:31 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
assert len(mock_get_encryption_key.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-01-31 04:05:48 +00:00
|
|
|
async def test_reauth_fixed_via_dashboard_add_encryption_remove_password(
|
2023-02-13 08:45:11 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_config_entry,
|
2023-06-26 01:18:21 +00:00
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-27 02:45:51 +00:00
|
|
|
"""Test reauth fixed automatically via dashboard with password removed."""
|
2023-01-31 04:05:48 +00:00
|
|
|
mock_client.device_info.side_effect = (
|
|
|
|
InvalidAuthAPIError,
|
|
|
|
DeviceInfo(uses_password=False, name="test"),
|
2023-01-27 02:45:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
) as mock_get_encryption_key:
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
2023-01-31 04:05:48 +00:00
|
|
|
"entry_id": mock_config_entry.entry_id,
|
|
|
|
"unique_id": mock_config_entry.unique_id,
|
2023-01-27 02:45:51 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT, result
|
2023-01-27 02:45:51 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
2023-01-31 04:05:48 +00:00
|
|
|
assert mock_config_entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
assert mock_config_entry.data[CONF_PASSWORD] == ""
|
2023-01-27 02:45:51 +00:00
|
|
|
|
|
|
|
assert len(mock_get_encryption_key.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_reauth_fixed_via_remove_password(
|
2023-03-01 15:11:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_config_entry,
|
|
|
|
mock_dashboard,
|
2023-06-26 01:18:21 +00:00
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-31 04:05:48 +00:00
|
|
|
"""Test reauth fixed automatically by seeing password removed."""
|
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test")
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": mock_config_entry.entry_id,
|
|
|
|
"unique_id": mock_config_entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT, result
|
2023-01-31 04:05:48 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert mock_config_entry.data[CONF_PASSWORD] == ""
|
|
|
|
|
|
|
|
|
2023-01-30 21:46:42 +00:00
|
|
|
async def test_reauth_fixed_via_dashboard_at_confirm(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-30 21:46:42 +00:00
|
|
|
"""Test reauth fixed automatically via dashboard at confirm step."""
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test")
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM, result
|
2023-01-30 21:46:42 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"configuration": "test.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
) as mock_get_encryption_key:
|
|
|
|
# We just fetch the form
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT, result
|
2023-01-30 21:46:42 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
assert len(mock_get_encryption_key.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_reauth_confirm_invalid(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2021-09-20 07:02:17 +00:00
|
|
|
"""Test reauth initiation with invalid PSK."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: INVALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
assert result["errors"]
|
|
|
|
assert result["errors"]["base"] == "invalid_psk"
|
2022-06-30 17:07:03 +00:00
|
|
|
|
2022-12-11 03:26:42 +00:00
|
|
|
mock_client.device_info = AsyncMock(
|
|
|
|
return_value=DeviceInfo(uses_password=False, name="test")
|
|
|
|
)
|
2022-12-03 00:49:13 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-12-03 00:49:13 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
2023-02-13 08:45:11 +00:00
|
|
|
async def test_reauth_confirm_invalid_with_unique_id(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2022-12-03 00:49:13 +00:00
|
|
|
"""Test reauth initiation with invalid PSK."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
unique_id="test",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: INVALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-12-03 00:49:13 +00:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
assert result["errors"]
|
|
|
|
assert result["errors"]["base"] == "invalid_psk"
|
|
|
|
|
2022-12-11 03:26:42 +00:00
|
|
|
mock_client.device_info = AsyncMock(
|
|
|
|
return_value=DeviceInfo(uses_password=False, name="test")
|
|
|
|
)
|
2022-12-03 00:49:13 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-12-03 00:49:13 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
2022-06-30 17:07:03 +00:00
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
async def test_discovery_dhcp_updates_host(
|
|
|
|
hass: HomeAssistant, mock_client: APIClient, mock_setup_entry: None
|
|
|
|
) -> None:
|
2022-06-30 17:07:03 +00:00
|
|
|
"""Test dhcp discovery updates host and aborts."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
2022-12-11 03:26:42 +00:00
|
|
|
unique_id="11:22:33:44:55:aa",
|
2022-06-30 17:07:03 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
service_info = dhcp.DhcpServiceInfo(
|
|
|
|
ip="192.168.43.184",
|
|
|
|
hostname="test8266",
|
2022-12-11 03:26:42 +00:00
|
|
|
macaddress="1122334455aa",
|
2022-06-30 17:07:03 +00:00
|
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_DHCP}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-06-30 17:07:03 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
assert entry.data[CONF_HOST] == "192.168.43.184"
|
|
|
|
|
|
|
|
|
2023-06-26 01:18:21 +00:00
|
|
|
async def test_discovery_dhcp_no_changes(
|
|
|
|
hass: HomeAssistant, mock_client: APIClient, mock_setup_entry: None
|
|
|
|
) -> None:
|
2022-06-30 17:07:03 +00:00
|
|
|
"""Test dhcp discovery updates host and aborts."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_entry_data = MagicMock()
|
|
|
|
mock_entry_data.device_info.name = "test8266"
|
|
|
|
domain_data = DomainData.get(hass)
|
|
|
|
domain_data.set_entry_data(entry, mock_entry_data)
|
|
|
|
|
|
|
|
service_info = dhcp.DhcpServiceInfo(
|
|
|
|
ip="192.168.43.183",
|
|
|
|
hostname="test8266",
|
2024-02-14 19:57:37 +00:00
|
|
|
macaddress="000000000000",
|
2022-06-30 17:07:03 +00:00
|
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_DHCP}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-06-30 17:07:03 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
assert entry.data[CONF_HOST] == "192.168.43.183"
|
2023-01-11 13:30:52 +00:00
|
|
|
|
|
|
|
|
2023-03-01 15:11:21 +00:00
|
|
|
async def test_discovery_hassio(hass: HomeAssistant, mock_dashboard) -> None:
|
2023-01-11 13:30:52 +00:00
|
|
|
"""Test dashboard discovery."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
data=HassioServiceInfo(
|
|
|
|
config={
|
|
|
|
"host": "mock-esphome",
|
|
|
|
"port": 6052,
|
|
|
|
},
|
|
|
|
name="ESPHome",
|
|
|
|
slug="mock-slug",
|
2023-04-25 07:48:47 +00:00
|
|
|
uuid="1234",
|
2023-01-11 13:30:52 +00:00
|
|
|
),
|
|
|
|
context={"source": config_entries.SOURCE_HASSIO},
|
|
|
|
)
|
|
|
|
assert result
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2023-01-11 13:30:52 +00:00
|
|
|
assert result["reason"] == "service_received"
|
|
|
|
|
|
|
|
dash = dashboard.async_get_dashboard(hass)
|
|
|
|
assert dash is not None
|
|
|
|
assert dash.addon_slug == "mock-slug"
|
2023-01-11 20:28:31 +00:00
|
|
|
|
|
|
|
|
2023-01-18 16:59:55 +00:00
|
|
|
async def test_zeroconf_encryption_key_via_dashboard(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-11 20:28:31 +00:00
|
|
|
"""Test encryption key retrieved from dashboard."""
|
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-01-11 20:28:31 +00:00
|
|
|
hostname="test8266.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
},
|
|
|
|
type="mock_type",
|
|
|
|
)
|
|
|
|
flow = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert flow["type"] is FlowResultType.FORM
|
2023-01-11 20:28:31 +00:00
|
|
|
assert flow["step_id"] == "discovery_confirm"
|
|
|
|
|
2023-01-18 16:59:55 +00:00
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test8266",
|
|
|
|
"configuration": "test8266.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-01-18 20:15:37 +00:00
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
2023-01-11 20:28:31 +00:00
|
|
|
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test8266",
|
2023-07-02 14:29:45 +00:00
|
|
|
mac_address="11:22:33:44:55:AA",
|
2023-01-11 20:28:31 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
2023-07-09 22:57:04 +00:00
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
) as mock_get_encryption_key:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow["flow_id"], user_input={}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(mock_get_encryption_key.mock_calls) == 1
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-09 22:57:04 +00:00
|
|
|
assert result["title"] == "test8266"
|
|
|
|
assert result["data"][CONF_HOST] == "192.168.43.183"
|
|
|
|
assert result["data"][CONF_PORT] == 6053
|
|
|
|
assert result["data"][CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
assert result["result"]
|
|
|
|
assert result["result"].unique_id == "11:22:33:44:55:aa"
|
|
|
|
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_encryption_key_via_dashboard_with_api_encryption_prop(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test encryption key retrieved from dashboard with api_encryption property set."""
|
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-07-09 22:57:04 +00:00
|
|
|
hostname="test8266.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
"api_encryption": "any",
|
|
|
|
},
|
|
|
|
type="mock_type",
|
|
|
|
)
|
|
|
|
flow = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert flow["type"] is FlowResultType.FORM
|
2023-07-09 22:57:04 +00:00
|
|
|
assert flow["step_id"] == "discovery_confirm"
|
|
|
|
|
|
|
|
mock_dashboard["configured"].append(
|
|
|
|
{
|
|
|
|
"name": "test8266",
|
|
|
|
"configuration": "test8266.yaml",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test8266",
|
|
|
|
mac_address="11:22:33:44:55:AA",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
|
2023-01-11 20:28:31 +00:00
|
|
|
return_value=VALID_NOISE_PSK,
|
|
|
|
) as mock_get_encryption_key:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow["flow_id"], user_input={}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(mock_get_encryption_key.mock_calls) == 1
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-01-11 20:28:31 +00:00
|
|
|
assert result["title"] == "test8266"
|
|
|
|
assert result["data"][CONF_HOST] == "192.168.43.183"
|
|
|
|
assert result["data"][CONF_PORT] == 6053
|
|
|
|
assert result["data"][CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
assert result["result"]
|
|
|
|
assert result["result"].unique_id == "11:22:33:44:55:aa"
|
|
|
|
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_zeroconf_no_encryption_key_via_dashboard(
|
2023-06-26 01:18:21 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_dashboard,
|
|
|
|
mock_setup_entry: None,
|
2023-02-13 08:45:11 +00:00
|
|
|
) -> None:
|
2023-01-11 20:28:31 +00:00
|
|
|
"""Test encryption key not retrieved from dashboard."""
|
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address("192.168.43.183"),
|
|
|
|
ip_addresses=[ip_address("192.168.43.183")],
|
2023-01-11 20:28:31 +00:00
|
|
|
hostname="test8266.local.",
|
|
|
|
name="mock_name",
|
|
|
|
port=6053,
|
|
|
|
properties={
|
|
|
|
"mac": "1122334455aa",
|
|
|
|
},
|
|
|
|
type="mock_type",
|
|
|
|
)
|
|
|
|
flow = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert flow["type"] is FlowResultType.FORM
|
2023-01-11 20:28:31 +00:00
|
|
|
assert flow["step_id"] == "discovery_confirm"
|
|
|
|
|
2023-01-18 20:15:37 +00:00
|
|
|
await dashboard.async_get_dashboard(hass).async_refresh()
|
2023-01-11 20:28:31 +00:00
|
|
|
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
2023-01-18 16:59:55 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow["flow_id"], user_input={}
|
|
|
|
)
|
2023-01-11 20:28:31 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-01-11 20:28:31 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
2023-06-26 01:18:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("option_value", [True, False])
|
|
|
|
async def test_option_flow(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
option_value: bool,
|
|
|
|
mock_client: APIClient,
|
|
|
|
mock_generic_device_entry,
|
|
|
|
) -> None:
|
|
|
|
"""Test config flow options."""
|
|
|
|
entry = await mock_generic_device_entry(
|
|
|
|
mock_client=mock_client,
|
|
|
|
entity_info=[],
|
|
|
|
user_service=[],
|
|
|
|
states=[],
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-06-26 01:18:21 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
assert result["data_schema"]({}) == {
|
|
|
|
CONF_ALLOW_SERVICE_CALLS: DEFAULT_NEW_CONFIG_ALLOW_ALLOW_SERVICE_CALLS
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.esphome.async_setup_entry", return_value=True
|
|
|
|
) as mock_reload:
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_ALLOW_SERVICE_CALLS: option_value,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-06-26 01:18:21 +00:00
|
|
|
assert result["data"] == {CONF_ALLOW_SERVICE_CALLS: option_value}
|
|
|
|
assert len(mock_reload.mock_calls) == int(option_value)
|
2023-07-11 18:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_user_discovers_name_no_dashboard(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_client,
|
|
|
|
mock_zeroconf: None,
|
|
|
|
mock_setup_entry: None,
|
|
|
|
) -> None:
|
|
|
|
"""Test user step can discover the name and the there is not dashboard."""
|
|
|
|
mock_client.device_info.side_effect = [
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError("Wrong key", "test"),
|
|
|
|
DeviceInfo(
|
|
|
|
uses_password=False,
|
|
|
|
name="test",
|
|
|
|
mac_address="11:22:33:44:55:AA",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={"source": config_entries.SOURCE_USER},
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-07-11 18:11:51 +00:00
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-07-11 18:11:51 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
CONF_DEVICE_NAME: "test",
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|