Skip to content

Commit

Permalink
Improve Solar.Forecast configuration flow tests (#133077)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Dec 12, 2024
1 parent aa7e024 commit 61b1b50
Showing 1 changed file with 56 additions and 25 deletions.
81 changes: 56 additions & 25 deletions tests/components/forecast_solar/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from unittest.mock import AsyncMock

import pytest

from homeassistant.components.forecast_solar.const import (
CONF_AZIMUTH,
CONF_DAMPING_EVENING,
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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."""
Expand All @@ -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!",
Expand All @@ -84,13 +89,39 @@ 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"}

# Ensure we can recover from this error
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(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test config flow options."""
Expand All @@ -100,11 +131,11 @@ async def test_options_flow(

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
result2 = await hass.config_entries.options.async_configure(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "SolarForecast150",
Expand All @@ -118,8 +149,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,
Expand All @@ -130,9 +161,9 @@ async def test_options_flow(
}


@pytest.mark.usefixtures("mock_setup_entry")
async def test_options_flow_without_key(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test config flow options."""
Expand All @@ -142,11 +173,11 @@ 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"

# 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,
Expand All @@ -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,
Expand Down

0 comments on commit 61b1b50

Please sign in to comment.