hass-core/tests/components/hydrawise/test_config_flow.py
David Knowles f8a8fe760d
Add config flow to Hydrawise (#95589)
* Add config flow to Hydrawise

* Raise an issue when a YAML config is detected

* Add a test for YAML import

* Add missing __init__.py

* Update CODEOWNERS

* Update requirements_test_all.txt

* Add config flow data to strings.json

* Hande scan_interval not being in YAML on import

* Fix requirements

* Update deprecation dates

* Update requirements_test_all.txt

* Changes from review

* Update homeassistant/components/hydrawise/__init__.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Add already_configured to strings.json

* Add back setup_platform functions

* Apply suggestions from code review

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Add back setup_platform

* Update requirements_test_all.txt

* Run black on hydrawise/*.py

* Add missing import of HOMEASSISTANT_DOMAIN

* Use more specific errors in config flow

* Add additional tests

* Update config flow to use pydrawise.legacy

* Re-work YAML deprecation issues

* Revert some changes to binary_sensor, as requested in review

* Changes requested during review

* Apply suggestions from code review

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Remove unused STE_USER_DATA_SCHEMA

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update comment in setup_platform

* Re-work the config flow again

* Apply suggestions from code review

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update tests

* Add back the _default_watering_timer attribute

* Bump deprecation dates

* Update requirements_test_all.txt

* Update CODEOWNERS

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-24 00:03:07 +02:00

213 lines
7.3 KiB
Python

"""Test the Hydrawise config flow."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from requests.exceptions import ConnectTimeout, HTTPError
from homeassistant import config_entries
from homeassistant.components.hydrawise.const import DOMAIN
from homeassistant.const import CONF_API_KEY, CONF_SCAN_INTERVAL
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
import homeassistant.helpers.issue_registry as ir
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_form(
mock_api: MagicMock, hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {"api_key": "abc123"}
)
mock_api.return_value.customer_id = 12345
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Hydrawise"
assert result2["data"] == {"api_key": "abc123"}
assert len(mock_setup_entry.mock_calls) == 1
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_form_api_error(mock_api: MagicMock, hass: HomeAssistant) -> None:
"""Test we handle API errors."""
mock_api.side_effect = HTTPError
init_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
data = {"api_key": "abc123"}
result = await hass.config_entries.flow.async_configure(
init_result["flow_id"], data
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "cannot_connect"}
mock_api.side_effect = None
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], data)
assert result2["type"] == FlowResultType.CREATE_ENTRY
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_form_connect_timeout(mock_api: MagicMock, hass: HomeAssistant) -> None:
"""Test we handle API errors."""
mock_api.side_effect = ConnectTimeout
init_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
data = {"api_key": "abc123"}
result = await hass.config_entries.flow.async_configure(
init_result["flow_id"], data
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "timeout_connect"}
mock_api.side_effect = None
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], data)
assert result2["type"] == FlowResultType.CREATE_ENTRY
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_flow_import_success(mock_api: MagicMock, hass: HomeAssistant) -> None:
"""Test that we can import a YAML config."""
mock_api.return_value.status = "All good!"
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "__api_key__",
CONF_SCAN_INTERVAL: 120,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Hydrawise"
assert result["data"] == {
CONF_API_KEY: "__api_key__",
}
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, "deprecated_yaml_hydrawise"
)
assert issue.translation_key == "deprecated_yaml"
@patch("pydrawise.legacy.LegacyHydrawise", side_effect=HTTPError)
async def test_flow_import_api_error(mock_api: MagicMock, hass: HomeAssistant) -> None:
"""Test that we handle API errors on YAML import."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "__api_key__",
CONF_SCAN_INTERVAL: 120,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "cannot_connect"
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
DOMAIN, "deprecated_yaml_import_issue_cannot_connect"
)
assert issue.translation_key == "deprecated_yaml_import_issue"
@patch("pydrawise.legacy.LegacyHydrawise", side_effect=ConnectTimeout)
async def test_flow_import_connect_timeout(
mock_api: MagicMock, hass: HomeAssistant
) -> None:
"""Test that we handle connection timeouts on YAML import."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "__api_key__",
CONF_SCAN_INTERVAL: 120,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "timeout_connect"
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
DOMAIN, "deprecated_yaml_import_issue_timeout_connect"
)
assert issue.translation_key == "deprecated_yaml_import_issue"
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_flow_import_no_status(mock_api: MagicMock, hass: HomeAssistant) -> None:
"""Test we handle a lack of API status on YAML import."""
mock_api.return_value.status = None
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "__api_key__",
CONF_SCAN_INTERVAL: 120,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "unknown"
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
DOMAIN, "deprecated_yaml_import_issue_unknown"
)
assert issue.translation_key == "deprecated_yaml_import_issue"
@patch("pydrawise.legacy.LegacyHydrawise")
async def test_flow_import_already_imported(
mock_api: MagicMock, hass: HomeAssistant
) -> None:
"""Test that we can handle a YAML config already imported."""
mock_config_entry = MockConfigEntry(
title="Hydrawise",
domain=DOMAIN,
data={
CONF_API_KEY: "__api_key__",
},
unique_id="hydrawise-CUSTOMER_ID",
)
mock_config_entry.add_to_hass(hass)
mock_api.return_value.customer_id = "CUSTOMER_ID"
mock_api.return_value.status = "All good!"
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_API_KEY: "__api_key__",
CONF_SCAN_INTERVAL: 120,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result.get("reason") == "already_configured"
issue_registry = ir.async_get(hass)
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, "deprecated_yaml_hydrawise"
)
assert issue.translation_key == "deprecated_yaml"