1 """Offer geolocation automation rules."""
3 from __future__
import annotations
6 from typing
import Final
8 import voluptuous
as vol
14 EventStateChangedData,
28 _LOGGER = logging.getLogger(__name__)
30 EVENT_ENTER: Final =
"enter"
31 EVENT_LEAVE: Final =
"leave"
32 DEFAULT_EVENT: Final = EVENT_ENTER
34 TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
36 vol.Required(CONF_PLATFORM):
"geo_location",
37 vol.Required(CONF_SOURCE): cv.string,
39 vol.Required(CONF_EVENT, default=DEFAULT_EVENT): vol.Any(
40 EVENT_ENTER, EVENT_LEAVE
47 """Check if the state matches the provided source."""
48 return state
is not None and state.attributes.get(
"source") == source
54 action: TriggerActionType,
55 trigger_info: TriggerInfo,
57 """Listen for state changes based on configuration."""
58 trigger_data = trigger_info[
"trigger_data"]
59 source: str = config[CONF_SOURCE].lower()
60 zone_entity_id: str = config[CONF_ZONE]
61 trigger_event: str = config[CONF_EVENT]
65 def state_change_listener(event: Event[EventStateChangedData]) ->
None:
66 """Handle specific state changes."""
68 from_state = event.data[
"old_state"]
69 to_state = event.data[
"new_state"]
73 if (zone_state := hass.states.get(zone_entity_id))
is None:
75 "Unable to execute automation %s: Zone %s not found",
82 condition.zone(hass, zone_state, from_state)
if from_state
else False
84 to_match = condition.zone(hass, zone_state, to_state)
if to_state
else False
87 trigger_event == EVENT_ENTER
90 or trigger_event == EVENT_LEAVE
94 hass.async_run_hass_job(
99 "platform":
"geo_location",
101 "entity_id": event.data[
"entity_id"],
102 "from_state": from_state,
103 "to_state": to_state,
105 "event": trigger_event,
106 "description": f
"geo_location - {source}",
113 hass,
TrackStates(
False, set(), {DOMAIN}), state_change_listener
bool source_match(State|None state, str source)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
Callable[[Any], str] entity_domain(str|list[str] domain)
_TrackStateChangeFiltered async_track_state_change_filtered(HomeAssistant hass, TrackStates track_states, Callable[[Event[EventStateChangedData]], Any] action)