from typing import Any
import voluptuous as vol
from homeassistant.const import CONF_OPTIONS
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.trigger import Trigger, TriggerActionRunner, TriggerConfig
from homeassistant.helpers.typing import ConfigType
_OPTIONS_SCHEMA = vol.Schema({
vol.Required("event_type"): cv.string,
})
_CONFIG_SCHEMA = vol.Schema({
vol.Required(CONF_OPTIONS): _OPTIONS_SCHEMA,
})
class EventTrigger(Trigger):
"""Trigger on events."""
_options: dict[str, Any]
@classmethod
async def async_validate_config(
cls, hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate trigger-specific config."""
return _CONFIG_SCHEMA(config)
def __init__(self, hass: HomeAssistant, config: TriggerConfig) -> None:
"""Initialize trigger."""
super().__init__(hass, config)
assert config.options is not None
self._options = config.options
async def async_attach_runner(
self, run_action: TriggerActionRunner
) -> CALLBACK_TYPE:
"""Attach the trigger."""
@callback
def async_remove() -> None:
"""Remove trigger."""
# Your code to unregister the trigger
@callback
def async_on_event(event_data: dict) -> None:
"""Handle event."""
payload = {
"event_type": event_data["type"],
"data": event_data["data"],
}
description = f"Event {event_data['type']} detected"
run_action(payload, description)
# Dummy example method to register your event listener
register_for_events(async_on_event)
return async_remove