Files
ha-solaredge-optimizers/tests/test_config_flow.py
T

126 lines
4.3 KiB
Python

"""Tests for the SolarEdge Optimizers config flow."""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
from homeassistant import config_entries
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
from pytest_homeassistant_custom_component.common import MockConfigEntry
from custom_components.solaredge_optimizers.api import parse_snapshot
from custom_components.solaredge_optimizers.const import (
CONF_AREA_MAPPING,
CONF_SCAN_INTERVAL,
DOMAIN,
)
from .sample_data import SAMPLE_PAYLOAD
async def test_user_flow(hass: HomeAssistant) -> None:
"""Configure the integration manually."""
with patch(
"custom_components.solaredge_optimizers.config_flow.validate_input",
AsyncMock(return_value=("http://app:8099", parse_snapshot(SAMPLE_PAYLOAD))),
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "SolarEdge Site 4886699"
assert result["data"][CONF_URL] == "http://app:8099"
async def test_hassio_discovery_flow(hass: HomeAssistant) -> None:
"""Configure the integration from App discovery."""
discovery = HassioServiceInfo(
config={"host": "abc-solaredge-optimizer-data", "port": 8099},
name="SolarEdge Optimizer Data",
slug="abc_solaredge_optimizer_data",
uuid="discovery-uuid",
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=discovery,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "hassio_confirm"
with patch(
"custom_components.solaredge_optimizers.config_flow.validate_input",
AsyncMock(
return_value=(
"http://abc-solaredge-optimizer-data:8099",
parse_snapshot(SAMPLE_PAYLOAD),
)
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_URL] == "http://abc-solaredge-optimizer-data:8099"
async def test_options_flow_configures_areas(hass: HomeAssistant) -> None:
"""Configure area mappings through the options flow."""
entry = MockConfigEntry(
domain=DOMAIN,
title="SolarEdge Site 4886699",
unique_id="4886699",
data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600},
version=2,
)
entry.runtime_data = SimpleNamespace(data=parse_snapshot(SAMPLE_PAYLOAD))
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_AREA_MAPPING: {"west": ["1.1.1", "1.1.2"]}},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert entry.options[CONF_AREA_MAPPING] == {"west": ["1.1.1", "1.1.2"]}
async def test_options_flow_rejects_duplicate_optimizer(hass: HomeAssistant) -> None:
"""Reject assigning one optimizer to multiple areas."""
entry = MockConfigEntry(
domain=DOMAIN,
title="SolarEdge Site 4886699",
unique_id="4886699",
data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600},
version=2,
)
entry.runtime_data = SimpleNamespace(data=parse_snapshot(SAMPLE_PAYLOAD))
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_AREA_MAPPING: {
"west": ["1.1.1"],
"roof": ["1.1.1"],
}
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "duplicate_optimizer"}