mirror of
https://github.com/home-assistant/core.git
synced 2026-10-06 14:29:21 -04:00
Fix alert not firing when the watched entity is already in the alert … (#181218)
Co-authored-by: Sebastian Messmer <2373925+smessmer@users.noreply.github.com>
This commit is contained in:
co-authored by
Sebastian Messmer
parent
252d27bc85
commit
83300bc684
@@ -72,12 +72,24 @@ class AlertEntity(Entity):
|
||||
self._ack = False
|
||||
self._cancel: Callable[[], None] | None = None
|
||||
self._send_done_message = False
|
||||
self._watched_entity_id = watched_entity_id
|
||||
self.entity_id = f"{DOMAIN}.{entity_id}"
|
||||
|
||||
async_track_state_change_event(
|
||||
hass, [watched_entity_id], self.watched_entity_change
|
||||
)
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Start alerting if the watched entity is already in the alert state."""
|
||||
await super().async_added_to_hass()
|
||||
if (
|
||||
(state := self.hass.states.get(self._watched_entity_id)) is not None
|
||||
and state.state == self._alert_state
|
||||
and not self._firing
|
||||
):
|
||||
await self.begin_alerting()
|
||||
|
||||
@property
|
||||
@override
|
||||
def state(self) -> str:
|
||||
|
||||
@@ -94,6 +94,32 @@ async def test_fire(hass: HomeAssistant, mock_notifier: list[ServiceCall]) -> No
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
||||
|
||||
|
||||
async def test_fire_when_already_in_alert_state(
|
||||
hass: HomeAssistant, mock_notifier: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test the alert fires if the watched entity is already in the alert state.
|
||||
|
||||
This is the case after a restart when the watched entity's state is
|
||||
restored before the alert subscribes to state changes.
|
||||
"""
|
||||
hass.states.async_set("sensor.test", STATE_ON)
|
||||
assert await async_setup_component(hass, DOMAIN, TEST_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
||||
assert len(mock_notifier) == 1
|
||||
|
||||
|
||||
async def test_idle_when_already_not_in_alert_state(
|
||||
hass: HomeAssistant, mock_notifier: list[ServiceCall]
|
||||
) -> None:
|
||||
"""Test the alert stays idle if the watched entity is not in the alert state."""
|
||||
hass.states.async_set("sensor.test", STATE_OFF)
|
||||
assert await async_setup_component(hass, DOMAIN, TEST_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_IDLE
|
||||
assert len(mock_notifier) == 0
|
||||
|
||||
|
||||
async def test_silence(hass: HomeAssistant, mock_notifier: list[ServiceCall]) -> None:
|
||||
"""Test silencing the alert."""
|
||||
assert await async_setup_component(hass, DOMAIN, TEST_CONFIG)
|
||||
|
||||
Reference in New Issue
Block a user