|
"source": "from decimal import Decimal\n\nimport json\nimport unittest\n\nfrom dsmr_parser import telegram_specifications, obis_references\nfrom dsmr_parser.objects import MbusDevice\n\n\nclass MbusDeviceTest(unittest.TestCase):\n\n def setUp(self):\n v5_objects = telegram_specifications.V5['objects']\n\n device_type_parser = [\n object[\"value_parser\"]\n for object in v5_objects\n if object[\"obis_reference\"] == obis_references.MBUS_DEVICE_TYPE\n ][0]\n device_type = device_type_parser.parse('0-2:24.1.0(003)\\r\\n')\n\n equipment_parser = [\n object[\"value_parser\"]\n for object in v5_objects\n if object[\"obis_reference\"] == obis_references.MBUS_EQUIPMENT_IDENTIFIER\n ][0]\n equipment = equipment_parser.parse('0-2:96.1.0(4730303339303031393336393930363139)\\r\\n')\n\n gas_reading_parser = [\n object[\"value_parser\"]\n for object in v5_objects\n if object[\"obis_reference\"] == obis_references.MBUS_METER_READING\n ][0]\n gas_reading = gas_reading_parser.parse('0-2:24.2.1(200426223001S)(00246.138*m3)\\r\\n')\n\n mbus_device = MbusDevice(channel_id=2)\n mbus_device.add(obis_references.MBUS_DEVICE_TYPE, device_type, \"MBUS_DEVICE_TYPE\")\n mbus_device.add(obis_references.MBUS_EQUIPMENT_IDENTIFIER, equipment, \"MBUS_EQUIPMENT_IDENTIFIER\")\n mbus_device.add(obis_references.MBUS_METER_READING, gas_reading, \"MBUS_METER_READING\")\n\n self.mbus_device = mbus_device\n\n def test_attributes(self):\n self.assertEqual(self.mbus_device.MBUS_DEVICE_TYPE.value, 3)\n self.assertEqual(self.mbus_device.MBUS_DEVICE_TYPE.unit, None)\n\n self.assertEqual(self.mbus_device.MBUS_EQUIPMENT_IDENTIFIER.value,\n '4730303339303031393336393930363139')\n self.assertEqual(self.mbus_device.MBUS_EQUIPMENT_IDENTIFIER.unit, None)\n\n self.assertEqual(self.mbus_device.MBUS_METER_READING.value, Decimal('246.138'))\n self.assertEqual(self.mbus_device.MBUS_METER_READING.unit, 'm3')\n\n def test_to_json(self):\n self.assertEqual(\n json.loads(self.mbus_device.to_json()),\n {\n 'CHANNEL_ID': 2,\n 'MBUS_DEVICE_TYPE': {'value': 3, 'unit': None},\n 'MBUS_EQUIPMENT_IDENTIFIER': {'value': '4730303339303031393336393930363139', 'unit': None},\n 'MBUS_METER_READING': {'datetime': '2020-04-26T20:30:01+00:00', 'value': 246.138, 'unit': 'm3'}}\n )\n\n def test_str(self):\n self.assertEqual(\n str(self.mbus_device),\n (\n 'MBUS DEVICE (channel 2)\\n'\n '\\tMBUS_DEVICE_TYPE: \t 3\t[None]\\n'\n '\\tMBUS_EQUIPMENT_IDENTIFIER: \t 4730303339303031393336393930363139\t[None]\\n'\n '\\tMBUS_METER_READING: \t 246.138\t[m3] at 2020-04-26T20:30:01+00:00\\n'\n )\n )\n",
|
|
"review": "# 8\n\n## Bugs\n- No explicit bug was detected in this segment of the code.\n\n## Optimizations\n- Include error handling for parsing functions to manage unexpected inputs more gracefully.\n- Consider using constants or an enum for repeated string literals to improve maintainability.\n- The parsing of date from the MBUS_METER_READING should ensure timezone consistency.\n\n## Good points\n- Makes effective use of unit tests to validate the functionality of the `MbusDevice` class.\n- Uses `unittest` which is a standard Python module for testing, showing good practice.\n- Demonstrates clarity in structuring tests into distinct methods.\n- Code adheres to PEP 8 style guidelines making it readable.\n\n## Summary\nThe code presents a unit test suite for an `MbusDevice` class, verifying its parsing and JSON serialization functionalities. The tests are well-organized, covering attributes and different string representations of the device. While the code is generally well-written and free from apparent bugs, improvements can be made in terms of error handling and simplifying repetitive structures.\n\n## Open source alternatives\n- [Home Assistant](https://www.home-assistant.io/): An open-source platform that can receive and decode DSMR data from smart meters.\n- [DSMR Reader](https://github.com/dsmrreader/dsmr-reader): An open-source application for monitoring and visualizing Dutch Smart Meter data.",
|