issue-51-telegram fix bug when no mbus devices are present

This commit is contained in:
Nigel Dokter 2023-02-12 18:06:01 +01:00
parent 47db8cad27
commit 0593d4172d
2 changed files with 20 additions and 4 deletions

View File

@ -36,6 +36,9 @@ class Telegram(object):
# Group Mbus related values into a MbusDevice object. # Group Mbus related values into a MbusDevice object.
# TODO MaxDemandParser (BELGIUM_MAXIMUM_DEMAND_13_MONTHS) returns a list # TODO MaxDemandParser (BELGIUM_MAXIMUM_DEMAND_13_MONTHS) returns a list
if isinstance(dsmr_object, DSMRObject) and dsmr_object.is_mbus_reading: if isinstance(dsmr_object, DSMRObject) and dsmr_object.is_mbus_reading:
self._add_mbus(obis_reference, dsmr_object)
def _add_mbus(self, obis_reference, dsmr_object):
channel_id = dsmr_object.obis_id_code[1] channel_id = dsmr_object.obis_id_code[1]
mbus_device = self._mbus_devices[channel_id] mbus_device = self._mbus_devices[channel_id]
mbus_device.add(obis_reference, dsmr_object) mbus_device.add(obis_reference, dsmr_object)
@ -47,6 +50,8 @@ class Telegram(object):
return [d[1] for d in sorted(self._mbus_devices.items(), key=lambda x: x[0])] return [d[1] for d in sorted(self._mbus_devices.items(), key=lambda x: x[0])]
def get_mbus_device_by_channel(self, channel_id): def get_mbus_device_by_channel(self, channel_id):
# Check key, because defaultdict would otherwise instantiate an empty MbusDevice
if channel_id in self._mbus_devices:
return self._mbus_devices[channel_id] return self._mbus_devices[channel_id]
def __getattr__(self, name): def __getattr__(self, name):

View File

@ -356,3 +356,14 @@ class TelegramTest(unittest.TestCase):
self.assertEqual(mbus_device_2.DEVICE_TYPE.value, 3) self.assertEqual(mbus_device_2.DEVICE_TYPE.value, 3)
self.assertEqual(mbus_device_2.EQUIPMENT_IDENTIFIER_GAS.value, '4730303339303031393336393930363139') self.assertEqual(mbus_device_2.EQUIPMENT_IDENTIFIER_GAS.value, '4730303339303031393336393930363139')
self.assertEqual(mbus_device_2.HOURLY_GAS_METER_READING.value, Decimal('246.138')) self.assertEqual(mbus_device_2.HOURLY_GAS_METER_READING.value, Decimal('246.138'))
def test_without_mbus_devices(self):
parser = TelegramParser(telegram_specifications.V5, apply_checksum_validation=False)
telegram = parser.parse('')
self.assertEqual(telegram.get_mbus_devices(), [])
self.assertIsNone(telegram.get_mbus_device_by_channel(1))
# Because of a bug related to incorrect use of defaultdict,
# test again for unwanted side effects
self.assertEqual(telegram.get_mbus_devices(), [])