From 56ef9500f7621d7f114e09c68cfb373e4eae100a Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Thu, 4 Apr 2024 21:27:44 +0200 Subject: [PATCH] Use EventStateChangedData type when firing state changed event (#114740) --- homeassistant/core.py | 22 ++++++++++++++++++++-- homeassistant/helpers/event.py | 20 +++----------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 9a26a971f64..6e74c8bd010 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -161,6 +161,14 @@ class ConfigSource(enum.StrEnum): YAML = "yaml" +class EventStateChangedData(TypedDict): + """EventStateChanged data.""" + + entity_id: str + old_state: State | None + new_state: State | None + + # SOURCE_* are deprecated as of Home Assistant 2022.2, use ConfigSource instead _DEPRECATED_SOURCE_DISCOVERED = DeprecatedConstantEnum( ConfigSource.DISCOVERED, "2025.1" @@ -2019,9 +2027,14 @@ class StateMachine: return False old_state.expire() + state_changed_data: EventStateChangedData = { + "entity_id": entity_id, + "old_state": old_state, + "new_state": None, + } self._bus._async_fire( # pylint: disable=protected-access EVENT_STATE_CHANGED, - {"entity_id": entity_id, "old_state": old_state, "new_state": None}, + state_changed_data, context=context, ) return True @@ -2170,9 +2183,14 @@ class StateMachine: if old_state is not None: old_state.expire() self._states[entity_id] = state + state_changed_data: EventStateChangedData = { + "entity_id": entity_id, + "old_state": old_state, + "new_state": state, + } self._bus._async_fire( # pylint: disable=protected-access EVENT_STATE_CHANGED, - {"entity_id": entity_id, "old_state": old_state, "new_state": state}, + state_changed_data, context=context, time_fired=timestamp, ) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 749c6d3e6e4..cfbc40e7ed0 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -11,15 +11,7 @@ import functools as ft import logging from random import randint import time -from typing import ( - TYPE_CHECKING, - Any, - Concatenate, - Generic, - ParamSpec, - TypedDict, - TypeVar, -) +from typing import TYPE_CHECKING, Any, Concatenate, Generic, ParamSpec, TypeVar import attr @@ -33,6 +25,8 @@ from homeassistant.const import ( from homeassistant.core import ( CALLBACK_TYPE, Event, + # Explicit reexport of 'EventStateChangedData' for backwards compatibility + EventStateChangedData as EventStateChangedData, # noqa: PLC0414 HassJob, HassJobType, HomeAssistant, @@ -163,14 +157,6 @@ class TrackTemplateResult: result: Any -class EventStateChangedData(TypedDict): - """EventStateChanged data.""" - - entity_id: str - old_state: State | None - new_state: State | None - - def threaded_listener_factory( async_factory: Callable[Concatenate[HomeAssistant, _P], Any], ) -> Callable[Concatenate[HomeAssistant, _P], CALLBACK_TYPE]: