Html5 notifications improvements (#2840)

* Retry sending the push for 1 day instead of failing instantly if the target is unavailable

* Add timestamp to push payload

* Correctly use the title and body fields for their intended purposes

* Add callback support

* Revert changes to frontend files.

* Add default URL which will open Home Assistant. Also put all the data into the data object of the payload so it is accessible in the browser. Without doing this, things like URL wouldnt be accessible.

* Flake8 and pylint fixes

* event->type

* Dont send the default url if actions exist

* flake8/pylint fixes again

* Update html5 tests

* Remove callbacks from this branch, will re-stage on a different branch

* Remove remnant of callbacks

* Add url to data dictionary if it exists instead of copying the entire data dictionary in

* flake8 fix
This commit is contained in:
Robbie Trencheny 2016-08-16 14:26:01 -07:00 committed by GitHub
parent 822b7f8770
commit 72fc526ee8
2 changed files with 16 additions and 4 deletions

View file

@ -7,6 +7,7 @@ https://home-assistant.io/components/notify.html5/
import os
import logging
import json
import time
import voluptuous as vol
from voluptuous.humanize import humanize_error
@ -15,7 +16,7 @@ from homeassistant.const import (
HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR)
from homeassistant.util import ensure_unique_string
from homeassistant.components.notify import (
ATTR_TARGET, ATTR_DATA, BaseNotificationService,
ATTR_TARGET, ATTR_TITLE, ATTR_DATA, BaseNotificationService,
PLATFORM_SCHEMA)
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.frontend import add_manifest_json_key
@ -145,9 +146,14 @@ class HTML5NotificationService(BaseNotificationService):
"""Send a message to a user."""
from pywebpush import WebPusher
timestamp = int(time.time())
payload = {
'title': message,
'body': message,
'data': {},
'icon': '/static/icons/favicon-192x192.png',
'timestamp': (timestamp*1000), # Javascript ms since epoch
'title': kwargs.get(ATTR_TITLE)
}
data = kwargs.get(ATTR_DATA)
@ -155,6 +161,12 @@ class HTML5NotificationService(BaseNotificationService):
if data:
payload.update(data)
if data.get('url') is not None:
payload['data']['url'] = data.get('url')
elif (payload['data'].get('url') is None and
payload.get('actions') is None):
payload['data']['url'] = '/'
targets = kwargs.get(ATTR_TARGET)
if not targets:
@ -170,4 +182,4 @@ class HTML5NotificationService(BaseNotificationService):
continue
WebPusher(info[ATTR_SUBSCRIPTION]).send(
json.dumps(payload), gcm_key=self._gcm_key, ttl='0')
json.dumps(payload), gcm_key=self._gcm_key, ttl='86400')

View file

@ -65,7 +65,7 @@ class TestHtml5Notify(object):
# Call to send
payload = json.loads(mock_wp.mock_calls[1][1][0])
assert payload['title'] == 'Hello'
assert payload['body'] == 'Hello'
assert payload['icon'] == 'beer.png'
def test_registering_new_device_view(self):