|
| 1 | +"""A single-register write must survive hardware that only accepts FC 0x10 (#353). |
| 2 | +
|
| 3 | +Register 30410 (VPP AC charge enable) rejects Write Single Register on at least one WIT |
| 4 | +8000TL3-HU and accepts Write Multiple Registers with count=1. The caller logged a warning |
| 5 | +and continued, so every other write in the mode sequence succeeded and grid charging |
| 6 | +silently never engaged - the control reported success and did nothing. |
| 7 | +
|
| 8 | +FC 0x06 is still tried first. Switching everything to FC 0x10 would risk the opposite |
| 9 | +failure on devices that only accept the single-register form, and there is one report in |
| 10 | +each direction. The fallback can only add an attempt where the first was refused outright. |
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import importlib |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +_gm = importlib.import_module("growatt_under_test.growatt_modbus") |
| 19 | + |
| 20 | +REG = 30410 |
| 21 | + |
| 22 | + |
| 23 | +class _Client(_gm.GrowattModbus): |
| 24 | + """Records which function codes were attempted, and how each was answered.""" |
| 25 | + |
| 26 | + def __init__(self, single_ok=True, multi_ok=True, single_raises=False): |
| 27 | + super().__init__(connection_type="tcp", host="10.0.0.1", port=502, |
| 28 | + register_map="WIT_4000_15000TL3") |
| 29 | + self.calls: list[str] = [] |
| 30 | + self._single_ok = single_ok |
| 31 | + self._multi_ok = multi_ok |
| 32 | + self._single_raises = single_raises |
| 33 | + |
| 34 | + def write_register(self, register, value): |
| 35 | + self.calls.append("fc06") |
| 36 | + if self._single_raises: |
| 37 | + raise OSError("Illegal Function") |
| 38 | + return self._single_ok |
| 39 | + |
| 40 | + def write_registers(self, register, values): |
| 41 | + self.calls.append("fc10") |
| 42 | + return self._multi_ok |
| 43 | + |
| 44 | + |
| 45 | +def test_fc06_is_tried_first_and_nothing_else_happens_when_it_works(): |
| 46 | + """The common path must be untouched - no extra traffic on healthy hardware.""" |
| 47 | + c = _Client() |
| 48 | + assert c.write_single_register_any_fc(REG, 1) is True |
| 49 | + assert c.calls == ["fc06"], "FC 0x10 was attempted even though FC 0x06 succeeded" |
| 50 | + |
| 51 | + |
| 52 | +def test_it_falls_back_when_fc06_raises(): |
| 53 | + """The reported case: the device refuses Write Single Register outright.""" |
| 54 | + c = _Client(single_raises=True, multi_ok=True) |
| 55 | + assert c.write_single_register_any_fc(REG, 1) is True |
| 56 | + assert c.calls == ["fc06", "fc10"] |
| 57 | + |
| 58 | + |
| 59 | +def test_it_falls_back_when_fc06_returns_false(): |
| 60 | + """Not every refusal raises - some paths report failure by return value.""" |
| 61 | + c = _Client(single_ok=False, multi_ok=True) |
| 62 | + assert c.write_single_register_any_fc(REG, 1) is True |
| 63 | + assert c.calls == ["fc06", "fc10"] |
| 64 | + |
| 65 | + |
| 66 | +def test_both_refused_reports_failure(): |
| 67 | + """The caller must be able to tell the difference between 'worked somehow' and |
| 68 | + 'nothing worked' - the old code could not, which is why this went unnoticed.""" |
| 69 | + c = _Client(single_ok=False, multi_ok=False) |
| 70 | + assert c.write_single_register_any_fc(REG, 1) is False |
| 71 | + assert c.calls == ["fc06", "fc10"] |
| 72 | + |
| 73 | + |
| 74 | +def test_a_raising_fc10_is_not_propagated(): |
| 75 | + """A mode change must not abort on the fallback attempt itself.""" |
| 76 | + c = _Client(single_raises=True) |
| 77 | + c.write_registers = lambda r, v: (_ for _ in ()).throw(OSError("also refused")) |
| 78 | + assert c.write_single_register_any_fc(REG, 1) is False |
| 79 | + |
| 80 | + |
| 81 | +def test_the_vpp_charge_paths_use_it(): |
| 82 | + """Both the Hold and Charge sequences write 30410. A helper wired into one of them |
| 83 | + would leave the other silently broken.""" |
| 84 | + from pathlib import Path |
| 85 | + |
| 86 | + source = (Path(__file__).parent.parent / "custom_components" / "growatt_modbus" |
| 87 | + / "select.py").read_text(encoding="utf-8") |
| 88 | + assert source.count("write_single_register_any_fc(self.VPP_AC_CHARGE_ENABLE, 1)") == 2, ( |
| 89 | + "not both 30410 write sites go through the fallback" |
| 90 | + ) |
| 91 | + assert "client.write_register(self.VPP_AC_CHARGE_ENABLE" not in source, ( |
| 92 | + "a bare FC 0x06 write to 30410 remains" |
| 93 | + ) |
0 commit comments