Fix error handling for expired html5 registration (#67676)

This commit is contained in:
Eric Severance 2022-03-30 04:30:08 -07:00 committed by GitHub
parent 9f0665bbb4
commit 3953b6abe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -533,7 +533,9 @@ class HTML5NotificationService(BaseNotificationService):
if response.status_code == 410:
_LOGGER.info("Notification channel has expired")
reg = self.registrations.pop(target)
if not save_json(self.registrations_json_path, self.registrations):
try:
save_json(self.registrations_json_path, self.registrations)
except HomeAssistantError:
self.registrations[target] = reg
_LOGGER.error("Error saving registration")
else:

View file

@ -527,3 +527,46 @@ async def test_send_fcm_without_targets(hass, hass_client):
assert mock_wp.mock_calls[0][1][0] == SUBSCRIPTION_5["subscription"]
# Third mock_call checks the status_code of the response.
assert mock_wp.mock_calls[2][0] == "().send().status_code.__eq__"
async def test_send_fcm_expired(hass, hass_client):
"""Test that the FCM target is removed when expired."""
registrations = {"device": SUBSCRIPTION_5}
await mock_client(hass, hass_client, registrations)
with (
patch("homeassistant.components.html5.notify.WebPusher") as mock_wp,
patch("homeassistant.components.html5.notify.save_json"),
):
mock_wp().send().status_code = 410
await hass.services.async_call(
"notify",
"notify",
{"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}},
blocking=True,
)
# "device" should be removed when expired.
assert "device" not in registrations
async def test_send_fcm_expired_save_fails(hass, hass_client):
"""Test that the FCM target remains after expiry if save_json fails."""
registrations = {"device": SUBSCRIPTION_5}
await mock_client(hass, hass_client, registrations)
with (
patch("homeassistant.components.html5.notify.WebPusher") as mock_wp,
patch(
"homeassistant.components.html5.notify.save_json",
side_effect=HomeAssistantError(),
),
):
mock_wp().send().status_code = 410
await hass.services.async_call(
"notify",
"notify",
{"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}},
blocking=True,
)
# "device" should still exist if save fails.
assert "device" in registrations