Attempt to solve UnicodeEncodeError in HassIO when running on RPi

This commit is contained in:
Otto Winter 2018-06-05 23:19:46 +02:00
parent b799d2df7f
commit 16eeb3af31
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
2 changed files with 12 additions and 6 deletions

View file

@ -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')
message = message.replace('\033', '\\033')
try:
print(message)
except UnicodeEncodeError:
print(message.encode('ascii', 'backslashreplace'))
def write_cpp(config):

View file

@ -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)