Use EventStateChangedData type when firing state changed event (#114740)

This commit is contained in:
Marc Mueller 2024-04-04 21:27:44 +02:00 committed by GitHub
parent 9189cd5ec2
commit 56ef9500f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 19 deletions

View File

@ -161,6 +161,14 @@ class ConfigSource(enum.StrEnum):
YAML = "yaml" 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 # SOURCE_* are deprecated as of Home Assistant 2022.2, use ConfigSource instead
_DEPRECATED_SOURCE_DISCOVERED = DeprecatedConstantEnum( _DEPRECATED_SOURCE_DISCOVERED = DeprecatedConstantEnum(
ConfigSource.DISCOVERED, "2025.1" ConfigSource.DISCOVERED, "2025.1"
@ -2019,9 +2027,14 @@ class StateMachine:
return False return False
old_state.expire() 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 self._bus._async_fire( # pylint: disable=protected-access
EVENT_STATE_CHANGED, EVENT_STATE_CHANGED,
{"entity_id": entity_id, "old_state": old_state, "new_state": None}, state_changed_data,
context=context, context=context,
) )
return True return True
@ -2170,9 +2183,14 @@ class StateMachine:
if old_state is not None: if old_state is not None:
old_state.expire() old_state.expire()
self._states[entity_id] = state 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 self._bus._async_fire( # pylint: disable=protected-access
EVENT_STATE_CHANGED, EVENT_STATE_CHANGED,
{"entity_id": entity_id, "old_state": old_state, "new_state": state}, state_changed_data,
context=context, context=context,
time_fired=timestamp, time_fired=timestamp,
) )

View File

@ -11,15 +11,7 @@ import functools as ft
import logging import logging
from random import randint from random import randint
import time import time
from typing import ( from typing import TYPE_CHECKING, Any, Concatenate, Generic, ParamSpec, TypeVar
TYPE_CHECKING,
Any,
Concatenate,
Generic,
ParamSpec,
TypedDict,
TypeVar,
)
import attr import attr
@ -33,6 +25,8 @@ from homeassistant.const import (
from homeassistant.core import ( from homeassistant.core import (
CALLBACK_TYPE, CALLBACK_TYPE,
Event, Event,
# Explicit reexport of 'EventStateChangedData' for backwards compatibility
EventStateChangedData as EventStateChangedData, # noqa: PLC0414
HassJob, HassJob,
HassJobType, HassJobType,
HomeAssistant, HomeAssistant,
@ -163,14 +157,6 @@ class TrackTemplateResult:
result: Any result: Any
class EventStateChangedData(TypedDict):
"""EventStateChanged data."""
entity_id: str
old_state: State | None
new_state: State | None
def threaded_listener_factory( def threaded_listener_factory(
async_factory: Callable[Concatenate[HomeAssistant, _P], Any], async_factory: Callable[Concatenate[HomeAssistant, _P], Any],
) -> Callable[Concatenate[HomeAssistant, _P], CALLBACK_TYPE]: ) -> Callable[Concatenate[HomeAssistant, _P], CALLBACK_TYPE]: