Home Assistant Unofficial Reference 2024.12.1
device_automation.py
Go to the documentation of this file.
1 """Provides device automations for MQTT."""
2 
3 from __future__ import annotations
4 
5 import functools
6 
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import device_trigger
14 from .config import MQTT_BASE_SCHEMA
15 from .entity import async_setup_non_entity_entry_helper
16 
17 AUTOMATION_TYPE_TRIGGER = "trigger"
18 AUTOMATION_TYPES = [AUTOMATION_TYPE_TRIGGER]
19 AUTOMATION_TYPES_SCHEMA = vol.In(AUTOMATION_TYPES)
20 CONF_AUTOMATION_TYPE = "automation_type"
21 
22 DISCOVERY_SCHEMA = MQTT_BASE_SCHEMA.extend(
23  {vol.Required(CONF_AUTOMATION_TYPE): AUTOMATION_TYPES_SCHEMA},
24  extra=vol.ALLOW_EXTRA,
25 )
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
29  """Set up MQTT device automation dynamically through MQTT discovery."""
30 
31  setup = functools.partial(_async_setup_automation, hass, config_entry=config_entry)
33  hass, "device_automation", setup, DISCOVERY_SCHEMA
34  )
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  config_entry: ConfigEntry,
41  discovery_data: DiscoveryInfoType,
42 ) -> None:
43  """Set up an MQTT device automation."""
44  if config[CONF_AUTOMATION_TYPE] == AUTOMATION_TYPE_TRIGGER:
45  await device_trigger.async_setup_trigger(
46  hass, config, config_entry, discovery_data
47  )
48 
49 
50 async def async_removed_from_device(hass: HomeAssistant, device_id: str) -> None:
51  """Handle Mqtt removed from a device."""
52  await device_trigger.async_removed_from_device(hass, device_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
None _async_setup_automation(HomeAssistant hass, ConfigType config, ConfigEntry config_entry, DiscoveryInfoType discovery_data)
None async_removed_from_device(HomeAssistant hass, str device_id)
None async_setup_non_entity_entry_helper(HomeAssistant hass, str domain, _SetupNonEntityHelperCallbackProtocol async_setup, vol.Schema discovery_schema)
Definition: entity.py:204