From d7a005ad0fa7a456a2dce375ed889c7117dd69d3 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Mon, 14 Nov 2016 05:08:56 +0100 Subject: [PATCH] notify.html5: decode bytes values in registration data Occassionally the values of `keys` and `p256h` are bytes objects instead of strings. As JSON by default does not serialize bytes objects let's decode bytes objects to unicode strings. Resolves the registration issue mentioned in #4012. Signed-off-by: Martin Weinelt --- homeassistant/components/notify/html5.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/html5.py b/homeassistant/components/notify/html5.py index 35f59af1135..c1456923a70 100644 --- a/homeassistant/components/notify/html5.py +++ b/homeassistant/components/notify/html5.py @@ -141,11 +141,23 @@ def _load_config(filename): return None +class JSONBytesDecoder(json.JSONEncoder): + """JSONEncoder to decode bytes objects to unicode.""" + + # pylint: disable=method-hidden + def default(self, obj): + """Decode object if it's a bytes object, else defer to baseclass.""" + if isinstance(obj, bytes): + return obj.decode() + return json.JSONEncoder.default(self, obj) + + def _save_config(filename, config): """Save configuration.""" try: with open(filename, 'w') as fdesc: - fdesc.write(json.dumps(config)) + fdesc.write(json.dumps( + config, cls=JSONBytesDecoder, indent=4, sort_keys=True)) except (IOError, TypeError) as error: _LOGGER.error('Saving config file failed: %s', error) return False