Files
homeassistant-core/homeassistant/components/config/script.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
1.7 KiB
Python
Raw Normal View History

2017-08-15 22:09:10 -07:00
"""Provide configuration end points for scripts."""
2024-01-18 09:20:19 +01:00
from __future__ import annotations
from typing import Any
from homeassistant.components.script import DOMAIN
from homeassistant.components.script.config import (
SCRIPT_ENTITY_SCHEMA,
async_validate_config_item,
)
2019-10-15 16:15:26 -07:00
from homeassistant.config import SCRIPT_CONFIG_PATH
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, entity_registry as er
2017-08-15 22:09:10 -07:00
from .const import ACTION_DELETE
from .view import EditKeyBasedConfigView
2017-08-15 22:09:10 -07:00
@callback
def async_setup(hass: HomeAssistant) -> bool:
2017-08-15 22:09:10 -07:00
"""Set up the script config API."""
2019-07-31 12:25:30 -07:00
2024-01-18 09:20:19 +01:00
async def hook(action: str, config_key: str) -> None:
2018-09-27 23:13:11 +02:00
"""post_write_hook for Config View that reloads scripts."""
if action != ACTION_DELETE:
2022-11-10 17:27:14 +01:00
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
return
ent_reg = er.async_get(hass)
entity_id = ent_reg.async_get_entity_id(DOMAIN, DOMAIN, config_key)
if entity_id is None:
return
ent_reg.async_remove(entity_id)
2017-08-15 22:09:10 -07:00
hass.http.register_view(
EditScriptConfigView(
2020-08-21 04:38:25 -05:00
DOMAIN,
2017-08-15 22:09:10 -07:00
"config",
2019-10-15 16:15:26 -07:00
SCRIPT_CONFIG_PATH,
2017-08-15 22:09:10 -07:00
cv.slug,
SCRIPT_ENTITY_SCHEMA,
2018-09-27 23:13:11 +02:00
post_write_hook=hook,
2020-08-21 04:38:25 -05:00
data_validator=async_validate_config_item,
2017-08-15 22:09:10 -07:00
)
2019-07-31 12:25:30 -07:00
)
2017-08-15 22:09:10 -07:00
return True
class EditScriptConfigView(EditKeyBasedConfigView):
"""Edit script config."""
2024-01-18 09:20:19 +01:00
def _write_value(
self,
hass: HomeAssistant,
data: dict[str, dict[str, Any]],
config_key: str,
new_value: dict[str, Any],
) -> None:
"""Set value."""
data[config_key] = new_value