From a7ac103a75545b626a92e4c12868b96e208b1225 Mon Sep 17 00:00:00 2001 From: Johan Bloemberg Date: Fri, 6 Mar 2020 13:35:47 +0100 Subject: [PATCH] Warn instead of error when value can not be converted using expected type. --- rflink/parser.py | 14 +++++++++++++- setup.cfg | 5 +---- tests/test_parse.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/rflink/parser.py b/rflink/parser.py index 1f30223..451f14b 100644 --- a/rflink/parser.py +++ b/rflink/parser.py @@ -3,11 +3,14 @@ # ./.homeassistant/deps/lib/python/site-packages/rflink/parser.py # /Library/Frameworks/Python.framework/Versions/3.6//lib/python3.6/site-packages/rflink/parser.py +import logging import re import time from enum import Enum from typing import Any, Callable, DefaultDict, Dict, Generator, cast +log = logging.getLogger(__name__) + UNKNOWN = "unknown" SWITCH_COMMAND_TEMPLATE = "{node};{protocol};{id};{switch};{command};" PACKET_ID_SEP = "_" @@ -322,7 +325,16 @@ def decode_packet(packet: str) -> PacketType: continue key, value = attr.lower().split("=", 1) if key in VALUE_TRANSLATION: - value = VALUE_TRANSLATION[key](value) + try: + value = VALUE_TRANSLATION[key](value) + except ValueError: + log.warning( + "Could not convert attr '%s' value '%s' to expected type '%s'", + key, + value, + VALUE_TRANSLATION[key].__name__, + ) + continue name = PACKET_FIELDS.get(key, key) data[name] = value unit = UNITS.get(key, None) diff --git a/setup.cfg b/setup.cfg index 171e2e7..ec6e140 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,13 +1,10 @@ [pylama] -linters = pydocstyle,pycodestyle,pyflakes,mccabe,isort +linters = pydocstyle,pycodestyle,pyflakes,isort ignore = D213,E128,D203 [pycodestyle] max_line_length = 100 -[pylama:mccabe] -complexity=15 - [mypy] # 3.4 would be more correct, but when checking on later versions we may # have e.g. async_timeout which uses async and would thus error out with diff --git a/tests/test_parse.py b/tests/test_parse.py index 2ef57f7..81aec1e 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -139,3 +139,15 @@ def test_packet_valiation(packet): http://www.nemcon.nl/blog2/protref """ assert valid_packet(packet) + + +def test_invalid_type(): + """Packet where a value type cannot be converted to expected type should not error.""" + packet = "20;2D;RFX10METER;ID=79;TYPE=10;METER=7ef36;" + + assert decode_packet(packet) == { + "node": "gateway", + "protocol": "rfx10meter", + "id": "79", + "type": "10", + }