Home Assistant Unofficial Reference 2024.12.1
trigger.py
Go to the documentation of this file.
1 """LG Netcast TV trigger dispatcher."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from homeassistant.const import CONF_PLATFORM
8 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
10  TriggerActionType,
11  TriggerInfo,
12  TriggerProtocol,
13 )
14 from homeassistant.helpers.typing import ConfigType
15 
16 from .triggers import turn_on
17 
18 TRIGGERS = {
19  "turn_on": turn_on,
20 }
21 
22 
23 def _get_trigger_platform(config: ConfigType) -> TriggerProtocol:
24  """Return trigger platform."""
25  platform_split = config[CONF_PLATFORM].split(".", maxsplit=1)
26  if len(platform_split) < 2 or platform_split[1] not in TRIGGERS:
27  raise ValueError(
28  f"Unknown LG Netcast TV trigger platform {config[CONF_PLATFORM]}"
29  )
30  return cast(TriggerProtocol, TRIGGERS[platform_split[1]])
31 
32 
34  hass: HomeAssistant, config: ConfigType
35 ) -> ConfigType:
36  """Validate config."""
37  platform = _get_trigger_platform(config)
38  return cast(ConfigType, platform.TRIGGER_SCHEMA(config))
39 
40 
42  hass: HomeAssistant,
43  config: ConfigType,
44  action: TriggerActionType,
45  trigger_info: TriggerInfo,
46 ) -> CALLBACK_TYPE:
47  """Attach trigger of specified platform."""
48  platform = _get_trigger_platform(config)
49  return await platform.async_attach_trigger(hass, config, action, trigger_info)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
Definition: trigger.py:46
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
Definition: trigger.py:35
TriggerProtocol _get_trigger_platform(ConfigType config)
Definition: trigger.py:23