Home Assistant Unofficial Reference 2024.12.1
script.py
Go to the documentation of this file.
1 """Provide configuration end points for scripts."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
8 from homeassistant.components.script.config import async_validate_config_item
9 from homeassistant.config import SCRIPT_CONFIG_PATH
10 from homeassistant.const import SERVICE_RELOAD
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers import config_validation as cv, entity_registry as er
13 
14 from .const import ACTION_DELETE
15 from .view import EditKeyBasedConfigView
16 
17 
18 @callback
19 def async_setup(hass: HomeAssistant) -> bool:
20  """Set up the script config API."""
21 
22  async def hook(action: str, config_key: str) -> None:
23  """post_write_hook for Config View that reloads scripts."""
24  if action != ACTION_DELETE:
25  await hass.services.async_call(SCRIPT_DOMAIN, SERVICE_RELOAD)
26  return
27 
28  ent_reg = er.async_get(hass)
29 
30  entity_id = ent_reg.async_get_entity_id(
31  SCRIPT_DOMAIN, SCRIPT_DOMAIN, config_key
32  )
33 
34  if entity_id is None:
35  return
36 
37  ent_reg.async_remove(entity_id)
38 
39  hass.http.register_view(
41  SCRIPT_DOMAIN,
42  "config",
43  SCRIPT_CONFIG_PATH,
44  cv.slug,
45  post_write_hook=hook,
46  data_validator=async_validate_config_item,
47  )
48  )
49  return True
50 
51 
53  """Edit script config."""
54 
56  self,
57  hass: HomeAssistant,
58  data: dict[str, dict[str, Any]],
59  config_key: str,
60  new_value: dict[str, Any],
61  ) -> None:
62  """Set value."""
63  data[config_key] = new_value
None _write_value(self, HomeAssistant hass, dict[str, dict[str, Any]] data, str config_key, dict[str, Any] new_value)
Definition: script.py:61
bool async_setup(HomeAssistant hass)
Definition: script.py:19