Add configurable optimizer area sensors
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Tests for optimizer area mappings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from custom_components.solaredge_optimizers.areas import (
|
||||
AreaMappingError,
|
||||
default_area_mapping,
|
||||
parse_area_mapping,
|
||||
serialize_area_mapping,
|
||||
)
|
||||
|
||||
|
||||
def test_site_default_mapping() -> None:
|
||||
"""Load the six known areas for site 4886699."""
|
||||
mapping = default_area_mapping("4886699")
|
||||
|
||||
assert set(mapping) == {"west1", "west2", "gaube", "süd", "ost1", "ost2"}
|
||||
assert sum(len(optimizer_ids) for optimizer_ids in mapping.values()) == 34
|
||||
|
||||
|
||||
def test_parse_and_serialize_mapping() -> None:
|
||||
"""Normalize and serialize a valid mapping."""
|
||||
mapping = parse_area_mapping(
|
||||
{"west": ["1.1.1", "1.1.2"]},
|
||||
{"1.1.1", "1.1.2"},
|
||||
)
|
||||
|
||||
assert mapping == {"west": ("1.1.1", "1.1.2")}
|
||||
assert serialize_area_mapping(mapping) == {"west": ["1.1.1", "1.1.2"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mapping", "error"),
|
||||
[
|
||||
({"west": []}, "empty_area"),
|
||||
({"west": ["1.1.99"]}, "unknown_optimizer"),
|
||||
(
|
||||
{"west": ["1.1.1"], "roof": ["1.1.1"]},
|
||||
"duplicate_optimizer",
|
||||
),
|
||||
({"süd": ["1.1.1"], "sud": ["1.1.2"]}, "duplicate_area"),
|
||||
],
|
||||
)
|
||||
def test_reject_invalid_mapping(mapping: object, error: str) -> None:
|
||||
"""Reject invalid mappings with a translated error key."""
|
||||
with pytest.raises(AreaMappingError) as raised:
|
||||
parse_area_mapping(mapping, {"1.1.1", "1.1.2"})
|
||||
|
||||
assert raised.value.translation_key == error
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
@@ -9,9 +10,11 @@ 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,
|
||||
)
|
||||
@@ -68,3 +71,55 @@ async def test_hassio_discovery_flow(hass: HomeAssistant) -> None:
|
||||
|
||||
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"}
|
||||
|
||||
+86
-6
@@ -2,16 +2,21 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from homeassistant.const import CONF_URL
|
||||
from homeassistant.const import CONF_URL, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
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_SCAN_INTERVAL, DOMAIN
|
||||
from custom_components.solaredge_optimizers.const import (
|
||||
CONF_AREA_MAPPING,
|
||||
CONF_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
from .sample_data import SAMPLE_PAYLOAD
|
||||
|
||||
@@ -23,9 +28,18 @@ async def test_optimizer_sensors(hass: HomeAssistant) -> None:
|
||||
title="SolarEdge Site 4886699",
|
||||
unique_id="4886699",
|
||||
data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600},
|
||||
options={CONF_AREA_MAPPING: {"west": ["1.1.1", "1.1.2"]}},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
device_registry = dr.async_get(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
stale_area_entity = entity_registry.async_get_or_create(
|
||||
"sensor",
|
||||
DOMAIN,
|
||||
"4886699_area_old_daily_energy",
|
||||
config_entry=entry,
|
||||
original_name="Old area energy",
|
||||
)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, "4886699_14F28854-E2")},
|
||||
@@ -43,15 +57,19 @@ async def test_optimizer_sensors(hass: HomeAssistant) -> None:
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
assert len(entities) == 4
|
||||
assert len(entities) == 6
|
||||
assert entity_registry.async_get(stale_area_entity.entity_id) is None
|
||||
|
||||
energy_entity = next(
|
||||
entity for entity in entities if entity.unique_id.endswith("_daily_energy")
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_14F28854-E2_daily_energy"
|
||||
)
|
||||
power_entity = next(
|
||||
entity for entity in entities if entity.unique_id.endswith("_current_power")
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_14F28854-E2_current_power"
|
||||
)
|
||||
energy_state = hass.states.get(energy_entity.entity_id)
|
||||
power_state = hass.states.get(power_entity.entity_id)
|
||||
@@ -60,9 +78,71 @@ async def test_optimizer_sensors(hass: HomeAssistant) -> None:
|
||||
assert "1.1.1" in energy_state.name
|
||||
assert "1.1.1" in power_state.name
|
||||
|
||||
area_energy_entity = next(
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_area_west_daily_energy"
|
||||
)
|
||||
area_power_entity = next(
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_area_west_current_power"
|
||||
)
|
||||
area_energy_state = hass.states.get(area_energy_entity.entity_id)
|
||||
area_power_state = hass.states.get(area_power_entity.entity_id)
|
||||
assert area_energy_state.state == "2468.5"
|
||||
assert area_power_state.state == "321.4"
|
||||
assert area_energy_state.attributes["optimizer_count"] == 2
|
||||
assert area_energy_state.attributes["optimizer_ids"] == ["1.1.1", "1.1.2"]
|
||||
|
||||
devices = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
|
||||
assert len(devices) == 1
|
||||
assert devices[0].identifiers == {(DOMAIN, "4886699")}
|
||||
assert devices[0].name == "SolarEdge Site 4886699"
|
||||
assert all(entity.device_id == devices[0].id for entity in entities)
|
||||
assert entry.version == 2
|
||||
|
||||
|
||||
async def test_area_energy_unavailable_when_one_optimizer_failed(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Do not publish a misleading partial area sum."""
|
||||
payload = deepcopy(SAMPLE_PAYLOAD)
|
||||
payload["optimizers"][1]["dailyEnergyWh"] = None
|
||||
payload["optimizers"][1]["error"] = "Request failed"
|
||||
payload["successfulOptimizerCount"] = 1
|
||||
payload["failedOptimizerCount"] = 1
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="SolarEdge Site 4886699",
|
||||
unique_id="4886699",
|
||||
data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600},
|
||||
options={CONF_AREA_MAPPING: {"west": ["1.1.1", "1.1.2"]}},
|
||||
version=2,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"custom_components.solaredge_optimizers.api."
|
||||
"SolarEdgeOptimizerApiClient.async_get_optimizers",
|
||||
AsyncMock(return_value=parse_snapshot(payload)),
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
||||
area_energy_entity = next(
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_area_west_daily_energy"
|
||||
)
|
||||
area_power_entity = next(
|
||||
entity
|
||||
for entity in entities
|
||||
if entity.unique_id == "4886699_area_west_current_power"
|
||||
)
|
||||
|
||||
assert hass.states.get(area_energy_entity.entity_id).state == STATE_UNAVAILABLE
|
||||
assert hass.states.get(area_power_entity.entity_id).state == "321.4"
|
||||
|
||||
Reference in New Issue
Block a user