Separate JBD decoding from serial transport
Move response decoding into jbd_proto and the data container into jbd_snapshot. Preserve the existing jbd_bms imports and test the transport boundary.
This commit is contained in:
@@ -20,6 +20,7 @@ COMPOSITE_TEMP_PACKET = (2, 0xFE, 0x15, b"\xb0\x06\x14") # field 102: 20 C
|
||||
|
||||
@pytest.mark.parametrize("first", [
|
||||
"ecoflow_proto", "ecoflow_river2", "ecoflow_river3", "ecoflow_snapshot",
|
||||
"jbd_proto", "jbd_snapshot",
|
||||
])
|
||||
def test_decoders_import_in_any_order_without_transport_dependencies(first):
|
||||
# A fresh interpreter catches import cycles hidden by pytest's import order.
|
||||
@@ -37,8 +38,9 @@ class NoTransportImports(importlib.abc.MetaPathFinder):
|
||||
|
||||
sys.meta_path.insert(0, NoTransportImports())
|
||||
importlib.import_module('bmspy.' + sys.argv[1])
|
||||
from bmspy import ecoflow_proto, ecoflow_river2, ecoflow_river3
|
||||
from bmspy import ecoflow_proto, ecoflow_river2, ecoflow_river3, jbd_proto
|
||||
from bmspy.ecoflow_snapshot import EcoFlowSnapshot
|
||||
from bmspy.jbd_snapshot import JBDBMS
|
||||
|
||||
assert ecoflow_proto.format_payload(2, 0xFE, 0x15, b'\\x08\\x00') == [
|
||||
'[DisplayPropertyUpload]', ' errcode: 0',
|
||||
@@ -46,7 +48,9 @@ assert ecoflow_proto.format_payload(2, 0xFE, 0x15, b'\\x08\\x00') == [
|
||||
assert ecoflow_proto.format_payload(2, 0x20, 2, bytes(23))[0] == '[PD heartbeat]'
|
||||
assert ecoflow_river2.decode_river2_payload(2, 0x20, 2, bytes(23))['soc'] == 0
|
||||
assert ecoflow_river3.decode_river3_payload(2, 0xFE, 0x15, b'\\x08\\x00') == {}
|
||||
assert jbd_proto.bytes_to_digits(1, 0) == 256
|
||||
assert list(EcoFlowSnapshot().items()) == []
|
||||
assert list(JBDBMS().items()) == []
|
||||
"""
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-c", script, first],
|
||||
@@ -55,10 +59,17 @@ assert list(EcoFlowSnapshot().items()) == []
|
||||
assert result.returncode == 0, result.stderr
|
||||
|
||||
|
||||
def test_snapshot_and_decoder_imports_remain_compatible():
|
||||
from bmspy import ecoflow_bt
|
||||
def test_snapshot_and_jbd_function_imports_remain_compatible():
|
||||
from bmspy import ecoflow_bt, jbd_bms, jbd_proto
|
||||
from bmspy.jbd_snapshot import JBDBMS
|
||||
|
||||
assert ecoflow_bt.EcoFlowSnapshot is EcoFlowSnapshot
|
||||
assert jbd_bms.JBDBMS is JBDBMS
|
||||
for name in (
|
||||
"bytes_to_digits", "bytes_to_date", "convert_to_signed",
|
||||
"calculate_checksum", "verify_checksum", "parse_03_response", "parse_04_response",
|
||||
):
|
||||
assert getattr(jbd_bms, name) is getattr(jbd_proto, name)
|
||||
assert ecoflow_bt.decode_river2_payload is ecoflow_river2.decode_river2_payload
|
||||
assert ecoflow_bt.decode_river3_payload is ecoflow_river3.decode_river3_payload
|
||||
|
||||
|
||||
Reference in New Issue
Block a user