Add scripts editor backend (#8993)

* Add scripts editor backend

* Fix docstrings
pull/8903/merge
Paulus Schoutsen 2017-08-15 22:09:10 -07:00 committed by GitHub
parent 95c57412ff
commit 2dab239021
4 changed files with 45 additions and 10 deletions

View File

@ -14,7 +14,7 @@ from homeassistant.util.yaml import load_yaml, dump
DOMAIN = 'config'
DEPENDENCIES = ['http']
SECTIONS = ('core', 'group', 'hassbian', 'automation')
SECTIONS = ('core', 'group', 'hassbian', 'automation', 'script')
ON_DEMAND = ('zwave')

View File

@ -0,0 +1,19 @@
"""Provide configuration end points for scripts."""
import asyncio
from homeassistant.components.config import EditKeyBasedConfigView
from homeassistant.components.script import SCRIPT_ENTRY_SCHEMA, async_reload
import homeassistant.helpers.config_validation as cv
CONFIG_PATH = 'scripts.yaml'
@asyncio.coroutine
def async_setup(hass):
"""Set up the script config API."""
hass.http.register_view(EditKeyBasedConfigView(
'script', 'config', CONFIG_PATH, cv.slug, SCRIPT_ENTRY_SCHEMA,
post_write_hook=async_reload
))
return True

View File

@ -39,13 +39,13 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}'
GROUP_NAME_ALL_SCRIPTS = 'all scripts'
_SCRIPT_ENTRY_SCHEMA = vol.Schema({
SCRIPT_ENTRY_SCHEMA = vol.Schema({
CONF_ALIAS: cv.string,
vol.Required(CONF_SEQUENCE): cv.SCRIPT_SCHEMA,
})
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({cv.slug: _SCRIPT_ENTRY_SCHEMA})
DOMAIN: vol.Schema({cv.slug: SCRIPT_ENTRY_SCHEMA})
}, extra=vol.ALLOW_EXTRA)
SCRIPT_SERVICE_SCHEMA = vol.Schema(dict)
@ -62,12 +62,6 @@ def is_on(hass, entity_id):
return hass.states.is_state(entity_id, STATE_ON)
@bind_hass
def reload(hass):
"""Reload script component."""
hass.services.call(DOMAIN, SERVICE_RELOAD)
@bind_hass
def turn_on(hass, entity_id, variables=None):
"""Turn script on."""
@ -88,6 +82,21 @@ def toggle(hass, entity_id):
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def reload(hass):
"""Reload script component."""
hass.services.call(DOMAIN, SERVICE_RELOAD)
@bind_hass
def async_reload(hass):
"""Reload the scripts from config.
Returns a coroutine object.
"""
return hass.services.async_call(DOMAIN, SERVICE_RELOAD)
@asyncio.coroutine
def async_setup(hass, config):
"""Load the scripts from the configuration."""

View File

@ -108,6 +108,7 @@ tts:
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
"""
@ -173,11 +174,14 @@ def create_default_config(config_dir, detect_location=True):
CONFIG_PATH as GROUP_CONFIG_PATH)
from homeassistant.components.config.automation import (
CONFIG_PATH as AUTOMATION_CONFIG_PATH)
from homeassistant.components.config.script import (
CONFIG_PATH as SCRIPT_CONFIG_PATH)
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
version_path = os.path.join(config_dir, VERSION_FILE)
group_yaml_path = os.path.join(config_dir, GROUP_CONFIG_PATH)
automation_yaml_path = os.path.join(config_dir, AUTOMATION_CONFIG_PATH)
script_yaml_path = os.path.join(config_dir, SCRIPT_CONFIG_PATH)
info = {attr: default for attr, default, _, _ in DEFAULT_CORE_CONFIG}
@ -216,12 +220,15 @@ def create_default_config(config_dir, detect_location=True):
with open(version_path, 'wt') as version_file:
version_file.write(__version__)
with open(group_yaml_path, 'w'):
with open(group_yaml_path, 'wt'):
pass
with open(automation_yaml_path, 'wt') as fil:
fil.write('[]')
with open(script_yaml_path, 'wt'):
pass
return config_path
except IOError: