1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-21 10:27:52 +00:00

Don't set last notification timestamp when sending message failed (#163251)

This commit is contained in:
Manu
2026-02-19 00:48:01 +01:00
committed by GitHub
parent b398197c07
commit 2fcbd77c95
2 changed files with 60 additions and 1 deletions

View File

@@ -161,9 +161,9 @@ class NotifyEntity(RestoreEntity):
Should not be overridden, handle setting last notification timestamp.
"""
await self.async_send_message(**kwargs)
self.__set_state(dt_util.utcnow().isoformat())
self.async_write_ha_state()
await self.async_send_message(**kwargs)
def send_message(self, message: str, title: str | None = None) -> None:
"""Send a message."""

View File

@@ -17,6 +17,7 @@ from homeassistant.components.notify import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError
from tests.common import (
MockConfigEntry,
@@ -52,6 +53,17 @@ class MockNotifyEntityNonAsync(MockEntity, NotifyEntity):
self.send_message_mock_calls(message, title=title)
class MockNotifyEntityWithException(MockEntity, NotifyEntity):
"""Mock Email notitier entity to use in tests."""
send_message_mock_calls = MagicMock()
async def async_send_message(self, message: str, title: str | None = None) -> None:
"""Send a notification message."""
self.send_message_mock_calls(message, title=title)
raise HomeAssistantError
async def help_async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
@@ -188,6 +200,53 @@ async def test_send_message_service_with_title(
)
async def test_send_message_exception(
hass: HomeAssistant, config_flow_fixture: None
) -> None:
"""Test send_message service."""
entity = MockNotifyEntityWithException(
name="test",
entity_id="notify.test",
supported_features=NotifyEntityFeature.TITLE,
)
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
mock_integration(
hass,
MockModule(
"test",
async_setup_entry=help_async_setup_entry_init,
async_unload_entry=help_async_unload_entry,
),
)
setup_test_component_platform(hass, DOMAIN, [entity], from_config_entry=True)
assert await hass.config_entries.async_setup(config_entry.entry_id)
state = hass.states.get("notify.test")
assert state.state is STATE_UNKNOWN
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SEND_MESSAGE,
copy.deepcopy(TEST_KWARGS_TITLE) | {"entity_id": "notify.test"},
blocking=True,
)
await hass.async_block_till_done()
entity.send_message_mock_calls.assert_called_once_with(
TEST_KWARGS_TITLE[notify.ATTR_MESSAGE],
title=TEST_KWARGS_TITLE[notify.ATTR_TITLE],
)
# assert last notification timestamp has not been updated
state = hass.states.get("notify.test")
assert state.state is STATE_UNKNOWN
@pytest.mark.parametrize(
("state", "init_state"),
[