2018-12-16 15:19:18 +00:00
|
|
|
"""Tests for the Daikin config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2023-09-19 16:58:46 +00:00
|
|
|
from ipaddress import ip_address
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import PropertyMock, patch
|
2018-12-16 15:19:18 +00:00
|
|
|
|
2021-10-06 08:07:30 +00:00
|
|
|
from aiohttp import ClientError, web_exceptions
|
2021-10-11 11:11:02 +00:00
|
|
|
from pydaikin.exceptions import DaikinException
|
2018-12-16 15:19:18 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-11-02 17:27:31 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2021-02-25 11:19:21 +00:00
|
|
|
from homeassistant.components.daikin.const import KEY_MAC
|
2021-05-04 21:23:59 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
2021-10-06 08:07:30 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD
|
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-16 15:19:18 +00:00
|
|
|
|
2019-12-05 06:47:40 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-12-16 15:19:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MAC = "AABBCCDDEEFF"
|
|
|
|
HOST = "127.0.0.1"
|
2018-12-16 15:19:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_daikin():
|
2019-04-05 06:48:41 +00:00
|
|
|
"""Mock pydaikin."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-05-06 11:59:07 +00:00
|
|
|
async def mock_daikin_factory(*args, **kwargs):
|
2019-04-05 06:48:41 +00:00
|
|
|
"""Mock the init function in pydaikin."""
|
2020-05-06 11:59:07 +00:00
|
|
|
return Appliance
|
2019-04-05 06:48:41 +00:00
|
|
|
|
2019-12-05 06:47:40 +00:00
|
|
|
with patch("homeassistant.components.daikin.config_flow.Appliance") as Appliance:
|
2020-05-06 11:59:07 +00:00
|
|
|
type(Appliance).mac = PropertyMock(return_value="AABBCCDDEEFF")
|
|
|
|
Appliance.factory.side_effect = mock_daikin_factory
|
2019-12-05 06:47:40 +00:00
|
|
|
yield Appliance
|
2018-12-16 15:19:18 +00:00
|
|
|
|
|
|
|
|
2020-06-12 15:25:18 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_daikin_discovery():
|
|
|
|
"""Mock pydaikin Discovery."""
|
|
|
|
with patch("homeassistant.components.daikin.config_flow.Discovery") as Discovery:
|
2020-06-17 14:53:29 +00:00
|
|
|
Discovery().poll.return_value = {
|
|
|
|
"127.0.01": {"mac": "AABBCCDDEEFF", "id": "test"}
|
2020-06-20 20:59:25 +00:00
|
|
|
}.values()
|
2020-06-12 15:25:18 +00:00
|
|
|
yield Discovery
|
|
|
|
|
|
|
|
|
2023-02-11 07:26:13 +00:00
|
|
|
async def test_user(hass: HomeAssistant, mock_daikin) -> None:
|
2018-12-16 15:19:18 +00:00
|
|
|
"""Test user config."""
|
2020-05-18 14:57:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER},
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
2018-12-16 15:19:18 +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"
|
2018-12-16 15:19:18 +00:00
|
|
|
|
2020-05-18 14:57:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST},
|
2020-05-18 14:57:52 +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["title"] == HOST
|
|
|
|
assert result["data"][CONF_HOST] == HOST
|
|
|
|
assert result["data"][KEY_MAC] == MAC
|
2018-12-16 15:19:18 +00:00
|
|
|
|
|
|
|
|
2023-02-11 07:26:13 +00:00
|
|
|
async def test_abort_if_already_setup(hass: HomeAssistant, mock_daikin) -> None:
|
2018-12-16 15:19:18 +00:00
|
|
|
"""Test we abort if Daikin is already setup."""
|
2020-05-18 14:57:52 +00:00
|
|
|
MockConfigEntry(domain="daikin", unique_id=MAC).add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, KEY_MAC: MAC},
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
2018-12-16 15:19:18 +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"
|
2018-12-16 15:19:18 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("s_effect", "reason"),
|
2020-05-11 17:39:20 +00:00
|
|
|
[
|
2024-02-05 10:31:33 +00:00
|
|
|
(TimeoutError, "cannot_connect"),
|
2021-10-06 08:07:30 +00:00
|
|
|
(ClientError, "cannot_connect"),
|
|
|
|
(web_exceptions.HTTPForbidden, "invalid_auth"),
|
2021-10-11 11:11:02 +00:00
|
|
|
(DaikinException, "unknown"),
|
2020-10-08 19:25:54 +00:00
|
|
|
(Exception, "unknown"),
|
2020-05-11 17:39:20 +00:00
|
|
|
],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2023-02-11 07:26:13 +00:00
|
|
|
async def test_device_abort(hass: HomeAssistant, mock_daikin, s_effect, reason) -> None:
|
2018-12-16 15:19:18 +00:00
|
|
|
"""Test device abort."""
|
2020-05-06 11:59:07 +00:00
|
|
|
mock_daikin.factory.side_effect = s_effect
|
2018-12-16 15:19:18 +00:00
|
|
|
|
2020-05-18 14:57:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, KEY_MAC: MAC},
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-05-11 17:39:20 +00:00
|
|
|
assert result["errors"] == {"base": reason}
|
|
|
|
assert result["step_id"] == "user"
|
2020-05-18 14:57:52 +00:00
|
|
|
|
|
|
|
|
2023-02-08 12:01:44 +00:00
|
|
|
async def test_api_password_abort(hass: HomeAssistant) -> None:
|
2021-10-06 08:07:30 +00:00
|
|
|
"""Test device abort."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, CONF_API_KEY: "aa", CONF_PASSWORD: "aa"},
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-10-06 08:07:30 +00:00
|
|
|
assert result["errors"] == {"base": "api_password"}
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
|
2020-05-18 14:57:52 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("source", "data", "unique_id"),
|
2020-05-28 23:30:01 +00:00
|
|
|
[
|
2021-11-23 15:32:58 +00:00
|
|
|
(
|
|
|
|
SOURCE_ZEROCONF,
|
|
|
|
zeroconf.ZeroconfServiceInfo(
|
2023-09-19 16:58:46 +00:00
|
|
|
ip_address=ip_address(HOST),
|
|
|
|
ip_addresses=[ip_address(HOST)],
|
2021-11-23 15:32:58 +00:00
|
|
|
hostname="mock_hostname",
|
|
|
|
name="mock_name",
|
|
|
|
port=None,
|
|
|
|
properties={},
|
|
|
|
type="mock_type",
|
|
|
|
),
|
|
|
|
MAC,
|
|
|
|
),
|
2020-05-28 23:30:01 +00:00
|
|
|
],
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
2020-06-12 15:25:18 +00:00
|
|
|
async def test_discovery_zeroconf(
|
2023-02-11 07:26:13 +00:00
|
|
|
hass: HomeAssistant, mock_daikin, mock_daikin_discovery, source, data, unique_id
|
|
|
|
) -> None:
|
2020-05-18 14:57:52 +00:00
|
|
|
"""Test discovery/zeroconf step."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": source},
|
|
|
|
data=data,
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-05-18 14:57:52 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
MockConfigEntry(domain="daikin", unique_id=unique_id).add_to_hass(hass)
|
2020-06-10 13:04:59 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"daikin",
|
|
|
|
context={"source": SOURCE_USER, "unique_id": unique_id},
|
|
|
|
data={CONF_HOST: HOST},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-06-10 13:04:59 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
2020-05-18 14:57:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"daikin",
|
|
|
|
context={"source": source},
|
|
|
|
data=data,
|
2020-05-18 14:57:52 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-05-18 14:57:52 +00:00
|
|
|
assert result["reason"] == "already_in_progress"
|