diff --git a/midealocal/crc8.py b/midealocal/crc8.py index 3d56f2d2..036f30da 100644 --- a/midealocal/crc8.py +++ b/midealocal/crc8.py @@ -1,4 +1,6 @@ -crc8_854_table = [ +"""CRC8 calculator.""" + +crc8_854_table: list[int] = [ 0x00, 0x5E, 0xBC, @@ -258,13 +260,12 @@ ] -def calculate(data): - crc_value = 0 +def calculate(data: bytearray) -> int: + """ + Calculate CRC8 value of a bytearray. + + """ + crc_value: int = 0 for m in data: - k = crc_value ^ m - if k > 256: - k -= 256 - if k < 0: - k += 256 - crc_value = crc8_854_table[k] + crc_value = crc8_854_table[crc_value ^ m] return crc_value diff --git a/tests/crc8_test.py b/tests/crc8_test.py index 2e969033..5eed5713 100644 --- a/tests/crc8_test.py +++ b/tests/crc8_test.py @@ -7,54 +7,12 @@ def test_calculate() -> None: """Test calculate method.""" data: bytearray = bytearray( [ - # 2 bytes - StaicHeader 0x5A, - 0x5A, - # 2 bytes - mMessageType + 0x82, 0x01, 0x11, - # 2 bytes - PacketLenght - 0x00, - 0x00, - # 2 bytes + 0xFF, 0x20, - 0x00, - # 4 bytes - MessageId - 0x00, - 0x00, - 0x00, - 0x00, - # 8 bytes - Date&Time - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - # 6 bytes - mDeviceID - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - # 12 bytes - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, ] ) - assert calculate(data) == 86 + assert calculate(data) == 101