Files
ha-solaredge-optimizers/tests/test_convert_portal_layout.py
jensandClaude Opus 5.5 ce03c05d84 Add 3D roof view card for optimizer power
The integration now serves solaredge-roof-card and loads it in every
frontend session. The card shows the building in 3D with the current
power in W and the Panel-Position on every panel; the colour runs from
dark (0 W) to bright (Skalenmaximum, default 460 W). Failed panels are
grey and hatched, a tap opens the more-info dialog.

scripts/convert_portal_layout.py turns the SolarEdge portal's physical
layout and site model into the card's layout once, so nothing depends
on the portal at runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:25:47 +01:00

108 lines
3.6 KiB
Python

"""Tests for the portal layout converter."""
from __future__ import annotations
import math
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / "scripts"))
from convert_portal_layout import ( # noqa: E402
convert,
decode_module_handle,
point_in_polygon,
)
GROUP_ID = 2686254341
def handle(row: int, column: int, group: int = GROUP_ID) -> int:
"""Encode a module handle like the portal does."""
return (((column & 0x7FF) << 11) | (row & 0x7FF)) << 32 | group
def test_decode_module_handle() -> None:
"""Decode signed row and column from a module handle."""
assert decode_module_handle(GROUP_ID) == (0, 0)
assert decode_module_handle(handle(3, 2)) == (3, 2)
assert decode_module_handle(handle(-1, -2)) == (-1, -2)
def _vertex(x: float, y: float, z: float) -> dict:
return {"position": {"x": x, "y": y}, "heightWithoutPolygonGroups": z}
def _site() -> tuple[dict, dict]:
"""East-facing roof, 30 degrees, eave at x=4 (z=3), ridge at x=0."""
tilt = math.radians(30)
ridge = 3 + 4 * math.tan(tilt)
model = {
"vertices": {
"1": _vertex(0, 0, ridge),
"2": _vertex(4, 0, 3),
"3": _vertex(4, 6, 3),
"4": _vertex(0, 6, ridge),
},
"edges": {
"a": {"vertexAId": "1", "vertexBId": "2"},
"b": {"vertexAId": "2", "vertexBId": "3"},
"c": {"vertexAId": "3", "vertexBId": "4"},
"d": {"vertexAId": "4", "vertexBId": "1"},
},
"polygons": {"p": {"edgeIds": ["a", "b", "c", "d"]}},
"moduleGroups": {
str(GROUP_ID): {
"polygonId": "p",
"facetRotation": 0.0,
"rowSpacing": 0.03,
"columnSpacing": 0.03,
"position": {"x": 3.8, "y": 0.2},
"orientation": "LANDSCAPE",
}
},
"moduleModels": {"1": {"stcPower": 460.0, "length": 1762.0, "width": 1134.0}},
"optimizers": {
"o1": {"serialNumber": "A", "moduleIds": [str(handle(0, 0))]},
"o2": {"serialNumber": "B", "moduleIds": [str(handle(0, 1))]},
},
}
site_model = {
"modelData": model,
"moduleGroupDetails": {
str(GROUP_ID): {
"azimuth": math.radians(90),
"tilt": tilt,
"moduleHandleIds": [handle(0, 0), handle(0, 1)],
}
},
}
physical = {
"physicalDevicesMap": {
"inverters": {"i": {"sequenceNumber": 1}},
"strings": {
"s": {"sequenceNumber": 1, "inverterId": "i", "optimizerIds": [1, 2]}
},
"optimizers": {"1": {"serialNumber": "A"}, "2": {"serialNumber": "B"}},
}
}
return physical, site_model
def test_convert_places_panels_on_their_roof() -> None:
"""Panels get their Panel-Position, lie on the roof and rise up the slope."""
physical, site_model = _site()
layout = convert(physical, site_model, {"ost": ["1.1.1", "1.1.2"]})
assert layout["module_power"] == 460.0
assert [roof["name"] for roof in layout["roofs"]] == ["ost"]
assert [panel["id"] for panel in layout["panels"]] == ["1.1.1", "1.1.2"]
outline = [tuple(p) for p in layout["roofs"][0]["outline"]]
for panel in layout["panels"]:
lower_left, _, _, upper_left = panel["corners"]
assert upper_left[2] > lower_left[2]
for x, y, z in panel["corners"]:
assert point_in_polygon((x, y), outline, 0.01)
roof_z = 3 + (4 - x) * math.tan(math.radians(30))
assert math.isclose(z, roof_z + 0.05, abs_tol=0.01)