From e7e785fd60a652acb0099600935817c2df3fb201 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 31 Mar 2019 11:04:41 +0200 Subject: [PATCH] Fix dashboard wizard unicode (#494) * Fix dashboard wizard unicode Fixes https://github.com/esphome/issues/issues/169 * Fix password md5 --- esphome/dashboard/dashboard.py | 6 +++--- esphome/espota2.py | 2 +- esphome/py_compat.py | 10 ++++++++++ esphome/wizard.py | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index a70fe2add2..6c1d9f2616 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -28,7 +28,7 @@ import tornado.websocket from esphome import const from esphome.__main__ import get_serial_ports from esphome.helpers import mkdir_p, get_bool_env, run_system_command -from esphome.py_compat import IS_PY2 +from esphome.py_compat import IS_PY2, decode_text from esphome.storage_json import EsphomeStorageJSON, StorageJSON, \ esphome_storage_path, ext_storage_path, trash_storage_path from esphome.util import shlex_quote @@ -223,8 +223,8 @@ class WizardRequestHandler(BaseHandler): def post(self): from esphome import wizard - kwargs = {k: ''.join(v) for k, v in self.request.arguments.items()} - destination = os.path.join(CONFIG_DIR, kwargs['name'] + '.yaml') + kwargs = {k: u''.join(decode_text(x) for x in v) for k, v in self.request.arguments.items()} + destination = os.path.join(CONFIG_DIR, kwargs['name'] + u'.yaml') wizard.wizard_write(path=destination, **kwargs) self.redirect('/?begin=True') diff --git a/esphome/espota2.py b/esphome/espota2.py index dbe7e94313..ac0dd33ebc 100755 --- a/esphome/espota2.py +++ b/esphome/espota2.py @@ -195,7 +195,7 @@ def perform_ota(sock, password, file_handle, filename): send_check(sock, cnonce, 'auth cnonce') result_md5 = hashlib.md5() - result_md5.update(password.encode()) + result_md5.update(password.encode('utf-8')) result_md5.update(nonce.encode()) result_md5.update(cnonce.encode()) result = result_md5.hexdigest() diff --git a/esphome/py_compat.py b/esphome/py_compat.py index 16a4d27ebf..4b48aa0f88 100644 --- a/esphome/py_compat.py +++ b/esphome/py_compat.py @@ -69,3 +69,13 @@ def indexbytes(buf, i): return buf[i] else: return ord(buf[i]) + + +if IS_PY2: + def decode_text(data, encoding='utf-8', errors='strict'): + # type: (str, str, str) -> unicode + return unicode(data, encoding='utf-8', errors=errors) +else: + def decode_text(data, encoding='utf-8', errors='strict'): + # type: (bytes, str, str) -> str + return data.decode(encoding='utf-8', errors=errors) diff --git a/esphome/wizard.py b/esphome/wizard.py index a2401584a1..abc2270235 100644 --- a/esphome/wizard.py +++ b/esphome/wizard.py @@ -79,7 +79,7 @@ def wizard_write(path, **kwargs): kwargs['platform'] = 'ESP8266' if board in ESP8266_BOARD_PINS else 'ESP32' platform = kwargs['platform'] - with codecs.open(path, 'w') as f_handle: + with codecs.open(path, 'w', 'utf-8') as f_handle: f_handle.write(wizard_file(**kwargs)) storage = StorageJSON.from_wizard(name, name + '.local', platform, board) storage_path = ext_storage_path(os.path.dirname(path), os.path.basename(path))