Load user-provided descriptions for python_scripts (#26069)

* Load user-provided descriptions for python_scripts

* Import SERVICE_DESCRIPTION_CACHE

* Use async_set_service_schema to register service descriptions

* Add python_script tests for loading service descriptions

* Use async/await in test
This commit is contained in:
Jeff Irion 2019-08-22 11:01:56 -07:00 committed by Paulus Schoutsen
parent 2d432da14c
commit aff151c90a
2 changed files with 114 additions and 1 deletions

View file

@ -9,8 +9,10 @@ import voluptuous as vol
from homeassistant.const import SERVICE_RELOAD
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.service import async_set_service_schema
from homeassistant.loader import bind_hass
from homeassistant.util import sanitize_filename
from homeassistant.util.yaml.loader import load_yaml
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
@ -90,10 +92,23 @@ def discover_scripts(hass):
continue
hass.services.remove(DOMAIN, existing_service)
# Load user-provided service descriptions from python_scripts/services.yaml
services_yaml = os.path.join(path, "services.yaml")
if os.path.exists(services_yaml):
services_dict = load_yaml(services_yaml)
else:
services_dict = {}
for fil in glob.iglob(os.path.join(path, "*.py")):
name = os.path.splitext(os.path.basename(fil))[0]
hass.services.register(DOMAIN, name, python_script_service_handler)
service_desc = {
"description": services_dict.get(name, {}).get("description", ""),
"fields": services_dict.get(name, {}).get("fields", {}),
}
async_set_service_schema(hass, DOMAIN, name, service_desc)
@bind_hass
def execute_script(hass, name, data=None):