Refactored to new global json saving and loading (#10677)

* Refactored to new global json saving and loading

* Fixed emulated_hue tests

* Removed unnecassary error handling

* Added missing newline

* Remove unused imports

* Fixed linting error

* Moved _load_json wrapper out of the config class
This commit is contained in:
Markus Nigbur 2017-11-20 04:47:55 +01:00 committed by Paulus Schoutsen
parent 7695ca2c8b
commit a83e741dc7
15 changed files with 73 additions and 348 deletions

View file

@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.matrix/
"""
import logging
import json
import os
from urllib.parse import urlparse
@ -15,6 +14,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (ATTR_TARGET, PLATFORM_SCHEMA,
BaseNotificationService)
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_VERIFY_SSL
from homeassistant.util.json import load_json, save_json
REQUIREMENTS = ['matrix-client==0.0.6']
@ -82,8 +82,7 @@ class MatrixNotificationService(BaseNotificationService):
return {}
try:
with open(self.session_filepath) as handle:
data = json.load(handle)
data = load_json(self.session_filepath)
auth_tokens = {}
for mx_id, token in data.items():
@ -101,16 +100,7 @@ class MatrixNotificationService(BaseNotificationService):
"""Store authentication token to session and persistent storage."""
self.auth_tokens[self.mx_id] = token
try:
with open(self.session_filepath, 'w') as handle:
handle.write(json.dumps(self.auth_tokens))
# Not saving the tokens to disk should not stop the client, we can just
# login using the password every time.
except (OSError, IOError, PermissionError) as ex:
_LOGGER.warning(
"Storing authentication tokens to file '%s' failed: %s",
self.session_filepath, str(ex))
save_json(self.session_filepath, self.auth_tokens)
def login(self):
"""Login to the matrix homeserver and return the client instance."""