mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Improve Solar.Forecast configuration flow tests (#133077)
This commit is contained in:
parent
aa7e024853
commit
61b1b50c34
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.forecast_solar.const import (
|
from homeassistant.components.forecast_solar.const import (
|
||||||
CONF_AZIMUTH,
|
CONF_AZIMUTH,
|
||||||
CONF_DAMPING_EVENING,
|
CONF_DAMPING_EVENING,
|
||||||
@ -25,10 +27,10 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
|
|||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result.get("type") is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result.get("step_id") == "user"
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_NAME: "Name",
|
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 result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2.get("title") == "Name"
|
|
||||||
assert result2.get("data") == {
|
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_LATITUDE: 52.42,
|
||||||
CONF_LONGITUDE: 4.42,
|
CONF_LONGITUDE: 4.42,
|
||||||
}
|
}
|
||||||
assert result2.get("options") == {
|
assert config_entry.options == {
|
||||||
CONF_AZIMUTH: 142,
|
CONF_AZIMUTH: 142,
|
||||||
CONF_DECLINATION: 42,
|
CONF_DECLINATION: 42,
|
||||||
CONF_MODULES_POWER: 4242,
|
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
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_setup_entry")
|
||||||
async def test_options_flow_invalid_api(
|
async def test_options_flow_invalid_api(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_setup_entry: AsyncMock,
|
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test options config flow when API key is invalid."""
|
"""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)
|
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
|
||||||
|
|
||||||
assert result.get("type") is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result.get("step_id") == "init"
|
assert result["step_id"] == "init"
|
||||||
|
|
||||||
result2 = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_API_KEY: "solarPOWER!",
|
CONF_API_KEY: "solarPOWER!",
|
||||||
@ -84,27 +89,11 @@ async def test_options_flow_invalid_api(
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2.get("type") is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||||
|
|
||||||
|
# Ensure we can recover from this error
|
||||||
async def test_options_flow(
|
result = await hass.config_entries.options.async_configure(
|
||||||
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(
|
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_API_KEY: "SolarForecast150",
|
CONF_API_KEY: "SolarForecast150",
|
||||||
@ -118,8 +107,8 @@ async def test_options_flow(
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2.get("type") is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2.get("data") == {
|
assert result["data"] == {
|
||||||
CONF_API_KEY: "SolarForecast150",
|
CONF_API_KEY: "SolarForecast150",
|
||||||
CONF_DECLINATION: 21,
|
CONF_DECLINATION: 21,
|
||||||
CONF_AZIMUTH: 22,
|
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,
|
hass: HomeAssistant,
|
||||||
mock_setup_entry: AsyncMock,
|
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test config flow options."""
|
"""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)
|
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
|
||||||
|
|
||||||
assert result.get("type") is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result.get("step_id") == "init"
|
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
|
# Without the API key
|
||||||
result2 = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_DECLINATION: 21,
|
CONF_DECLINATION: 21,
|
||||||
@ -159,8 +190,8 @@ async def test_options_flow_without_key(
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result2.get("type") is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2.get("data") == {
|
assert result["data"] == {
|
||||||
CONF_API_KEY: None,
|
CONF_API_KEY: None,
|
||||||
CONF_DECLINATION: 21,
|
CONF_DECLINATION: 21,
|
||||||
CONF_AZIMUTH: 22,
|
CONF_AZIMUTH: 22,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user