Fix ESPHome event entity staying unavailable (#141650)

This commit is contained in:
Franck Nijhof 2025-03-28 13:55:11 +01:00 committed by GitHub
parent 0db643d9d1
commit d765936be3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 3 deletions

View File

@ -33,6 +33,16 @@ class EsphomeEvent(EsphomeEntity[EventInfo, Event], EventEntity):
self._trigger_event(self._state.event_type) self._trigger_event(self._state.event_type)
self.async_write_ha_state() self.async_write_ha_state()
@callback
def _on_device_update(self) -> None:
"""Call when device updates or entry data changes."""
super()._on_device_update()
if self._entry_data.available:
# Event entities should go available directly
# when the device comes online and not wait
# for the next data push.
self.async_write_ha_state()
async_setup_entry = partial( async_setup_entry = partial(
platform_async_setup_entry, platform_async_setup_entry,

View File

@ -4,6 +4,7 @@ from aioesphomeapi import APIClient, Event, EventInfo
import pytest import pytest
from homeassistant.components.event import EventDeviceClass from homeassistant.components.event import EventDeviceClass
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -11,9 +12,9 @@ from homeassistant.core import HomeAssistant
async def test_generic_event_entity( async def test_generic_event_entity(
hass: HomeAssistant, hass: HomeAssistant,
mock_client: APIClient, mock_client: APIClient,
mock_generic_device_entry, mock_esphome_device,
) -> None: ) -> None:
"""Test a generic event entity.""" """Test a generic event entity and its availability behavior."""
entity_info = [ entity_info = [
EventInfo( EventInfo(
object_id="myevent", object_id="myevent",
@ -26,13 +27,31 @@ async def test_generic_event_entity(
] ]
states = [Event(key=1, event_type="type1")] states = [Event(key=1, event_type="type1")]
user_service = [] user_service = []
await mock_generic_device_entry( device = await mock_esphome_device(
mock_client=mock_client, mock_client=mock_client,
entity_info=entity_info, entity_info=entity_info,
user_service=user_service, user_service=user_service,
states=states, states=states,
) )
await hass.async_block_till_done()
# Test initial state
state = hass.states.get("event.test_myevent") state = hass.states.get("event.test_myevent")
assert state is not None assert state is not None
assert state.state == "2024-04-24T00:00:00.000+00:00" assert state.state == "2024-04-24T00:00:00.000+00:00"
assert state.attributes["event_type"] == "type1" assert state.attributes["event_type"] == "type1"
# Test device becomes unavailable
await device.mock_disconnect(True)
await hass.async_block_till_done()
state = hass.states.get("event.test_myevent")
assert state.state == STATE_UNAVAILABLE
# Test device becomes available again
await device.mock_connect()
await hass.async_block_till_done()
# Event entity should be available immediately without waiting for data
state = hass.states.get("event.test_myevent")
assert state.state == "2024-04-24T00:00:00.000+00:00"
assert state.attributes["event_type"] == "type1"