fix: replace deprecated async_track_state_change with async_track_state_change_event#94
fix: replace deprecated async_track_state_change with async_track_state_change_event#94r0bb10 wants to merge 1 commit into
Conversation
…te_change_event async_track_state_change was deprecated in HA 2025.5 and scheduled for removal. This fix uses async_track_state_change_event on supported HA versions while falling back to the old function on older instances. Adds a try/except import for forward compatibility and an event adapter wrapper that extracts entity_id/old_state/new_state from the Event object to reuse the existing callback logic unchanged.
|
Hi, thanks for tackling this deprecation but there are a couple of issues that prevent merging as-is. 1. Unconditional import of In the 2. The Scattering the version check across every registration point makes the code fragile: any future call site would need the same A working fallback would look like this: try:
from homeassistant.helpers.event import async_track_state_change_event
except ImportError:
from homeassistant.helpers.event import async_track_state_change as _legacy
class _LegacyEvent:
def __init__(self, entity_id, old_state, new_state):
self.data = {"entity_id": entity_id, "old_state": old_state, "new_state": new_state}
def async_track_state_change_event(hass, entity_ids, action):
@callback
def _wrap(entity_id, old_state, new_state):
action(_LegacyEvent(entity_id, old_state, new_state))
return _legacy(hass, entity_ids, _wrap)With this shim in place, the call site needs no conditional at all. Let me know By(t)e |
Async_track_state_change was deprecated in HA 2025.5 and scheduled for removal. This fix uses async_track_state_change_event on supported HA versions while falling back to the old function on older instances.
Adds a try/except import for forward compatibility and an event adapter wrapper that extracts entity_id/old_state/new_state from the Event object to reuse the existing callback logic unchanged.