mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Improve zone typing (#78294)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
parent
dbc6dda41e
commit
7db1f8186c
@ -1,4 +1,6 @@
|
||||
"""Offer zone automation rules."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
@ -10,7 +12,14 @@ from homeassistant.const import (
|
||||
CONF_PLATFORM,
|
||||
CONF_ZONE,
|
||||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||
from homeassistant.core import (
|
||||
CALLBACK_TYPE,
|
||||
Event,
|
||||
HassJob,
|
||||
HomeAssistant,
|
||||
State,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.helpers import (
|
||||
condition,
|
||||
config_validation as cv,
|
||||
@ -21,9 +30,6 @@ from homeassistant.helpers.event import async_track_state_change_event
|
||||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
# mypy: allow-incomplete-defs, allow-untyped-defs
|
||||
# mypy: no-check-untyped-defs
|
||||
|
||||
EVENT_ENTER = "enter"
|
||||
EVENT_LEAVE = "leave"
|
||||
DEFAULT_EVENT = EVENT_ENTER
|
||||
@ -67,21 +73,22 @@ async def async_attach_trigger(
|
||||
"""Listen for state changes based on configuration."""
|
||||
trigger_data = trigger_info["trigger_data"]
|
||||
entity_id: list[str] = config[CONF_ENTITY_ID]
|
||||
zone_entity_id = config.get(CONF_ZONE)
|
||||
event = config.get(CONF_EVENT)
|
||||
zone_entity_id: str = config[CONF_ZONE]
|
||||
event: str = config[CONF_EVENT]
|
||||
job = HassJob(action)
|
||||
|
||||
@callback
|
||||
def zone_automation_listener(zone_event):
|
||||
def zone_automation_listener(zone_event: Event) -> None:
|
||||
"""Listen for state changes and calls action."""
|
||||
entity = zone_event.data.get("entity_id")
|
||||
from_s = zone_event.data.get("old_state")
|
||||
to_s = zone_event.data.get("new_state")
|
||||
from_s: State | None = zone_event.data.get("old_state")
|
||||
to_s: State | None = zone_event.data.get("new_state")
|
||||
|
||||
if (
|
||||
from_s
|
||||
and not location.has_location(from_s)
|
||||
or not location.has_location(to_s)
|
||||
or to_s
|
||||
and not location.has_location(to_s)
|
||||
):
|
||||
return
|
||||
|
||||
@ -119,7 +126,7 @@ async def async_attach_trigger(
|
||||
"description": description,
|
||||
}
|
||||
},
|
||||
to_s.context,
|
||||
to_s.context if to_s else None,
|
||||
)
|
||||
|
||||
return async_track_state_change_event(hass, entity_id, zone_automation_listener)
|
||||
|
Loading…
x
Reference in New Issue
Block a user