From 16eeb3af311ed55bb3b52f368f398a1163c7feb3 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 5 Jun 2018 23:19:46 +0200 Subject: [PATCH] Attempt to solve UnicodeEncodeError in HassIO when running on RPi --- esphomeyaml/__main__.py | 11 +++++++---- esphomeyaml/mqtt.py | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esphomeyaml/__main__.py b/esphomeyaml/__main__.py index 1462cdf29e..36c5414b58 100644 --- a/esphomeyaml/__main__.py +++ b/esphomeyaml/__main__.py @@ -102,12 +102,15 @@ def run_miniterm(config, port, escape=False): with serial.Serial(port, baudrate=baud_rate) as ser: while True: - line = ser.readline() + line = ser.readline().replace('\r', '').replace('\n', '') time = datetime.now().time().strftime('[%H:%M:%S]') - message = time + line.decode('unicode-escape').replace('\r', '').replace('\n', '') + message = time + line if escape: - message = message.replace('\033', '\\033').encode('ascii', 'replace') - print(message) + message = message.replace('\033', '\\033') + try: + print(message) + except UnicodeEncodeError: + print(message.encode('ascii', 'backslashreplace')) def write_cpp(config): diff --git a/esphomeyaml/mqtt.py b/esphomeyaml/mqtt.py index 4e4b75a17d..3497e600a1 100644 --- a/esphomeyaml/mqtt.py +++ b/esphomeyaml/mqtt.py @@ -57,10 +57,13 @@ def show_logs(config, topic=None, username=None, password=None, client_id=None, def on_message(client, userdata, msg): time = datetime.now().time().strftime(u'[%H:%M:%S]') - message = msg.payload.decode('utf-8') + message = time + msg.payload if escape: message = message.replace('\033', '\\033') - print(time + message) + try: + print(message) + except UnicodeEncodeError: + print(message.encode('ascii', 'backslashreplace')) return initialize(config, [topic], on_message, username, password, client_id)