mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Cleanup EventType typing (#97136)
This commit is contained in:
parent
4161f53bea
commit
995c29e052
@ -5,8 +5,10 @@ from dataclasses import dataclass
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from homeassistant.components.recorder import get_instance, history
|
from homeassistant.components.recorder import get_instance, history
|
||||||
from homeassistant.core import Event, HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
|
from homeassistant.helpers.event import EventStateChangedData
|
||||||
from homeassistant.helpers.template import Template
|
from homeassistant.helpers.template import Template
|
||||||
|
from homeassistant.helpers.typing import EventType
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .helpers import async_calculate_period, floored_timestamp
|
from .helpers import async_calculate_period, floored_timestamp
|
||||||
@ -55,7 +57,9 @@ class HistoryStats:
|
|||||||
self._start = start
|
self._start = start
|
||||||
self._end = end
|
self._end = end
|
||||||
|
|
||||||
async def async_update(self, event: Event | None) -> HistoryStatsState:
|
async def async_update(
|
||||||
|
self, event: EventType[EventStateChangedData] | None
|
||||||
|
) -> HistoryStatsState:
|
||||||
"""Update the stats at a given time."""
|
"""Update the stats at a given time."""
|
||||||
# Get previous values of start and end
|
# Get previous values of start and end
|
||||||
previous_period_start, previous_period_end = self._period
|
previous_period_start, previous_period_end = self._period
|
||||||
@ -104,8 +108,7 @@ class HistoryStats:
|
|||||||
)
|
)
|
||||||
):
|
):
|
||||||
new_data = False
|
new_data = False
|
||||||
if event and event.data["new_state"] is not None:
|
if event and (new_state := event.data["new_state"]) is not None:
|
||||||
new_state: State = event.data["new_state"]
|
|
||||||
if (
|
if (
|
||||||
current_period_start_timestamp
|
current_period_start_timestamp
|
||||||
<= floored_timestamp(new_state.last_changed)
|
<= floored_timestamp(new_state.last_changed)
|
||||||
|
@ -129,7 +129,7 @@ async def async_attach_trigger(
|
|||||||
_variables = trigger_info["variables"] or {}
|
_variables = trigger_info["variables"] or {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def state_automation_listener(event: EventType[EventStateChangedData]):
|
def state_automation_listener(event: EventType[EventStateChangedData]) -> None:
|
||||||
"""Listen for state changes and calls action."""
|
"""Listen for state changes and calls action."""
|
||||||
entity = event.data["entity_id"]
|
entity = event.data["entity_id"]
|
||||||
from_s = event.data["old_state"]
|
from_s = event.data["old_state"]
|
||||||
|
@ -37,9 +37,11 @@ from homeassistant.const import (
|
|||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, State, callback, split_entity_id
|
from homeassistant.core import HomeAssistant, State, callback, split_entity_id
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.event import EventStateChangedData
|
||||||
from homeassistant.helpers.storage import STORAGE_DIR
|
from homeassistant.helpers.storage import STORAGE_DIR
|
||||||
|
from homeassistant.helpers.typing import EventType
|
||||||
from homeassistant.util.unit_conversion import TemperatureConverter
|
from homeassistant.util.unit_conversion import TemperatureConverter
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -619,9 +621,9 @@ def state_needs_accessory_mode(state: State) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def state_changed_event_is_same_state(event: Event) -> bool:
|
def state_changed_event_is_same_state(event: EventType[EventStateChangedData]) -> bool:
|
||||||
"""Check if a state changed event is the same state."""
|
"""Check if a state changed event is the same state."""
|
||||||
event_data = event.data
|
event_data = event.data
|
||||||
old_state: State | None = event_data.get("old_state")
|
old_state = event_data["old_state"]
|
||||||
new_state: State | None = event_data.get("new_state")
|
new_state = event_data["new_state"]
|
||||||
return bool(new_state and old_state and new_state.state == old_state.state)
|
return bool(new_state and old_state and new_state.state == old_state.state)
|
||||||
|
@ -179,7 +179,7 @@ class Plant(Entity):
|
|||||||
self._brightness_history = DailyHistory(self._conf_check_days)
|
self._brightness_history = DailyHistory(self._conf_check_days)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _state_changed_event(self, event: EventType[EventStateChangedData]):
|
def _state_changed_event(self, event: EventType[EventStateChangedData]) -> None:
|
||||||
"""Sensor state change event."""
|
"""Sensor state change event."""
|
||||||
self.state_changed(event.data["entity_id"], event.data["new_state"])
|
self.state_changed(event.data["entity_id"], event.data["new_state"])
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ import voluptuous as vol
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
aiohttp_client,
|
aiohttp_client,
|
||||||
@ -23,13 +23,17 @@ from homeassistant.helpers import (
|
|||||||
device_registry as dr,
|
device_registry as dr,
|
||||||
entity_registry as er,
|
entity_registry as er,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import (
|
||||||
|
EventStateChangedData,
|
||||||
|
async_track_state_change_event,
|
||||||
|
)
|
||||||
from homeassistant.helpers.selector import (
|
from homeassistant.helpers.selector import (
|
||||||
SelectOptionDict,
|
SelectOptionDict,
|
||||||
SelectSelector,
|
SelectSelector,
|
||||||
SelectSelectorConfig,
|
SelectSelectorConfig,
|
||||||
SelectSelectorMode,
|
SelectSelectorMode,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.typing import EventType
|
||||||
|
|
||||||
from .const import CONF_SENSOR_INDICES, CONF_SHOW_ON_MAP, DOMAIN, LOGGER
|
from .const import CONF_SENSOR_INDICES, CONF_SHOW_ON_MAP, DOMAIN, LOGGER
|
||||||
|
|
||||||
@ -420,7 +424,9 @@ class PurpleAirOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
device_entities_removed_event = asyncio.Event()
|
device_entities_removed_event = asyncio.Event()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_device_entity_state_changed(_: Event) -> None:
|
def async_device_entity_state_changed(
|
||||||
|
_: EventType[EventStateChangedData],
|
||||||
|
) -> None:
|
||||||
"""Listen and respond when all device entities are removed."""
|
"""Listen and respond when all device entities are removed."""
|
||||||
if all(
|
if all(
|
||||||
self.hass.states.get(entity_entry.entity_id) is None
|
self.hass.states.get(entity_entry.entity_id) is None
|
||||||
|
@ -17,9 +17,11 @@ from homeassistant.const import (
|
|||||||
SERVICE_TURN_ON,
|
SERVICE_TURN_ON,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.event import EventStateChangedData
|
||||||
|
from homeassistant.helpers.typing import EventType
|
||||||
|
|
||||||
from .entity import BaseEntity
|
from .entity import BaseEntity
|
||||||
|
|
||||||
@ -74,7 +76,9 @@ class CoverSwitch(BaseEntity, CoverEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed_listener(self, event: Event | None = None) -> None:
|
def async_state_changed_listener(
|
||||||
|
self, event: EventType[EventStateChangedData] | None = None
|
||||||
|
) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
super().async_state_changed_listener(event)
|
super().async_state_changed_listener(event)
|
||||||
if (
|
if (
|
||||||
|
@ -12,7 +12,7 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.entity import DeviceInfo, Entity, ToggleEntity
|
from homeassistant.helpers.entity import DeviceInfo, Entity, ToggleEntity
|
||||||
from homeassistant.helpers.event import (
|
from homeassistant.helpers.event import (
|
||||||
@ -67,7 +67,9 @@ class BaseEntity(Entity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed_listener(self, event: Event | None = None) -> None:
|
def async_state_changed_listener(
|
||||||
|
self, event: EventType[EventStateChangedData] | None = None
|
||||||
|
) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
if (
|
if (
|
||||||
state := self.hass.states.get(self._switch_entity_id)
|
state := self.hass.states.get(self._switch_entity_id)
|
||||||
@ -163,7 +165,9 @@ class BaseToggleEntity(BaseEntity, ToggleEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed_listener(self, event: Event | None = None) -> None:
|
def async_state_changed_listener(
|
||||||
|
self, event: EventType[EventStateChangedData] | None = None
|
||||||
|
) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
super().async_state_changed_listener(event)
|
super().async_state_changed_listener(event)
|
||||||
if (
|
if (
|
||||||
|
@ -13,9 +13,11 @@ from homeassistant.const import (
|
|||||||
SERVICE_TURN_ON,
|
SERVICE_TURN_ON,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.event import EventStateChangedData
|
||||||
|
from homeassistant.helpers.typing import EventType
|
||||||
|
|
||||||
from .entity import BaseEntity
|
from .entity import BaseEntity
|
||||||
|
|
||||||
@ -68,7 +70,9 @@ class LockSwitch(BaseEntity, LockEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_state_changed_listener(self, event: Event | None = None) -> None:
|
def async_state_changed_listener(
|
||||||
|
self, event: EventType[EventStateChangedData] | None = None
|
||||||
|
) -> None:
|
||||||
"""Handle child updates."""
|
"""Handle child updates."""
|
||||||
super().async_state_changed_listener(event)
|
super().async_state_changed_listener(event)
|
||||||
if (
|
if (
|
||||||
|
@ -26,7 +26,7 @@ from homeassistant.const import (
|
|||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, CoreState, Event, HomeAssistant, State, callback
|
from homeassistant.core import Context, CoreState, HomeAssistant, State, callback
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
|
from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ class _TemplateAttribute:
|
|||||||
@callback
|
@callback
|
||||||
def handle_result(
|
def handle_result(
|
||||||
self,
|
self,
|
||||||
event: Event | None,
|
event: EventType[EventStateChangedData] | None,
|
||||||
template: Template,
|
template: Template,
|
||||||
last_result: str | None | TemplateError,
|
last_result: str | None | TemplateError,
|
||||||
result: str | TemplateError,
|
result: str | TemplateError,
|
||||||
|
@ -544,14 +544,14 @@ async def test_async_track_state_added_domain(hass: HomeAssistant) -> None:
|
|||||||
multiple_entity_id_tracker = []
|
multiple_entity_id_tracker = []
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def single_run_callback(event: EventType[EventStateChangedData]):
|
def single_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
single_entity_id_tracker.append((old_state, new_state))
|
single_entity_id_tracker.append((old_state, new_state))
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def multiple_run_callback(event: EventType[EventStateChangedData]):
|
def multiple_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
@ -656,14 +656,14 @@ async def test_async_track_state_removed_domain(hass: HomeAssistant) -> None:
|
|||||||
multiple_entity_id_tracker = []
|
multiple_entity_id_tracker = []
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def single_run_callback(event: EventType[EventStateChangedData]):
|
def single_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
single_entity_id_tracker.append((old_state, new_state))
|
single_entity_id_tracker.append((old_state, new_state))
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def multiple_run_callback(event: EventType[EventStateChangedData]):
|
def multiple_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
@ -738,14 +738,14 @@ async def test_async_track_state_removed_domain_match_all(hass: HomeAssistant) -
|
|||||||
match_all_entity_id_tracker = []
|
match_all_entity_id_tracker = []
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def single_run_callback(event: EventType[EventStateChangedData]):
|
def single_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
single_entity_id_tracker.append((old_state, new_state))
|
single_entity_id_tracker.append((old_state, new_state))
|
||||||
|
|
||||||
@ha.callback
|
@ha.callback
|
||||||
def match_all_run_callback(event: EventType[EventStateChangedData]):
|
def match_all_run_callback(event: EventType[EventStateChangedData]) -> None:
|
||||||
old_state = event.data["old_state"]
|
old_state = event.data["old_state"]
|
||||||
new_state = event.data["new_state"]
|
new_state = event.data["new_state"]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user