2017-12-13 09:17:12 +00:00
|
|
|
"""Provide configuration end points for Automations."""
|
2018-04-23 17:47:06 +00:00
|
|
|
from collections import OrderedDict
|
2018-04-30 13:56:42 +00:00
|
|
|
import uuid
|
2017-05-10 01:44:00 +00:00
|
|
|
|
2020-11-12 10:58:28 +00:00
|
|
|
from homeassistant.components.automation.config import (
|
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
async_validate_config_item,
|
|
|
|
)
|
2019-10-15 23:15:26 +00:00
|
|
|
from homeassistant.config import AUTOMATION_CONFIG_PATH
|
2019-12-08 16:57:28 +00:00
|
|
|
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
2020-01-27 07:01:35 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_registry
|
2017-05-10 01:44:00 +00:00
|
|
|
|
2020-01-27 07:01:35 +00:00
|
|
|
from . import ACTION_DELETE, EditIdBasedConfigView
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-05-10 01:44:00 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_setup(hass):
|
2017-05-10 01:44:00 +00:00
|
|
|
"""Set up the Automation config API."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-01-27 07:01:35 +00:00
|
|
|
async def hook(action, config_key):
|
2018-09-27 21:14:09 +00:00
|
|
|
"""post_write_hook for Config View that reloads automations."""
|
|
|
|
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
|
|
|
|
|
2020-01-27 07:01:35 +00:00
|
|
|
if action != ACTION_DELETE:
|
|
|
|
return
|
|
|
|
|
|
|
|
ent_reg = await entity_registry.async_get_registry(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)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.http.register_view(
|
|
|
|
EditAutomationConfigView(
|
|
|
|
DOMAIN,
|
|
|
|
"config",
|
2019-10-15 23:15:26 +00:00
|
|
|
AUTOMATION_CONFIG_PATH,
|
2019-07-31 19:25:30 +00:00
|
|
|
cv.string,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
post_write_hook=hook,
|
2019-09-27 15:48:48 +00:00
|
|
|
data_validator=async_validate_config_item,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2017-05-10 01:44:00 +00:00
|
|
|
return True
|
2018-04-23 17:47:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EditAutomationConfigView(EditIdBasedConfigView):
|
|
|
|
"""Edit automation config."""
|
|
|
|
|
|
|
|
def _write_value(self, hass, data, config_key, new_value):
|
|
|
|
"""Set value."""
|
|
|
|
index = None
|
|
|
|
for index, cur_value in enumerate(data):
|
2018-04-30 13:56:42 +00:00
|
|
|
# When people copy paste their automations to the config file,
|
|
|
|
# they sometimes forget to add IDs. Fix it here.
|
|
|
|
if CONF_ID not in cur_value:
|
|
|
|
cur_value[CONF_ID] = uuid.uuid4().hex
|
|
|
|
|
|
|
|
elif cur_value[CONF_ID] == config_key:
|
2018-04-23 17:47:06 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
cur_value = OrderedDict()
|
|
|
|
cur_value[CONF_ID] = config_key
|
|
|
|
index = len(data)
|
|
|
|
data.append(cur_value)
|
|
|
|
|
|
|
|
# Iterate through some keys that we want to have ordered in the output
|
|
|
|
updated_value = OrderedDict()
|
2019-09-17 19:12:54 +00:00
|
|
|
for key in ("id", "alias", "description", "trigger", "condition", "action"):
|
2018-04-23 17:47:06 +00:00
|
|
|
if key in cur_value:
|
|
|
|
updated_value[key] = cur_value[key]
|
|
|
|
if key in new_value:
|
|
|
|
updated_value[key] = new_value[key]
|
|
|
|
|
|
|
|
# We cover all current fields above, but just in case we start
|
|
|
|
# supporting more fields in the future.
|
|
|
|
updated_value.update(cur_value)
|
|
|
|
updated_value.update(new_value)
|
|
|
|
data[index] = updated_value
|