From 72fc526ee8838de35dbf7acbb3e0875a0b34b022 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 16 Aug 2016 14:26:01 -0700 Subject: [PATCH] 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 --- homeassistant/components/notify/html5.py | 18 +++++++++++++++--- tests/components/notify/test_html5.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/notify/html5.py b/homeassistant/components/notify/html5.py index 70ab9580a04..04e49ecabca 100644 --- a/homeassistant/components/notify/html5.py +++ b/homeassistant/components/notify/html5.py @@ -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') diff --git a/tests/components/notify/test_html5.py b/tests/components/notify/test_html5.py index b562775d32e..121cc1096d2 100644 --- a/tests/components/notify/test_html5.py +++ b/tests/components/notify/test_html5.py @@ -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):