1 """Support for tag triggers."""
3 from __future__
import annotations
5 import voluptuous
as vol
13 from .const
import DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, TAG_ID
15 TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
17 vol.Required(CONF_PLATFORM): DOMAIN,
18 vol.Required(TAG_ID): vol.All(cv.ensure_list, [cv.string]),
19 vol.Optional(DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
27 action: TriggerActionType,
28 trigger_info: TriggerInfo,
30 """Listen for tag_scanned events based on configuration."""
31 trigger_data = trigger_info[
"trigger_data"]
32 tag_ids: set[str] = set(config[TAG_ID])
33 device_ids: set[str] |
None = (
34 set(config[DEVICE_ID])
if DEVICE_ID
in config
else None
39 async
def handle_event(event: Event) ->
None:
40 """Listen for tag scan events and calls the action when data matches."""
41 if event.data.get(TAG_ID)
not in tag_ids
or (
42 device_ids
is not None and event.data.get(DEVICE_ID)
not in device_ids
46 task = hass.async_run_hass_job(
53 "description":
"Tag scanned",
62 return hass.bus.async_listen(EVENT_TAG_SCANNED, handle_event)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)