11 lines
4.4 KiB
JSON
Raw Normal View History

2024-12-29 19:47:35 +00:00
{
"extension": ".py",
"source": "from unittest.mock import Mock\n\nimport unittest\n\nfrom dsmr_parser import obis_references as obis\nfrom dsmr_parser.clients.rfxtrx_protocol import create_rfxtrx_dsmr_protocol, PACKETTYPE_DSMR, SUBTYPE_P1\nfrom dsmr_parser.objects import Telegram\n\nTELEGRAM_V2_2 = (\n '/ISk5\\2MT382-1004\\r\\n'\n '\\r\\n'\n '0-0:96.1.1(00000000000000)\\r\\n'\n '1-0:1.8.1(00001.001*kWh)\\r\\n'\n '1-0:1.8.2(00001.001*kWh)\\r\\n'\n '1-0:2.8.1(00001.001*kWh)\\r\\n'\n '1-0:2.8.2(00001.001*kWh)\\r\\n'\n '0-0:96.14.0(0001)\\r\\n'\n '1-0:1.7.0(0001.01*kW)\\r\\n'\n '1-0:2.7.0(0000.00*kW)\\r\\n'\n '0-0:17.0.0(0999.00*kW)\\r\\n'\n '0-0:96.3.10(1)\\r\\n'\n '0-0:96.13.1()\\r\\n'\n '0-0:96.13.0()\\r\\n'\n '0-1:24.1.0(3)\\r\\n'\n '0-1:96.1.0(000000000000)\\r\\n'\n '0-1:24.3.0(161107190000)(00)(60)(1)(0-1:24.2.1)(m3)\\r\\n'\n '(00001.001)\\r\\n'\n '0-1:24.4.0(1)\\r\\n'\n '!\\r\\n'\n)\n\nOTHER_RF_PACKET = b'\\x03\\x01\\x02\\x03'\n\n\ndef encode_telegram_as_RF_packets(telegram):\n data = b''\n\n for line in telegram.split('\\n'):\n packet_data = (line + '\\n').encode('ascii')\n packet_header = bytes(bytearray([\n len(packet_data) + 3, # excluding length byte\n PACKETTYPE_DSMR,\n SUBTYPE_P1,\n 0 # seq num (ignored)\n ]))\n\n data += packet_header + packet_data\n # other RF packets can pass by on the line\n data += OTHER_RF_PACKET\n\n return data\n\n\nclass RFXtrxProtocolTest(unittest.TestCase):\n\n def setUp(self):\n new_protocol, _ = create_rfxtrx_dsmr_protocol('2.2',\n telegram_callback=Mock(),\n keep_alive_interval=1)\n self.protocol = new_protocol()\n\n def test_complete_packet(self):\n \"\"\"Protocol should assemble incoming lines into complete packet.\"\"\"\n\n data = encode_telegram_as_RF_packets(TELEGRAM_V2_2)\n # send data broken up in two parts\n self.protocol.data_received(data[0:200])\n self.protocol.data_received(data[200:])\n\n telegram = self.protocol.telegram_callback.call_args_list[0][0][0]\n assert isinstance(telegram, Telegram)\n\n assert float(telegram[obis.CURRENT_ELECTRICITY_USAGE].value) == 1.01\n assert telegram[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'\n\n assert float(telegram[obis.GAS_METER_READING].value) == 1.001\n assert telegram[obis.GAS_METER_READING].unit == 'm3'\n",
"review": "# Grade: 7\n\n## Bugs\n- None detected\n\n## Optimizations\n- Use `self.assertIsInstance` instead of `assert isinstance` for better unittest integration.\n- Consider proper asynchronous function handling since `data_received` might be designed to work in an async environment.\n- Instead of hardcoding values, consider defining constants for repeating values, such as `3` in `packet_header`.\n- The expression `(line + '\\n')` can be optimized by using formatted strings.\n\n## Good points\n- The code has proper use of unittest framework with clear test structure.\n- Effective use of Mock to simulate external dependencies.\n- Code is modular, breaking logic into clearly defined functions.\n\n## Summary\nThe code is a unit test for the RFXtrx DSMR protocol using the unittest framework. It includes a helper function to encode a DSMR telegram into RF packets and uses Mock to simulate the telegram callback. The test checks that incoming data is correctly interpreted as a telegram object with the expected electricity and gas readings. The code is clean and follows good practices in terms of structure and decomposition, although minor optimizations could improve performance and readability.\n\n## Open source alternatives\n- [dsmr_parser](https://github.com/ndokter/dsmr_parser): While this is more focused on parsing DSMR readings, related functionalities of DSMR processing are relevant.\n- [Home Assistant](https://github.com/home-assistant/core): Offers broader smart home integrations, including DSMR integrations.\n- [pyRFXtrx](https://github.com/Danielhiversen/pyRFXtrx): A Python library that controls RFXtrx chips and can contribute to understanding RF protocol integration.",
"filename": "test_rfxtrx_protocol.py",
"path": "test/test_rfxtrx_protocol.py",
"directory": "test",
"grade": 7,
"size": 2436,
"line_count": 78
}