From a56fcf43c315443c630e266b304e3fa5b1f70c6a Mon Sep 17 00:00:00 2001 From: WillCodeForCats <48533968+WillCodeForCats@users.noreply.github.com> Date: Tue, 18 Aug 2026 14:38:14 -0700 Subject: [PATCH] Add tests for hub options --- tests/test_hub_options.py | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_hub_options.py diff --git a/tests/test_hub_options.py b/tests/test_hub_options.py new file mode 100644 index 00000000..f8ac2631 --- /dev/null +++ b/tests/test_hub_options.py @@ -0,0 +1,67 @@ +"""Tests for SolarEdgeModbusMultiHub options.""" + +from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT + +from custom_components.solaredge_modbus_multi.const import DOMAIN, ConfName +from custom_components.solaredge_modbus_multi.hub import SolarEdgeModbusMultiHub + +ENTRY_DATA = { + CONF_NAME: "SolarEdge", + CONF_HOST: "127.0.0.1", + CONF_PORT: 1502, +} + + +# 209460d "Fix bad default" (PR #408) +# bf0e822 "Incorrect casting of default value" (PR #414) + + +def _make_hub(hass, entry_data=None, entry_options=None): + hass.data[DOMAIN] = {"yaml": {}} + return SolarEdgeModbusMultiHub( + hass, + "test_entry_id", + entry_data if entry_data is not None else ENTRY_DATA, + entry_options if entry_options is not None else {}, + ) + + +async def test_options_default_to_int(hass): + hub = _make_hub(hass) + + for attr in ( + "_sleep_after_write", + "_battery_rating_adjust", + "_battery_energy_reset_cycles", + ): + value = getattr(hub, attr) + assert value == 0 + assert isinstance(value, int) and not isinstance(value, bool), ( + f"{attr} regressed to {type(value).__name__}" + ) + + +async def test_option_override_is_stored(hass): + hub = _make_hub(hass, entry_options={ConfName.SLEEP_AFTER_WRITE: 5}) + + assert hub._sleep_after_write == 5 + assert isinstance(hub._sleep_after_write, int) + assert not isinstance(hub._sleep_after_write, bool) + + +async def test_options_default_to_bool(hass): + hub = _make_hub(hass) + + for attr in ( + "_detect_meters", + "_detect_batteries", + "_keep_modbus_open", + ): + assert isinstance(getattr(hub, attr), bool) + + +async def test_device_list_defaults(hass): + entry_data = dict(ENTRY_DATA) + hub = _make_hub(hass, entry_data=entry_data) + + assert hub._inverter_list == ["1"]