From 31f411868c687b5fa36202cdaea37928452e6f18 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 2 Apr 2023 02:43:06 +0200 Subject: [PATCH] `Message.Encoder` should serialize binary data to `0x`-led hex strings. --- platypush/entities/sensors.py | 11 ++++++++--- platypush/message/__init__.py | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/platypush/entities/sensors.py b/platypush/entities/sensors.py index 0a2c098681..3b57e02db3 100644 --- a/platypush/entities/sensors.py +++ b/platypush/entities/sensors.py @@ -57,9 +57,14 @@ if 'raw_sensor' not in Base.metadata: @property def value(self): - if self.is_binary: - return self._value.decode() - if self.is_json: + if self._value is None: + return None + if self.is_binary and isinstance(self._value, str): + value = self._value[2:] + return bytes( + [int(value[i : i + 2], 16) for i in range(0, len(value), 2)] + ) + if self.is_json and isinstance(self._value, (str, bytes)): return json.loads(self._value) return self._value diff --git a/platypush/message/__init__.py b/platypush/message/__init__.py index 1fe399417d..eaed19e936 100644 --- a/platypush/message/__init__.py +++ b/platypush/message/__init__.py @@ -44,6 +44,8 @@ class Message: return obj.tolist() if isinstance(obj, decimal.Decimal): return float(obj) + if isinstance(obj, (bytes, bytearray)): + return '0x' + ''.join([f'{x:02x}' for x in obj]) if callable(obj): return ''.format(obj.__module__, obj.__name__)