diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..8122dce --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,22 @@ +name: Unittests +on: + push: + pull_request: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Run unittests + run: python3 -m unittest discover -s tests -p "*_test.py" diff --git a/tests/device_struct_test.py b/tests/device_struct_test.py new file mode 100644 index 0000000..850ef6f --- /dev/null +++ b/tests/device_struct_test.py @@ -0,0 +1,24 @@ +"""Unittest for device struct.""" + +import unittest + +from custom_components.bluetti_bt.bluetti_bt_lib.utils.commands import ReadHoldingRegisters +from custom_components.bluetti_bt.bluetti_bt_lib.utils.struct import DeviceStruct + +class TestDeviceStruct(unittest.TestCase): + def test_get_read_holding_registers(self): + struct = DeviceStruct() + + struct.add_swap_string_field("device_type", 110, 6) + struct.add_sn_field("serial_number", 116) + struct.add_decimal_field( + "power_generation", 154, 1 + ) + struct.add_uint_field("total_battery_percent", 102) + + registers = struct.get_read_holding_registers() + + self.assertGreater(len(registers), 0) + + for reg in registers: + self.assertIsInstance(reg, ReadHoldingRegisters)