Home Assistant Unofficial Reference 2024.12.1
trigger.py
Go to the documentation of this file.
1 """Samsung 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(f"Unknown Samsung TV trigger platform {config[CONF_PLATFORM]}")
28  return cast(TriggerProtocol, TRIGGERS[platform_split[1]])
29 
30 
32  hass: HomeAssistant, config: ConfigType
33 ) -> ConfigType:
34  """Validate config."""
35  platform = _get_trigger_platform(config)
36  return cast(ConfigType, platform.TRIGGER_SCHEMA(config))
37 
38 
40  hass: HomeAssistant,
41  config: ConfigType,
42  action: TriggerActionType,
43  trigger_info: TriggerInfo,
44 ) -> CALLBACK_TYPE:
45  """Attach trigger of specified platform."""
46  platform = _get_trigger_platform(config)
47  return await platform.async_attach_trigger(hass, config, action, trigger_info)
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
Definition: trigger.py:33
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
Definition: trigger.py:44
TriggerProtocol _get_trigger_platform(ConfigType config)
Definition: trigger.py:23