import pytest from bmspy.classes import BMSScalarField, BMSMultiField, BMSInfoField from bmspy.jbd_bms import JBDBMS # --------------------------------------------------------------------------- # Raw JBD BMS wire-format fixtures # --------------------------------------------------------------------------- # 03 response: 31 bytes, data_len=25, 4 cells, 1 NTC sensor # Encodes: 52.00V, 0.00A, 100.00Ah remaining, 100.00Ah nominal, # 10 cycles, 2023-01-15, no protection faults, rsoc=95, # charging+discharging MOSFET on, 4 cells, 25.0°C VALID_03_RESPONSE = bytearray([ 0xDD, 0xA5, 0x00, 0x19, # start, r/w, status OK, data_len=25 0x14, 0x50, # total voltage: 5200 * 0.01 = 52.00 V 0x00, 0x00, # current: 0 0x27, 0x10, # remaining cap: 10000 * 0.01 = 100.00 Ah 0x27, 0x10, # nominal cap: 10000 * 0.01 = 100.00 Ah 0x00, 0x0A, # charge cycles: 10 0x2E, 0x2F, # manufacture date: 0x2E2F → 2023-01-15 0x00, 0x00, # balance state high (no balancing) 0x00, 0x00, # balance state low (no balancing) 0x00, 0x00, # protection state (no faults) 0x00, # software version 0x5F, # rsoc: 95 * 0.01 = 0.95 0x03, # control status: charging(bit0) + discharging(bit1) 0x04, # cell count: 4 0x01, # NTC sensor count: 1 0x0B, 0xA5, # NTC 1: (0x0BA5=2981 - 2731) * 0.1 = 25.0 °C 0xFD, 0x97, # checksum ]) # 04 response: 14 bytes, 4 cells # Cell voltages: 3.600 V, 3.601 V, 3.599 V, 3.598 V VALID_04_RESPONSE = bytearray([ 0xDD, 0xA5, 0x00, 0x08, # start, r/w, status OK, data_len=8 0x0E, 0x10, # cell 1: 3600 * 0.001 = 3.600 V 0x0E, 0x11, # cell 2: 3601 * 0.001 = 3.601 V 0x0E, 0x0F, # cell 3: 3599 * 0.001 = 3.599 V 0x0E, 0x0E, # cell 4: 3598 * 0.001 = 3.598 V 0xFF, 0x82, # checksum ]) @pytest.fixture def valid_03_response() -> bytearray: return bytearray(VALID_03_RESPONSE) @pytest.fixture def valid_04_response() -> bytearray: return bytearray(VALID_04_RESPONSE) @pytest.fixture def populated_jbdbms() -> JBDBMS: bms = JBDBMS() bms.bms_voltage_total_volts = BMSScalarField( help="Total Voltage", raw_value=52.0, value="52.00", units="V" ) bms.bms_current_amps = BMSScalarField( help="Current", raw_value=0.0, value="0.00", units="A" ) bms.bms_capacity_charge_ratio = BMSScalarField( help="Percent Charge", raw_value=0.95, value="0.95", units="‰" ) bms.bms_manufacture_date = BMSInfoField( help="Date of Manufacture", info="2023-01-15" ) bms.bms_temperature_celcius = BMSMultiField( help="Temperature", label="sensor", raw_values={1: 25.0}, values={1: "25.00"}, units="°C", ) return bms