Improve zone typing (#78294)

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
epenet 2022-09-12 23:43:21 +02:00 committed by GitHub
parent dbc6dda41e
commit 7db1f8186c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Offer zone automation rules.""" """Offer zone automation rules."""
from __future__ import annotations
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -10,7 +12,14 @@ from homeassistant.const import (
CONF_PLATFORM, CONF_PLATFORM,
CONF_ZONE, 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 ( from homeassistant.helpers import (
condition, condition,
config_validation as cv, 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.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
# mypy: allow-incomplete-defs, allow-untyped-defs
# mypy: no-check-untyped-defs
EVENT_ENTER = "enter" EVENT_ENTER = "enter"
EVENT_LEAVE = "leave" EVENT_LEAVE = "leave"
DEFAULT_EVENT = EVENT_ENTER DEFAULT_EVENT = EVENT_ENTER
@ -67,21 +73,22 @@ async def async_attach_trigger(
"""Listen for state changes based on configuration.""" """Listen for state changes based on configuration."""
trigger_data = trigger_info["trigger_data"] trigger_data = trigger_info["trigger_data"]
entity_id: list[str] = config[CONF_ENTITY_ID] entity_id: list[str] = config[CONF_ENTITY_ID]
zone_entity_id = config.get(CONF_ZONE) zone_entity_id: str = config[CONF_ZONE]
event = config.get(CONF_EVENT) event: str = config[CONF_EVENT]
job = HassJob(action) job = HassJob(action)
@callback @callback
def zone_automation_listener(zone_event): def zone_automation_listener(zone_event: Event) -> None:
"""Listen for state changes and calls action.""" """Listen for state changes and calls action."""
entity = zone_event.data.get("entity_id") entity = zone_event.data.get("entity_id")
from_s = zone_event.data.get("old_state") from_s: State | None = zone_event.data.get("old_state")
to_s = zone_event.data.get("new_state") to_s: State | None = zone_event.data.get("new_state")
if ( if (
from_s from_s
and not location.has_location(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 return
@ -119,7 +126,7 @@ async def async_attach_trigger(
"description": description, "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) return async_track_state_change_event(hass, entity_id, zone_automation_listener)