|
"source": "from decimal import Decimal\nimport datetime\nimport unittest\n\nimport pytz\n\nfrom dsmr_parser import obis_references as obis\nfrom dsmr_parser import telegram_specifications\nfrom dsmr_parser.exceptions import InvalidChecksumError, ParseError\nfrom dsmr_parser.objects import CosemObject, MBusObject\nfrom dsmr_parser.parsers import TelegramParser\nfrom test.example_telegrams import TELEGRAM_V4_2\n\n\nclass TelegramParserV4_2Test(unittest.TestCase):\n \"\"\" Test parsing of a DSMR v4.2 telegram. \"\"\"\n\n def test_parse(self):\n parser = TelegramParser(telegram_specifications.V4)\n try:\n result = parser.parse(TELEGRAM_V4_2, throw_ex=True)\n except Exception as ex:\n assert False, f\"parse trigged an exception {ex}\"\n\n # P1_MESSAGE_HEADER (1-3:0.2.8)\n assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject)\n assert result[obis.P1_MESSAGE_HEADER].unit is None\n assert isinstance(result[obis.P1_MESSAGE_HEADER].value, str)\n assert result[obis.P1_MESSAGE_HEADER].value == '42'\n\n # P1_MESSAGE_TIMESTAMP (0-0:1.0.0)\n assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP], CosemObject)\n assert result[obis.P1_MESSAGE_TIMESTAMP].unit is None\n assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP].value, datetime.datetime)\n assert result[obis.P1_MESSAGE_TIMESTAMP].value == \\\n datetime.datetime(2016, 11, 13, 19, 57, 57, tzinfo=pytz.UTC)\n\n # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)\n assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)\n assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('1581.123')\n\n # ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)\n assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)\n assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('1435.706')\n\n # ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('0')\n\n # ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'\n assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)\n assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('0')\n\n # ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)\n assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)\n assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None\n assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)\n assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0002'\n\n # EQUIPMENT_IDENTIFIER (0-0:96.1.1)\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)\n assert result[obis.EQUIPMENT_IDENTIFIER].unit is None\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)\n assert result[obis.EQUIPMENT_IDENTIFIER].value == '3960221976967177082151037881335713'\n\n # CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)\n assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)\n assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'\n assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)\n assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('2.027')\n\n # CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)\n assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)\n assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'\n assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)\n assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')\n\n # SHORT_POWER_FAILURE_COUNT (1-0:96.7.21)\n assert isinstance(result[obis.SHORT_POWER_FAILURE_COUNT], CosemObject)\n assert result[obis.SHORT_POWER_FAILURE_COUNT].unit is None\n assert isinstance(result[obis.SHORT_POWER_FAILURE_COUNT].value, int)\n assert result[obis.SHORT_POWER_FAILURE_COUNT].value == 15\n\n # LONG_POWER_FAILURE_COUNT (96.7.9)\n assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT], CosemObject)\n assert result[obis.LONG_POWER_FAILURE_COUNT].unit is None\n assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT].value, int)\n assert result[obis.LONG_POWER_FAILURE_COUNT].value == 7\n\n # VOLTAGE_SAG_L1_COUNT (1-0:32.32.0)\n assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SAG_L1_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT].value, int)\n assert result[obis.VOLTAGE_SAG_L1_COUNT].value == 0\n\n # VOLTAGE_SAG_L2_COUNT (1-0:52.32.0)\n assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SAG_L2_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT].value, int)\n assert result[obis.VOLTAGE_SAG_L2_COUNT].value == 0\n\n # VOLTAGE_SAG_L3_COUNT (1-0:72.32.0)\n assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SAG_L3_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT].value, int)\n assert result[obis.VOLTAGE_SAG_L3_COUNT].value == 0\n\n # VOLTAGE_SWELL_L1_COUNT (1-0:32.36.0)\n assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SWELL_L1_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT].value, int)\n assert result[obis.VOLTAGE_SWELL_L1_COUNT].value == 0\n\n # VOLTAGE_SWELL_L2_COUNT (1-0:52.36.0)\n assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SWELL_L2_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT].value, int)\n assert result[obis.VOLTAGE_SWELL_L2_COUNT].value == 0\n\n # VOLTAGE_SWELL_L3_COUNT (1-0:72.36.0)\n assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT], CosemObject)\n assert result[obis.VOLTAGE_SWELL_L3_COUNT].unit is None\n assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT].value, int)\n assert result[obis.VOLTAGE_SWELL_L3_COUNT].value == 0\n\n # TEXT_MESSAGE_CODE (0-0:96.13.1)\n assert isinstance(result[obis.TEXT_MESSAGE_CODE], CosemObject)\n assert result[obis.TEXT_MESSAGE_CODE].unit is None\n assert result[obis.TEXT_MESSAGE_CODE].value is None\n\n # TEXT_MESSAGE (0-0:96.13.0)\n assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)\n assert result[obis.TEXT_MESSAGE].unit is None\n assert result[obis.TEXT_MESSAGE].value is None\n\n # INSTANTANEOUS_CURRENT_L1 (1-0:31.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L1].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L1].value == Decimal('0')\n\n # INSTANTANEOUS_CURRENT_L2 (1-0:51.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L2].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L2].value == Decimal('6')\n\n # INSTANTANEOUS_CURRENT_L3 (1-0:71.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3], CosemObject)\n assert result[obis.INSTANTANEOUS_CURRENT_L3].unit == 'A'\n assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3].value, Decimal)\n assert result[obis.INSTANTANEOUS_CURRENT_L3].value == Decimal('2')\n\n # DEVICE_TYPE (0-x:24.1.0)\n assert isinstance(result[obis.DEVICE_TYPE], CosemObject)\n assert result[obis.DEVICE_TYPE].unit is None\n assert isinstance(result[obis.DEVICE_TYPE].value, int)\n assert result[obis.DEVICE_TYPE].value == 3\n\n # INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE (1-0:21.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value == Decimal('0.170')\n\n # INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE (1-0:41.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value == Decimal('1.247')\n\n # INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE (1-0:61.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value == Decimal('0.209')\n\n # INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE (1-0:22.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value == Decimal('0')\n\n # INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE (1-0:42.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value == Decimal('0')\n\n # INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE (1-0:62.7.0)\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE], CosemObject)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].unit == 'kW'\n assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value, Decimal)\n assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value == Decimal('0')\n\n # EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)\n assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None\n assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)\n assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '4819243993373755377509728609491464'\n\n # HOURLY_GAS_METER_READING (0-1:24.2.1)\n assert isinstance(result[obis.HOURLY_GAS_METER_READING], MBusObject)\n assert result[obis.HOURLY_GAS_METER_READING].unit == 'm3'\n assert isinstance(result[obis.HOURLY_GAS_METER_READING].value, Decimal)\n assert result[obis.HOURLY_GAS_METER_READING].value == Decimal('981.443')\n\n # POWER_EVENT_FAILURE_LOG (99.97.0)\n # TODO to be implemented\n\n # ACTUAL_TRESHOLD_ELECTRICITY (0-0:17.0.0)\n # TODO to be implemented\n\n # ACTUAL_SWITCH_POSITION (0-0:96.3.10)\n # TODO to be implemented\n\n # VALVE_POSITION_GAS (0-x:24.4.0)\n # TODO to be implemented\n\n def test_checksum_valid(self):\n # No exception is raised.\n TelegramParser.validate_checksum(TELEGRAM_V4_2)\n\n def test_checksum_invalid(self):\n # Remove the electricty used data value. This causes the checksum to\n # not match anymore.\n corrupted_telegram = TELEGRAM_V4_2.replace(\n '1-0:1.8.1(001581.123*kWh)\\r\\n',\n ''\n )\n\n with self.assertRaises(InvalidChecksumError):\n TelegramParser.validate_checksum(corrupted_telegram)\n\n def test_checksum_missing(self):\n # Remove the checksum value causing a ParseError.\n corrupted_telegram = TELEGRAM_V4_2.replace('!6796\\r\\n', '')\n\n with self.assertRaises(ParseError):\n TelegramParser.validate_checksum(corrupted_telegram)\n",
|