issue-51-telegram fix bug when no mbus devices are present
This commit is contained in:
parent
47db8cad27
commit
0593d4172d
@ -36,9 +36,12 @@ class Telegram(object):
|
||||
# Group Mbus related values into a MbusDevice object.
|
||||
# TODO MaxDemandParser (BELGIUM_MAXIMUM_DEMAND_13_MONTHS) returns a list
|
||||
if isinstance(dsmr_object, DSMRObject) and dsmr_object.is_mbus_reading:
|
||||
channel_id = dsmr_object.obis_id_code[1]
|
||||
mbus_device = self._mbus_devices[channel_id]
|
||||
mbus_device.add(obis_reference, dsmr_object)
|
||||
self._add_mbus(obis_reference, dsmr_object)
|
||||
|
||||
def _add_mbus(self, obis_reference, dsmr_object):
|
||||
channel_id = dsmr_object.obis_id_code[1]
|
||||
mbus_device = self._mbus_devices[channel_id]
|
||||
mbus_device.add(obis_reference, dsmr_object)
|
||||
|
||||
def get_mbus_devices(self):
|
||||
"""
|
||||
@ -47,7 +50,9 @@ class Telegram(object):
|
||||
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):
|
||||
return self._mbus_devices[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]
|
||||
|
||||
def __getattr__(self, name):
|
||||
""" will only get called for undefined attributes """
|
||||
|
@ -356,3 +356,14 @@ class TelegramTest(unittest.TestCase):
|
||||
self.assertEqual(mbus_device_2.DEVICE_TYPE.value, 3)
|
||||
self.assertEqual(mbus_device_2.EQUIPMENT_IDENTIFIER_GAS.value, '4730303339303031393336393930363139')
|
||||
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(), [])
|
||||
|
Loading…
Reference in New Issue
Block a user