Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Alexa skill service end point."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.const import (
10  CONF_CLIENT_ID,
11  CONF_CLIENT_SECRET,
12  CONF_DESCRIPTION,
13  CONF_NAME,
14  CONF_PASSWORD,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers import config_validation as cv, entityfilter
18 from homeassistant.helpers.typing import ConfigType
19 
20 from . import flash_briefings, intent, smart_home
21 from .const import (
22  CONF_AUDIO,
23  CONF_DISPLAY_CATEGORIES,
24  CONF_DISPLAY_URL,
25  CONF_ENDPOINT,
26  CONF_ENTITY_CONFIG,
27  CONF_FILTER,
28  CONF_LOCALE,
29  CONF_SUPPORTED_LOCALES,
30  CONF_TEXT,
31  CONF_TITLE,
32  CONF_UID,
33  DOMAIN,
34 )
35 
36 CONF_FLASH_BRIEFINGS = "flash_briefings"
37 CONF_SMART_HOME = "smart_home"
38 DEFAULT_LOCALE = "en-US"
39 
40 # Alexa Smart Home API send events gateway endpoints
41 # https://developer.amazon.com/en-US/docs/alexa/smarthome/send-events.html#endpoints
42 VALID_ENDPOINTS = [
43  "https://api.amazonalexa.com/v3/events",
44  "https://api.eu.amazonalexa.com/v3/events",
45  "https://api.fe.amazonalexa.com/v3/events",
46 ]
47 
48 
49 ALEXA_ENTITY_SCHEMA = vol.Schema(
50  {
51  vol.Optional(CONF_DESCRIPTION): cv.string,
52  vol.Optional(CONF_DISPLAY_CATEGORIES): cv.string,
53  vol.Optional(CONF_NAME): cv.string,
54  }
55 )
56 
57 SMART_HOME_SCHEMA = vol.Schema(
58  {
59  vol.Optional(CONF_ENDPOINT): vol.All(vol.Lower, vol.In(VALID_ENDPOINTS)),
60  vol.Optional(CONF_CLIENT_ID): cv.string,
61  vol.Optional(CONF_CLIENT_SECRET): cv.string,
62  vol.Optional(CONF_LOCALE, default=DEFAULT_LOCALE): vol.In(
63  CONF_SUPPORTED_LOCALES
64  ),
65  vol.Optional(CONF_FILTER, default={}): entityfilter.FILTER_SCHEMA,
66  vol.Optional(CONF_ENTITY_CONFIG): {cv.entity_id: ALEXA_ENTITY_SCHEMA},
67  }
68 )
69 
70 CONFIG_SCHEMA = vol.Schema(
71  {
72  DOMAIN: {
73  CONF_FLASH_BRIEFINGS: {
74  vol.Required(CONF_PASSWORD): cv.string,
75  cv.string: vol.All(
76  cv.ensure_list,
77  [
78  {
79  vol.Optional(CONF_UID): cv.string,
80  vol.Required(CONF_TITLE): cv.template,
81  vol.Optional(CONF_AUDIO): cv.template,
82  vol.Required(CONF_TEXT, default=""): cv.template,
83  vol.Optional(CONF_DISPLAY_URL): cv.template,
84  }
85  ],
86  ),
87  },
88  # vol.Optional here would mean we couldn't distinguish between an empty
89  # smart_home: and none at all.
90  CONF_SMART_HOME: vol.Any(SMART_HOME_SCHEMA, None),
91  }
92  },
93  extra=vol.ALLOW_EXTRA,
94 )
95 
96 
97 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
98  """Activate the Alexa component."""
99  if DOMAIN not in config:
100  return True
101 
102  config = config[DOMAIN]
103 
104  intent.async_setup(hass)
105 
106  if flash_briefings_config := config.get(CONF_FLASH_BRIEFINGS):
107  flash_briefings.async_setup(hass, flash_briefings_config)
108 
109  # smart_home being absent is not the same as smart_home being None
110  if CONF_SMART_HOME in config:
111  smart_home_config: dict[str, Any] | None = config[CONF_SMART_HOME]
112  smart_home_config = smart_home_config or SMART_HOME_SCHEMA({})
113  await smart_home.async_setup(hass, smart_home_config)
114 
115  return True
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:149