Improve Solar.Forecast configuration flow tests (#133077)

This commit is contained in:
Franck Nijhof 2024-12-12 21:19:05 +01:00 committed by GitHub
parent aa7e024853
commit 61b1b50c34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,8 @@
from unittest.mock import AsyncMock
import pytest
from homeassistant.components.forecast_solar.const import (
CONF_AZIMUTH,
CONF_DAMPING_EVENING,
@ -25,10 +27,10 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_NAME: "Name",
@ -40,13 +42,16 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
},
)
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert result2.get("title") == "Name"
assert result2.get("data") == {
assert result["type"] is FlowResultType.CREATE_ENTRY
config_entry = result["result"]
assert config_entry.title == "Name"
assert config_entry.unique_id is None
assert config_entry.data == {
CONF_LATITUDE: 52.42,
CONF_LONGITUDE: 4.42,
}
assert result2.get("options") == {
assert config_entry.options == {
CONF_AZIMUTH: 142,
CONF_DECLINATION: 42,
CONF_MODULES_POWER: 4242,
@ -55,9 +60,9 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.usefixtures("mock_setup_entry")
async def test_options_flow_invalid_api(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test options config flow when API key is invalid."""
@ -67,10 +72,10 @@ async def test_options_flow_invalid_api(
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "init"
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
result2 = await hass.config_entries.options.async_configure(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "solarPOWER!",
@ -84,27 +89,11 @@ async def test_options_flow_invalid_api(
)
await hass.async_block_till_done()
assert result2.get("type") is FlowResultType.FORM
assert result2["errors"] == {CONF_API_KEY: "invalid_api_key"}
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
async def test_options_flow(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test config flow options."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "init"
# With the API key
result2 = await hass.config_entries.options.async_configure(
# Ensure we can recover from this error
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "SolarForecast150",
@ -118,8 +107,8 @@ async def test_options_flow(
)
await hass.async_block_till_done()
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert result2.get("data") == {
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_API_KEY: "SolarForecast150",
CONF_DECLINATION: 21,
CONF_AZIMUTH: 22,
@ -130,9 +119,9 @@ async def test_options_flow(
}
async def test_options_flow_without_key(
@pytest.mark.usefixtures("mock_setup_entry")
async def test_options_flow(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test config flow options."""
@ -142,11 +131,53 @@ async def test_options_flow_without_key(
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "init"
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
# With the API key
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "SolarForecast150",
CONF_DECLINATION: 21,
CONF_AZIMUTH: 22,
CONF_MODULES_POWER: 2122,
CONF_DAMPING_MORNING: 0.25,
CONF_DAMPING_EVENING: 0.25,
CONF_INVERTER_SIZE: 2000,
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_API_KEY: "SolarForecast150",
CONF_DECLINATION: 21,
CONF_AZIMUTH: 22,
CONF_MODULES_POWER: 2122,
CONF_DAMPING_MORNING: 0.25,
CONF_DAMPING_EVENING: 0.25,
CONF_INVERTER_SIZE: 2000,
}
@pytest.mark.usefixtures("mock_setup_entry")
async def test_options_flow_without_key(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test config flow options."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
# Without the API key
result2 = await hass.config_entries.options.async_configure(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_DECLINATION: 21,
@ -159,8 +190,8 @@ async def test_options_flow_without_key(
)
await hass.async_block_till_done()
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert result2.get("data") == {
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_API_KEY: None,
CONF_DECLINATION: 21,
CONF_AZIMUTH: 22,