# Path: mppsolar/protocols/pi16.py
# class pi16(AbstractProtocol):
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"PI16"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "QPIGS",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "QPI",
#         ]
#         self.DEFAULT_COMMAND = "QPI"
# 
#     def checksum(self, data):
#         # QED20150620106
# 
#         _sum = 0
#         _len = data.find("%")
#         if _len == -1:
#             _len = len(data)
#         while _len > 0:
#             _len -= 1
#             _sum += ord(data[_len])
#         _sum &= 0xFF
#         _sum = f"{_sum:03}"
#         _sum = _sum.encode()
#         return _sum
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different for PI16
#         """
#         log.info(f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands")
#         # These need to be set to allow other functions to work`
#         self._command = command
#         self._command_defn = self.get_command_defn(command)
#         # End of required variables setting
#         cmd = bytes(self._command, "utf-8")
#         if (
#             self._command_defn
#             and "checksum_required" in self._command_defn
#             and self._command_defn["checksum_required"] == "True"
#         ):
#             # calculate the CRC
#             checksum = self.checksum(self._command)
#             log.debug(f"checksum {checksum}")
#             # combine byte_cmd, CRC , return
#             full_command = cmd + checksum + bytes([13])
#         else:
#             full_command = cmd + bytes([13])
#         log.debug(f"full command: {full_command}")
#         return full_command


import unittest
from mppsolar.protocols.pi16 import pi16 as pi
            "raw_response": ["(0 6 1234 12 43·l\r", ""],
            "_command": "QPIBI",
            "_command_description": "Battery information query",
            "unknown": [0, ""],
            "Number of batteries": [6, "#"],
            "Battery total capacity": [1234, "Ah"],
            "unknown_2": [12, ""],
            "Battery remaining time": [43, "min"],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_pi16_QMOD_badkey(self):
        """ test the decode of a QMOD response - with a bad key"""
        protocol = pi()
        response = b"(012345\x9c\xaf\r"
        command = "QMOD"
        expected = {
            "raw_response": ["(012345\x9c¯\r", ""],
            "_command": "QMOD",
            "_command_description": "Operational mode query",
            "Device Mode": ["Invalid key: 012345", ""],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_pi16_QMOD(self):
        """ test the decode of a QMOD response"""

# Path: mppsolar/protocols/pi16.py
# class pi16(AbstractProtocol):
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"PI16"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "QPIGS",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "QPI",
#         ]
#         self.DEFAULT_COMMAND = "QPI"
# 
#     def checksum(self, data):
#         # QED20150620106
# 
#         _sum = 0
#         _len = data.find("%")
#         if _len == -1:
#             _len = len(data)
#         while _len > 0:
#             _len -= 1
#             _sum += ord(data[_len])
#         _sum &= 0xFF
#         _sum = f"{_sum:03}"
#         _sum = _sum.encode()
#         return _sum
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different for PI16
#         """
#         log.info(f"Using protocol {self._protocol_id} with {len(self.COMMANDS)} commands")
#         # These need to be set to allow other functions to work`
#         self._command = command
#         self._command_defn = self.get_command_defn(command)
#         # End of required variables setting
#         cmd = bytes(self._command, "utf-8")
#         if (
#             self._command_defn
#             and "checksum_required" in self._command_defn
#             and self._command_defn["checksum_required"] == "True"
#         ):
#             # calculate the CRC
#             checksum = self.checksum(self._command)
#             log.debug(f"checksum {checksum}")
#             # combine byte_cmd, CRC , return
#             full_command = cmd + checksum + bytes([13])
#         else:
#             full_command = cmd + bytes([13])
#         log.debug(f"full command: {full_command}")
#         return full_command


# Path: tests/test_protocol_pi16.py
import unittest
from mppsolar.protocols.pi16 import pi16 as pi

            "raw_response": ["(0 6 1234 12 43·l\r", ""],
            "_command": "QPIBI",
            "_command_description": "Battery information query",
            "unknown": [0, ""],
            "Number of batteries": [6, "#"],
            "Battery total capacity": [1234, "Ah"],
            "unknown_2": [12, ""],
            "Battery remaining time": [43, "min"],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_pi16_QMOD_badkey(self):
        """ test the decode of a QMOD response - with a bad key"""
        protocol = pi()
        response = b"(012345\x9c\xaf\r"
        command = "QMOD"
        expected = {
            "raw_response": ["(012345\x9c¯\r", ""],
            "_command": "QMOD",
            "_command_description": "Operational mode query",
            "Device Mode": ["Invalid key: 012345", ""],
        }
        result = protocol.decode(response, command)
        # print(result)
        self.assertEqual(result, expected)

    def test_pi16_QMOD(self):
        """ test the decode of a QMOD response"""

        protocol = pi()