Skip to content

Commit

Permalink
Warn instead of error when value can not be converted using expected …
Browse files Browse the repository at this point in the history
…type.
  • Loading branch information
Johan Bloemberg committed Mar 6, 2020
1 parent 63ae866 commit a7ac103
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
14 changes: 13 additions & 1 deletion rflink/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "_"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

0 comments on commit a7ac103

Please sign in to comment.