Don't duplicate html5 registrations (#11451)
* Don't duplicate html5 registrations If a registration is posted and another registration with the same endpoint URL exists, update that one instead. That way, we preserve the device name that has been configured. The previous behavior used to append 'unnamed device' registrations over and over, leading to multiple copies of the same registration. The endpoint URL is unique per service worker so it is safe to update matching registrations. * Refactor html5 registration view to not write json in the event loop
This commit is contained in:
1 parent
01896786fb
commit
455c629f47
2 files changed
+172
-73
No files matched your search
@@ -169,15 +169,35 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
||||
return self.json_message(
|
||||
humanize_error(data, ex), HTTP_BAD_REQUEST)
|
||||
|
||||
name = ensure_unique_string('unnamed device', self.registrations)
|
||||
name = self.find_registration_name(data)
|
||||
previous_registration = self.registrations.get(name)
|
||||
|
||||
self.registrations[name] = data
|
||||
|
||||
if not save_json(self.json_path, self.registrations):
|
||||
try:
|
||||
hass = request.app['hass']
|
||||
|
||||
yield from hass.async_add_job(save_json, self.json_path,
|
||||
self.registrations)
|
||||
return self.json_message(
|
||||
'Push notification subscriber registered.')
|
||||
except HomeAssistantError:
|
||||
if previous_registration is not None:
|
||||
self.registrations[name] = previous_registration
|
||||
else:
|
||||
self.registrations.pop(name)
|
||||
|
||||
return self.json_message(
|
||||
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
||||
|
||||
return self.json_message('Push notification subscriber registered.')
|
||||
def find_registration_name(self, data):
|
||||
"""Find a registration name matching data or generate a unique one."""
|
||||
endpoint = data.get(ATTR_SUBSCRIPTION).get(ATTR_ENDPOINT)
|
||||
for key, registration in self.registrations.items():
|
||||
subscription = registration.get(ATTR_SUBSCRIPTION)
|
||||
if subscription.get(ATTR_ENDPOINT) == endpoint:
|
||||
return key
|
||||
return ensure_unique_string('unnamed device', self.registrations)
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete(self, request):
|
||||
@@ -202,7 +222,12 @@ class HTML5PushRegistrationView(HomeAssistantView):
|
||||
|
||||
reg = self.registrations.pop(found)
|
||||
|
||||
if not save_json(self.json_path, self.registrations):
|
||||
try:
|
||||
hass = request.app['hass']
|
||||
|
||||
yield from hass.async_add_job(save_json, self.json_path,
|
||||
self.registrations)
|
||||
except HomeAssistantError:
|
||||
self.registrations[found] = reg
|
||||
return self.json_message(
|
||||
'Error saving registration.', HTTP_INTERNAL_SERVER_ERROR)
|
||||
|
||||
Reference in new issue
Block a user