mirror of
https://github.com/home-assistant/core.git
synced 2026-11-06 13:41:27 -04:00
Add zeroconf support to Homevolt (#162897)
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
4d0d5d6817
commit
82fb3c35dc
@@ -2,19 +2,31 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ipaddress import IPv4Address
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from homevolt import HomevoltAuthenticationError, HomevoltConnectionError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.homevolt.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
DISCOVERY_INFO = ZeroconfServiceInfo(
|
||||
ip_address=IPv4Address("192.168.1.123"),
|
||||
ip_addresses=[IPv4Address("192.168.1.123")],
|
||||
port=80,
|
||||
hostname="homevolt.local.",
|
||||
type="_http._tcp.local.",
|
||||
name="homevolt._http._tcp.local.",
|
||||
properties={},
|
||||
)
|
||||
|
||||
|
||||
async def test_full_flow_success(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_homevolt_client: MagicMock
|
||||
@@ -305,3 +317,157 @@ async def test_reauth_flow_errors(
|
||||
CONF_HOST: "127.0.0.1",
|
||||
CONF_PASSWORD: "correct-password",
|
||||
}
|
||||
|
||||
|
||||
async def test_zeroconf_confirm_flow_success(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_homevolt_client: MagicMock
|
||||
) -> None:
|
||||
"""Test zeroconf flow shows confirm step before creating entry."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["description_placeholders"] == {"host": "192.168.1.123"}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Homevolt"
|
||||
assert result["data"] == {CONF_HOST: "192.168.1.123", CONF_PASSWORD: None}
|
||||
assert result["result"].unique_id == "40580137858664"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_zeroconf_duplicate_aborts(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_homevolt_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test zeroconf flow aborts when unique id is already configured."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_config_entry.data[CONF_HOST] == "192.168.1.123"
|
||||
|
||||
|
||||
async def test_zeroconf_confirm_with_password_success(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_homevolt_client: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""Test zeroconf confirm collects password and creates entry when auth is required."""
|
||||
|
||||
mock_homevolt_client.update_info.side_effect = HomevoltAuthenticationError
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["description_placeholders"] == {"host": "192.168.1.123"}
|
||||
|
||||
mock_homevolt_client.update_info.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "test-password"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Homevolt"
|
||||
assert result["data"] == {
|
||||
CONF_HOST: "192.168.1.123",
|
||||
CONF_PASSWORD: "test-password",
|
||||
}
|
||||
assert result["result"].unique_id == "40580137858664"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_zeroconf_confirm_with_password_invalid_then_success(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_homevolt_client: MagicMock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""Test zeroconf confirm shows error on invalid password, then succeeds."""
|
||||
|
||||
mock_homevolt_client.update_info.side_effect = HomevoltAuthenticationError
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "wrong-password"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
mock_homevolt_client.update_info.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "correct-password"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Homevolt"
|
||||
assert result["data"] == {
|
||||
CONF_HOST: "192.168.1.123",
|
||||
CONF_PASSWORD: "correct-password",
|
||||
}
|
||||
assert result["result"].unique_id == "40580137858664"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "expected_reason"),
|
||||
[
|
||||
(HomevoltConnectionError, "cannot_connect"),
|
||||
(Exception("Unexpected error"), "unknown"),
|
||||
],
|
||||
ids=["connection_error", "unknown_error"],
|
||||
)
|
||||
async def test_zeroconf_error_aborts(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_homevolt_client: MagicMock,
|
||||
exception: Exception,
|
||||
expected_reason: str,
|
||||
) -> None:
|
||||
"""Test zeroconf flow aborts on error during discovery."""
|
||||
mock_homevolt_client.update_info.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == expected_reason
|
||||
|
||||
Reference in New Issue
Block a user