mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
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 <hexa@darmstadt.ccc.de>
This commit is contained in:
parent
0364498dee
commit
d7a005ad0f
@ -141,11 +141,23 @@ def _load_config(filename):
|
|||||||
return None
|
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):
|
def _save_config(filename, config):
|
||||||
"""Save configuration."""
|
"""Save configuration."""
|
||||||
try:
|
try:
|
||||||
with open(filename, 'w') as fdesc:
|
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:
|
except (IOError, TypeError) as error:
|
||||||
_LOGGER.error('Saving config file failed: %s', error)
|
_LOGGER.error('Saving config file failed: %s', error)
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user