Repository: aequitas/python-rflink

Issue:
decode_packet: too many values to unpack (expected 2)
I use rflink integration in my HA setup and often get this in HA log:

> 2020-01-22 07:49:56 ERROR (MainThread) [rflink.protocol] failed to parse packet: None
> Traceback (most recent call last):
>   File "/usr/local/lib/python3.7/site-packages/rflink/protocol.py", line 152, in handle_raw_packet
>     packet = decode_packet(raw_packet)
>   File "/usr/local/lib/python3.7/site-packages/rflink/parser.py", line 308, in decode_packet
>     key, value = attr.lower().split("=")
> ValueError: too many values to unpack (expected 2)

Obvoiusly there is no impact on the functionality, but the messages flood the log.
I hope it's possible to rewrite the code so it account for situations like this.

Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 30fa75f..e52730d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,11 +7,13 @@ python:
 - 3.8
 - 2.7
 - pypy3
-install: pip install tox-travis python-coveralls codeclimate-test-reporter
+# python-coveralls 5.0 no longer supports python34
+install: pip install tox-travis codeclimate-test-reporter
 script: tox
 matrix:
   allow_failures:
   - python: 2.7
+  - python: 3.4
   - python: pypy3
   include:
     - python: 3.7
diff --git a/rflink/parser.py b/rflink/parser.py
index 05e7ea0..8a74a52 100644
--- a/rflink/parser.py
+++ b/rflink/parser.py
@@ -305,7 +305,7 @@ def decode_packet(packet: str) -> PacketType:
 
     # convert key=value pairs where needed
     for attr in filter(None, attrs.strip(DELIM).split(DELIM)):
-        key, value = attr.lower().split("=")
+        key, value = attr.lower().split("=", 1)
         if key in VALUE_TRANSLATION:
             value = VALUE_TRANSLATION[key](value)
         name = PACKET_FIELDS.get(key, key)
diff --git a/rflink/protocol.py b/rflink/protocol.py
index a3c54da..9922910 100644
--- a/rflink/protocol.py
+++ b/rflink/protocol.py
@@ -151,7 +151,7 @@ class PacketHandling(ProtocolBase):
         try:
             packet = decode_packet(raw_packet)
         except BaseException:
-            log.exception("failed to parse packet: %s", packet)
+            log.exception("failed to parse packet data: %s", raw_packet)
 
         log.debug("decoded packet: %s", packet)
 


Test patch:
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 3f55831..274e0ae 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -80,6 +80,10 @@ PROTOCOL_SAMPLES = os.path.join(os.path.dirname(__file__), "protocol_samples.txt
             "20;00;Internal Pullup on RF-in disabled;",
             {"message": "Internal Pullup on RF-in disabled"},
         ],
+        [
+            "20;9A;FA500;ID=0000db9e;SWITCH=01;CMD=SET_LEVEL=2;",
+            {"command": "set_level=2"},
+        ],
     ],
 )
 def test_packet_parsing(packet, expect):