Skip to content

Commit

Permalink
Print error when modbus/tcp is used instead of modbus/at
Browse files Browse the repository at this point in the history
  • Loading branch information
kbialek committed Oct 12, 2023
1 parent 47eb3f0 commit 63d3e1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/deye_modbus_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def __extract_modbus_response_frame(self, frame: bytes | None) -> bytes | None:
if len(frame) == 29:
self.__parse_response_error_code(frame)
return None
if frame[0:3] == b"AT+":
self.__log.error(
"AT response detected. Try switching to 'AT' protocol. "
"Set 'DEYE_LOGGER_PROTOCOL=at' and remove DEYE_LOGGER_PORT from your config"
)
return None
if len(frame) < (29 + 4):
self.__log.error("Response frame is too short")
return None
Expand Down
19 changes: 19 additions & 0 deletions tests/deye_modbus_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ def test_unknown_error_code(self, connector):
self.assertEqual(len(captured.records), 1)
self.assertEqual(captured.records[0].getMessage(), "Unknown response error code. Error frame: 0100")

@patch("deye_connector.DeyeConnector")
def test_at_protocol_detected(self, connector):
# given
sut = DeyeModbus(DeyeModbusTcp(self.config, connector))
connector.send_request.return_value = bytearray.fromhex(
"41542b595a434d505645523d4d57335f3136555f353430365f322e33322d44310d0a0d0a"
)

# when
with self.assertLogs() as captured:
sut.read_registers(0x50, 0x5F)

# then
self.assertEqual(len(captured.records), 1)
self.assertEqual(
captured.records[0].getMessage(),
"AT response detected. Try switching to 'AT' protocol. Set 'DEYE_LOGGER_PROTOCOL=at' and remove DEYE_LOGGER_PORT from your config",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 63d3e1f

Please sign in to comment.