mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
notify.html5: use new json save and load functions (#10416)
* update to use new save_json and load_json * it is no longer possible to determine if the json file contains valid or empty data. * fix lint
This commit is contained in:
parent
c8648fbfb8
commit
59e943b3c1
@ -8,7 +8,6 @@ import asyncio
|
|||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
@ -16,6 +15,8 @@ from aiohttp.hdrs import AUTHORIZATION
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from voluptuous.humanize import humanize_error
|
from voluptuous.humanize import humanize_error
|
||||||
|
|
||||||
|
from homeassistant.util.json import load_json, save_json
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.components.frontend import add_manifest_json_key
|
from homeassistant.components.frontend import add_manifest_json_key
|
||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
@ -125,22 +126,12 @@ def get_service(hass, config, discovery_info=None):
|
|||||||
|
|
||||||
def _load_config(filename):
|
def _load_config(filename):
|
||||||
"""Load configuration."""
|
"""Load configuration."""
|
||||||
if not os.path.isfile(filename):
|
|
||||||
return {}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(filename, 'r') as fdesc:
|
return load_json(filename)
|
||||||
inp = fdesc.read()
|
except HomeAssistantError:
|
||||||
|
pass
|
||||||
# In case empty file
|
|
||||||
if not inp:
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
return json.loads(inp)
|
|
||||||
except (IOError, ValueError) as error:
|
|
||||||
_LOGGER.error("Reading config file %s failed: %s", filename, error)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class JSONBytesDecoder(json.JSONEncoder):
|
class JSONBytesDecoder(json.JSONEncoder):
|
||||||
"""JSONEncoder to decode bytes objects to unicode."""
|
"""JSONEncoder to decode bytes objects to unicode."""
|
||||||
@ -153,18 +144,6 @@ class JSONBytesDecoder(json.JSONEncoder):
|
|||||||
return json.JSONEncoder.default(self, obj)
|
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, cls=JSONBytesDecoder, indent=4, sort_keys=True))
|
|
||||||
except (IOError, TypeError) as error:
|
|
||||||
_LOGGER.error("Saving configuration file failed: %s", error)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class HTML5PushRegistrationView(HomeAssistantView):
|
class HTML5PushRegistrationView(HomeAssistantView):
|
||||||
"""Accepts push registrations from a browser."""
|
"""Accepts push registrations from a browser."""
|
||||||
|
|
||||||
@ -194,7 +173,7 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
|||||||
|
|
||||||
self.registrations[name] = data
|
self.registrations[name] = data
|
||||||
|
|
||||||
if not _save_config(self.json_path, self.registrations):
|
if not save_json(self.json_path, self.registrations):
|
||||||
return self.json_message(
|
return self.json_message(
|
||||||
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
@ -223,7 +202,7 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
|||||||
|
|
||||||
reg = self.registrations.pop(found)
|
reg = self.registrations.pop(found)
|
||||||
|
|
||||||
if not _save_config(self.json_path, self.registrations):
|
if not save_json(self.json_path, self.registrations):
|
||||||
self.registrations[found] = reg
|
self.registrations[found] = reg
|
||||||
return self.json_message(
|
return self.json_message(
|
||||||
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
||||||
@ -411,7 +390,7 @@ class HTML5NotificationService(BaseNotificationService):
|
|||||||
if response.status_code == 410:
|
if response.status_code == 410:
|
||||||
_LOGGER.info("Notification channel has expired")
|
_LOGGER.info("Notification channel has expired")
|
||||||
reg = self.registrations.pop(target)
|
reg = self.registrations.pop(target)
|
||||||
if not _save_config(self.registrations_json_path,
|
if not save_json(self.registrations_json_path,
|
||||||
self.registrations):
|
self.registrations):
|
||||||
self.registrations[target] = reg
|
self.registrations[target] = reg
|
||||||
_LOGGER.error("Error saving registration")
|
_LOGGER.error("Error saving registration")
|
||||||
|
@ -57,24 +57,13 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open()
|
m = mock_open()
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
|
|
||||||
assert service is not None
|
assert service is not None
|
||||||
|
|
||||||
def test_get_service_with_bad_json(self):
|
|
||||||
"""Test ."""
|
|
||||||
hass = MagicMock()
|
|
||||||
|
|
||||||
m = mock_open(read_data='I am not JSON')
|
|
||||||
with patch(
|
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
|
||||||
):
|
|
||||||
service = html5.get_service(hass, {})
|
|
||||||
|
|
||||||
assert service is None
|
|
||||||
|
|
||||||
@patch('pywebpush.WebPusher')
|
@patch('pywebpush.WebPusher')
|
||||||
def test_sending_message(self, mock_wp):
|
def test_sending_message(self, mock_wp):
|
||||||
"""Test sending message."""
|
"""Test sending message."""
|
||||||
@ -86,7 +75,8 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open(read_data=json.dumps(data))
|
m = mock_open(read_data=json.dumps(data))
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
||||||
|
|
||||||
@ -120,7 +110,8 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open()
|
m = mock_open()
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
@ -158,7 +149,8 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open()
|
m = mock_open()
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
@ -193,7 +185,8 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open()
|
m = mock_open()
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
@ -222,7 +215,7 @@ class TestHtml5Notify(object):
|
|||||||
}))
|
}))
|
||||||
assert resp.status == 400
|
assert resp.status == 400
|
||||||
|
|
||||||
with patch('homeassistant.components.notify.html5._save_config',
|
with patch('homeassistant.components.notify.html5.save_json',
|
||||||
return_value=False):
|
return_value=False):
|
||||||
# resp = view.post(Request(builder.get_environ()))
|
# resp = view.post(Request(builder.get_environ()))
|
||||||
resp = yield from client.post(REGISTER_URL, data=json.dumps({
|
resp = yield from client.post(REGISTER_URL, data=json.dumps({
|
||||||
@ -243,13 +236,11 @@ class TestHtml5Notify(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
m = mock_open(read_data=json.dumps(config))
|
m = mock_open(read_data=json.dumps(config))
|
||||||
|
with patch(
|
||||||
with patch('homeassistant.components.notify.html5.open', m,
|
'homeassistant.util.json.open',
|
||||||
create=True):
|
m, create=True
|
||||||
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
|
|
||||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
|
||||||
return_value=True):
|
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
|
|
||||||
assert service is not None
|
assert service is not None
|
||||||
@ -291,11 +282,10 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open(read_data=json.dumps(config))
|
m = mock_open(read_data=json.dumps(config))
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
|
||||||
return_value=True):
|
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
|
|
||||||
assert service is not None
|
assert service is not None
|
||||||
@ -335,11 +325,10 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open(read_data=json.dumps(config))
|
m = mock_open(read_data=json.dumps(config))
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
|
||||||
return_value=True):
|
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
|
|
||||||
assert service is not None
|
assert service is not None
|
||||||
@ -357,7 +346,7 @@ class TestHtml5Notify(object):
|
|||||||
client = yield from test_client(app)
|
client = yield from test_client(app)
|
||||||
hass.http.is_banned_ip.return_value = False
|
hass.http.is_banned_ip.return_value = False
|
||||||
|
|
||||||
with patch('homeassistant.components.notify.html5._save_config',
|
with patch('homeassistant.components.notify.html5.save_json',
|
||||||
return_value=False):
|
return_value=False):
|
||||||
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
|
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
|
||||||
'subscription': SUBSCRIPTION_1['subscription'],
|
'subscription': SUBSCRIPTION_1['subscription'],
|
||||||
@ -375,7 +364,8 @@ class TestHtml5Notify(object):
|
|||||||
|
|
||||||
m = mock_open()
|
m = mock_open()
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
service = html5.get_service(hass, {})
|
service = html5.get_service(hass, {})
|
||||||
@ -406,16 +396,15 @@ class TestHtml5Notify(object):
|
|||||||
hass = MagicMock()
|
hass = MagicMock()
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'device': SUBSCRIPTION_1,
|
'device': SUBSCRIPTION_1
|
||||||
}
|
}
|
||||||
|
|
||||||
m = mock_open(read_data=json.dumps(data))
|
m = mock_open(read_data=json.dumps(data))
|
||||||
with patch(
|
with patch(
|
||||||
'homeassistant.components.notify.html5.open', m, create=True
|
'homeassistant.util.json.open',
|
||||||
|
m, create=True
|
||||||
):
|
):
|
||||||
hass.config.path.return_value = 'file.conf'
|
hass.config.path.return_value = 'file.conf'
|
||||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
|
||||||
return_value=True):
|
|
||||||
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
||||||
|
|
||||||
assert service is not None
|
assert service is not None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user