|
| 1 | +"""Tests for per-hub plugin runtime state.""" |
| 2 | + |
| 3 | +from dataclasses import replace |
| 4 | +from types import ModuleType, SimpleNamespace |
| 5 | +from typing import Any, cast |
| 6 | + |
| 7 | +from homeassistant.const import CONF_NAME |
| 8 | + |
| 9 | +from custom_components.solax_modbus import SolaXModbusHub |
| 10 | +from custom_components.solax_modbus.const import CONF_INTERFACE, plugin_base |
| 11 | +from custom_components.solax_modbus.plugin_sofar import battery_config as SofarBatteryConfig |
| 12 | +from custom_components.solax_modbus.plugin_sofar import plugin_instance as sofar_template |
| 13 | +from custom_components.solax_modbus.plugin_solinteg import plugin_instance as solinteg_template |
| 14 | + |
| 15 | + |
| 16 | +def make_hub(plugin_template: plugin_base, name: str) -> SolaXModbusHub: |
| 17 | + """Create a hub without opening a real transport.""" |
| 18 | + plugin_module = cast(ModuleType, SimpleNamespace(plugin_instance=plugin_template)) |
| 19 | + entry = SimpleNamespace(options={CONF_NAME: name, CONF_INTERFACE: "test"}) |
| 20 | + hass = SimpleNamespace() |
| 21 | + return SolaXModbusHub(cast(Any, hass), plugin_module, cast(Any, entry)) |
| 22 | + |
| 23 | + |
| 24 | +def test_hubs_get_independent_plugin_instances() -> None: |
| 25 | + """Each hub must receive its own plugin runtime object.""" |
| 26 | + first = make_hub(sofar_template, "Sofar 1") |
| 27 | + second = make_hub(sofar_template, "Sofar 2") |
| 28 | + |
| 29 | + assert first.plugin is not second.plugin |
| 30 | + assert first.plugin is not sofar_template |
| 31 | + assert second.plugin is not sofar_template |
| 32 | + |
| 33 | + first.plugin.inverter_model = "First model" |
| 34 | + |
| 35 | + assert second.plugin.inverter_model is None |
| 36 | + assert sofar_template.inverter_model is None |
| 37 | + |
| 38 | + |
| 39 | +def test_sofar_battery_runtime_state_is_isolated_per_hub() -> None: |
| 40 | + """Battery discovery state from one Sofar hub must not leak to another.""" |
| 41 | + first = make_hub(sofar_template, "Sofar 1") |
| 42 | + second = make_hub(sofar_template, "Sofar 2") |
| 43 | + first_battery_base = first.plugin.BATTERY_CONFIG |
| 44 | + second_battery_base = second.plugin.BATTERY_CONFIG |
| 45 | + template_battery_base = sofar_template.BATTERY_CONFIG |
| 46 | + |
| 47 | + assert first_battery_base is not None |
| 48 | + assert second_battery_base is not None |
| 49 | + assert template_battery_base is not None |
| 50 | + first_battery = cast(SofarBatteryConfig, first_battery_base) |
| 51 | + second_battery = cast(SofarBatteryConfig, second_battery_base) |
| 52 | + template_battery = cast(SofarBatteryConfig, template_battery_base) |
| 53 | + assert first_battery is not second_battery |
| 54 | + assert first_battery is not template_battery |
| 55 | + |
| 56 | + first_battery.number_strings = 2 |
| 57 | + first_battery.number_cels_in_parallel = 3 |
| 58 | + first_battery.selected_batt_nr = 1 |
| 59 | + first_battery.selected_batt_pack_nr = 2 |
| 60 | + first_battery.batt_pack_serials[1] = {2: "SOFAR-PACK-1"} |
| 61 | + |
| 62 | + assert second_battery.number_strings is None |
| 63 | + assert second_battery.number_cels_in_parallel is None |
| 64 | + assert second_battery.selected_batt_nr is None |
| 65 | + assert second_battery.selected_batt_pack_nr is None |
| 66 | + assert second_battery.batt_pack_serials == {} |
| 67 | + assert template_battery.batt_pack_serials == {} |
| 68 | + |
| 69 | + |
| 70 | +def test_solinteg_runtime_descriptions_are_isolated_per_hub() -> None: |
| 71 | + """MPPT-specific description changes must stay local to one Solinteg hub.""" |
| 72 | + first = make_hub(solinteg_template, "Solinteg 1") |
| 73 | + second = make_hub(solinteg_template, "Solinteg 2") |
| 74 | + first_index = next(index for index, description in enumerate(first.plugin.SELECT_TYPES) if description.key == "shadow_scan") |
| 75 | + second_index = next(index for index, description in enumerate(second.plugin.SELECT_TYPES) if description.key == "shadow_scan") |
| 76 | + template_index = next(index for index, description in enumerate(solinteg_template.SELECT_TYPES) if description.key == "shadow_scan") |
| 77 | + second_description = second.plugin.SELECT_TYPES[second_index] |
| 78 | + template_description = solinteg_template.SELECT_TYPES[template_index] |
| 79 | + |
| 80 | + first.plugin.SELECT_TYPES[first_index] = replace( |
| 81 | + first.plugin.SELECT_TYPES[first_index], |
| 82 | + option_dict={0: "off", 1: "mppt1"}, |
| 83 | + ) |
| 84 | + |
| 85 | + assert first.plugin.SELECT_TYPES[first_index].option_dict == {0: "off", 1: "mppt1"} |
| 86 | + assert second.plugin.SELECT_TYPES[second_index] == second_description |
| 87 | + assert solinteg_template.SELECT_TYPES[template_index] == template_description |
0 commit comments