Set Fronius entities to "unknown" when receiving invalid zero value (#102270)
This commit is contained in:
parent
704881743b
commit
fb13d9ce7c
3 changed files with 70 additions and 11 deletions
|
@ -1,6 +1,10 @@
|
|||
"""Tests for the Fronius integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.fronius.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
|
@ -32,55 +36,78 @@ async def setup_fronius_integration(
|
|||
return entry
|
||||
|
||||
|
||||
def _load_and_patch_fixture(
|
||||
override_data: dict[str, list[tuple[list[str], Any]]]
|
||||
) -> Callable[[str, str | None], str]:
|
||||
"""Return a fixture loader that patches values at nested keys for a given filename."""
|
||||
|
||||
def load_and_patch(filename: str, integration: str):
|
||||
"""Load a fixture and patch given values."""
|
||||
text = load_fixture(filename, integration)
|
||||
if filename not in override_data:
|
||||
return text
|
||||
|
||||
_loaded = json.loads(text)
|
||||
for keys, value in override_data[filename]:
|
||||
_dic = _loaded
|
||||
for key in keys[:-1]:
|
||||
_dic = _dic[key]
|
||||
_dic[keys[-1]] = value
|
||||
return json.dumps(_loaded)
|
||||
|
||||
return load_and_patch
|
||||
|
||||
|
||||
def mock_responses(
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
host: str = MOCK_HOST,
|
||||
fixture_set: str = "symo",
|
||||
inverter_ids: list[str | int] = [1],
|
||||
night: bool = False,
|
||||
override_data: dict[str, list[tuple[list[str], Any]]]
|
||||
| None = None, # {filename: [([list of nested keys], patch_value)]}
|
||||
) -> None:
|
||||
"""Mock responses for Fronius devices."""
|
||||
aioclient_mock.clear_requests()
|
||||
_night = "_night" if night else ""
|
||||
_load = _load_and_patch_fixture(override_data) if override_data else load_fixture
|
||||
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/GetAPIVersion.cgi",
|
||||
text=load_fixture(f"{fixture_set}/GetAPIVersion.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetAPIVersion.json", "fronius"),
|
||||
)
|
||||
for inverter_id in inverter_ids:
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&"
|
||||
f"DeviceId={inverter_id}&DataCollection=CommonInverterData",
|
||||
text=load_fixture(
|
||||
text=_load(
|
||||
f"{fixture_set}/GetInverterRealtimeData_Device_{inverter_id}{_night}.json",
|
||||
"fronius",
|
||||
),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetInverterInfo.cgi",
|
||||
text=load_fixture(f"{fixture_set}/GetInverterInfo{_night}.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetInverterInfo{_night}.json", "fronius"),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetLoggerInfo.cgi",
|
||||
text=load_fixture(f"{fixture_set}/GetLoggerInfo.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetLoggerInfo.json", "fronius"),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System",
|
||||
text=load_fixture(f"{fixture_set}/GetMeterRealtimeData.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetMeterRealtimeData.json", "fronius"),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetPowerFlowRealtimeData.fcgi",
|
||||
text=load_fixture(
|
||||
f"{fixture_set}/GetPowerFlowRealtimeData{_night}.json", "fronius"
|
||||
),
|
||||
text=_load(f"{fixture_set}/GetPowerFlowRealtimeData{_night}.json", "fronius"),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetStorageRealtimeData.cgi?Scope=System",
|
||||
text=load_fixture(f"{fixture_set}/GetStorageRealtimeData.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetStorageRealtimeData.json", "fronius"),
|
||||
)
|
||||
aioclient_mock.get(
|
||||
f"{host}/solar_api/v1/GetOhmPilotRealtimeData.cgi?Scope=System",
|
||||
text=load_fixture(f"{fixture_set}/GetOhmPilotRealtimeData.json", "fronius"),
|
||||
text=_load(f"{fixture_set}/GetOhmPilotRealtimeData.json", "fronius"),
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue