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"
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,
)

View File

@ -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]: