# Path: mppsolar/protocols/ved.py
# class ved(AbstractProtocol):
#     """
#     VED - VEDirect protocol handler
#     """
# 
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"VED"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "vedtext",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "vedtext"
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different for VEDirect
#         """
#         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
#         if self._command_defn is None:
#             return None
# 
#         # VEDHEX
#         # : start of command
#         # 7 Get
#         # 0000 id of the value to get
#         # 00 flags
#         # 00 cs
#         # \n
#         # eg b':70010003E\n' = get battery capacity id = 0x1000 = 0010 little endian
#         cmd_type = self._command_defn["type"]
#         if cmd_type == "VEDTEXT":
#             # Just listen - dont need to send a command
#             log.debug(f"command is VEDTEXT type so returning {cmd_type}")
#             return cmd_type
#         elif cmd_type == "VEDGET":
#             ID = self._command_defn["command_code"]
#             cmd = f"7{ID}00"
#             # pad cmd and convert to bytes for checksum
#             _r = f"0{cmd}"
#             _r = bytes.fromhex(_r)
#             checksum = vedHexChecksum(_r)
#             cmd = f":{cmd}{checksum:02X}\n"
#             log.debug(f"full command: {cmd}")
#             return cmd
#         log.warn("unable to generate full command - is the definition wrong?")
#         return None
# 
#     def check_response_valid(self, response) -> Tuple[bool, dict]:
#         """
#         VED HEX protocol - sum of bytes should be 0x55
#         VED Text protocol - no validity check
#         """
#         if not response:
#             return False, {"ERROR": ["No response", ""]}
#         if b":" in response:
#             # HEX protocol response
#             log.debug(f"checking validity of {response}")
#             _r = response.split(b":")[1][:-1].decode()
#             # print(f"trimmed response {_r}")
#             _r = f"0{_r}"
#             # print(f"padded response {_r}")
#             _r = bytes.fromhex(_r)
#             # print(f"bytes response {_r}")
#             data = _r[:-1]
#             checksum = _r[-1:][0]
#             if vedHexChecksum(data) == checksum:
#                 log.debug(f"VED Hex Checksum matches in response '{response}' checksum:{checksum}")
#                 return True, {}
#             else:
#                 # print("VED Hex Checksum does not match")
#                 return False, {
#                     "ERROR": [
#                         f"VED HEX checksum did not match for response {response}",
#                         "",
#                     ]
#                 }
#         else:
#             return True, {}
# 
#     def get_responses(self, response):
#         """
#         Override the default get_responses as its different for PI00
#         """
#         # remove \n
#         response = response.replace(b"\n", b"")
#         responses = []
#         # for hex protocol responses - these contain a :
#         if b":" in response:
#             # trim anything before ':'
#             _r = response.split(b":")[1].decode()
#             # pad command (which is single char)
#             _r = f"0{_r}"
#             _r = bytes.fromhex(_r)
#             if (
#                 self._command_defn is not None
#                 and self._command_defn["response_type"] == "POSITIONAL"
#             ):
#                 # Have a POSITIONAL type response, so need to break it up...
#                 for defn in self._command_defn["response"]:
#                     size = defn[1]
#                     item = _r[:size]
#                     responses.append(item)
#                     _r = _r[size:]
#                 return responses
#             else:
#                 return bytearray(response)
#                 # convert string hex to bytes
#                 _r = bytes.fromhex(_r)
#                 return bytearray(_r)
#         else:
#             # for text protocol responses
#             _responses = response.split(b"\r")
#             for resp in _responses:
#                 _resp = resp.split(b"\t")
#                 responses.append(_resp)
#         # Trim leading '(' of first response
#         # 3responses[0] = responses[0][1:]
#         # Remove CRC and \r of last response
#         # responses[-1] = responses[-1][:-3]
#         return responses


import unittest
from mppsolar.protocols.ved import ved as pi


class test_daly_decode(unittest.TestCase):
    maxDiff = 9999

    def test_vedtext(self):
        """test the decode of a VEDTEXT response"""

# Path: mppsolar/protocols/ved.py
# class ved(AbstractProtocol):
#     """
#     VED - VEDirect protocol handler
#     """
# 
#     def __init__(self, *args, **kwargs) -> None:
#         super().__init__()
#         self._protocol_id = b"VED"
#         self.COMMANDS = COMMANDS
#         self.STATUS_COMMANDS = [
#             "vedtext",
#         ]
#         self.SETTINGS_COMMANDS = [
#             "",
#         ]
#         self.DEFAULT_COMMAND = "vedtext"
# 
#     def get_full_command(self, command) -> bytes:
#         """
#         Override the default get_full_command as its different for VEDirect
#         """
#         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
#         if self._command_defn is None:
#             return None
# 
#         # VEDHEX
#         # : start of command
#         # 7 Get
#         # 0000 id of the value to get
#         # 00 flags
#         # 00 cs
#         # \n
#         # eg b':70010003E\n' = get battery capacity id = 0x1000 = 0010 little endian
#         cmd_type = self._command_defn["type"]
#         if cmd_type == "VEDTEXT":
#             # Just listen - dont need to send a command
#             log.debug(f"command is VEDTEXT type so returning {cmd_type}")
#             return cmd_type
#         elif cmd_type == "VEDGET":
#             ID = self._command_defn["command_code"]
#             cmd = f"7{ID}00"
#             # pad cmd and convert to bytes for checksum
#             _r = f"0{cmd}"
#             _r = bytes.fromhex(_r)
#             checksum = vedHexChecksum(_r)
#             cmd = f":{cmd}{checksum:02X}\n"
#             log.debug(f"full command: {cmd}")
#             return cmd
#         log.warn("unable to generate full command - is the definition wrong?")
#         return None
# 
#     def check_response_valid(self, response) -> Tuple[bool, dict]:
#         """
#         VED HEX protocol - sum of bytes should be 0x55
#         VED Text protocol - no validity check
#         """
#         if not response:
#             return False, {"ERROR": ["No response", ""]}
#         if b":" in response:
#             # HEX protocol response
#             log.debug(f"checking validity of {response}")
#             _r = response.split(b":")[1][:-1].decode()
#             # print(f"trimmed response {_r}")
#             _r = f"0{_r}"
#             # print(f"padded response {_r}")
#             _r = bytes.fromhex(_r)
#             # print(f"bytes response {_r}")
#             data = _r[:-1]
#             checksum = _r[-1:][0]
#             if vedHexChecksum(data) == checksum:
#                 log.debug(f"VED Hex Checksum matches in response '{response}' checksum:{checksum}")
#                 return True, {}
#             else:
#                 # print("VED Hex Checksum does not match")
#                 return False, {
#                     "ERROR": [
#                         f"VED HEX checksum did not match for response {response}",
#                         "",
#                     ]
#                 }
#         else:
#             return True, {}
# 
#     def get_responses(self, response):
#         """
#         Override the default get_responses as its different for PI00
#         """
#         # remove \n
#         response = response.replace(b"\n", b"")
#         responses = []
#         # for hex protocol responses - these contain a :
#         if b":" in response:
#             # trim anything before ':'
#             _r = response.split(b":")[1].decode()
#             # pad command (which is single char)
#             _r = f"0{_r}"
#             _r = bytes.fromhex(_r)
#             if (
#                 self._command_defn is not None
#                 and self._command_defn["response_type"] == "POSITIONAL"
#             ):
#                 # Have a POSITIONAL type response, so need to break it up...
#                 for defn in self._command_defn["response"]:
#                     size = defn[1]
#                     item = _r[:size]
#                     responses.append(item)
#                     _r = _r[size:]
#                 return responses
#             else:
#                 return bytearray(response)
#                 # convert string hex to bytes
#                 _r = bytes.fromhex(_r)
#                 return bytearray(_r)
#         else:
#             # for text protocol responses
#             _responses = response.split(b"\r")
#             for resp in _responses:
#                 _resp = resp.split(b"\t")
#                 responses.append(_resp)
#         # Trim leading '(' of first response
#         # 3responses[0] = responses[0][1:]
#         # Remove CRC and \r of last response
#         # responses[-1] = responses[-1][:-3]
#         return responses


# Path: tests/test_protocol_ved.py
import unittest
from mppsolar.protocols.ved import ved as pi



class test_daly_decode(unittest.TestCase):
    maxDiff = 9999

    def test_vedtext(self):
        """test the decode of a VEDTEXT response"""

        protocol = pi()