Ensure HA script and Python script services have a name (#47204)
Co-authored-by: Franck Nijhof <git@frenck.dev>pull/48545/head
parent
bee55a0494
commit
6fe04f40a2
homeassistant/components
python_script
script
tests/components
python_script
script
|
@ -19,7 +19,7 @@ from RestrictedPython.Guards import (
|
||||||
)
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import SERVICE_RELOAD
|
from homeassistant.const import CONF_DESCRIPTION, CONF_NAME, SERVICE_RELOAD
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.service import async_set_service_schema
|
from homeassistant.helpers.service import async_set_service_schema
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
|
@ -71,6 +71,8 @@ ALLOWED_DT_UTIL = {
|
||||||
"get_age",
|
"get_age",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CONF_FIELDS = "fields"
|
||||||
|
|
||||||
|
|
||||||
class ScriptError(HomeAssistantError):
|
class ScriptError(HomeAssistantError):
|
||||||
"""When a script error occurs."""
|
"""When a script error occurs."""
|
||||||
|
@ -125,8 +127,9 @@ def discover_scripts(hass):
|
||||||
hass.services.register(DOMAIN, name, python_script_service_handler)
|
hass.services.register(DOMAIN, name, python_script_service_handler)
|
||||||
|
|
||||||
service_desc = {
|
service_desc = {
|
||||||
"description": services_dict.get(name, {}).get("description", ""),
|
CONF_NAME: services_dict.get(name, {}).get("name", name),
|
||||||
"fields": services_dict.get(name, {}).get("fields", {}),
|
CONF_DESCRIPTION: services_dict.get(name, {}).get("description", ""),
|
||||||
|
CONF_FIELDS: services_dict.get(name, {}).get("fields", {}),
|
||||||
}
|
}
|
||||||
async_set_service_schema(hass, DOMAIN, name, service_desc)
|
async_set_service_schema(hass, DOMAIN, name, service_desc)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# Describes the format for available python_script services
|
# Describes the format for available python_script services
|
||||||
|
|
||||||
reload:
|
reload:
|
||||||
|
name: Reload
|
||||||
description: Reload all available python_scripts
|
description: Reload all available python_scripts
|
||||||
|
|
|
@ -12,6 +12,7 @@ from homeassistant.const import (
|
||||||
ATTR_NAME,
|
ATTR_NAME,
|
||||||
CONF_ALIAS,
|
CONF_ALIAS,
|
||||||
CONF_DEFAULT,
|
CONF_DEFAULT,
|
||||||
|
CONF_DESCRIPTION,
|
||||||
CONF_ICON,
|
CONF_ICON,
|
||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
@ -54,7 +55,6 @@ ATTR_LAST_TRIGGERED = "last_triggered"
|
||||||
ATTR_VARIABLES = "variables"
|
ATTR_VARIABLES = "variables"
|
||||||
|
|
||||||
CONF_ADVANCED = "advanced"
|
CONF_ADVANCED = "advanced"
|
||||||
CONF_DESCRIPTION = "description"
|
|
||||||
CONF_EXAMPLE = "example"
|
CONF_EXAMPLE = "example"
|
||||||
CONF_FIELDS = "fields"
|
CONF_FIELDS = "fields"
|
||||||
CONF_REQUIRED = "required"
|
CONF_REQUIRED = "required"
|
||||||
|
@ -287,6 +287,7 @@ async def _async_process_config(hass, config, component):
|
||||||
|
|
||||||
# Register the service description
|
# Register the service description
|
||||||
service_desc = {
|
service_desc = {
|
||||||
|
CONF_NAME: script_entity.name,
|
||||||
CONF_DESCRIPTION: cfg[CONF_DESCRIPTION],
|
CONF_DESCRIPTION: cfg[CONF_DESCRIPTION],
|
||||||
CONF_FIELDS: cfg[CONF_FIELDS],
|
CONF_FIELDS: cfg[CONF_FIELDS],
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
# Describes the format for available python_script services
|
# Describes the format for available python_script services
|
||||||
|
|
||||||
reload:
|
reload:
|
||||||
|
name: Reload
|
||||||
description: Reload all the available scripts
|
description: Reload all the available scripts
|
||||||
|
|
||||||
turn_on:
|
turn_on:
|
||||||
|
name: Turn on
|
||||||
description: Turn on script
|
description: Turn on script
|
||||||
target:
|
target:
|
||||||
|
|
||||||
turn_off:
|
turn_off:
|
||||||
|
name: Turn off
|
||||||
description: Turn off script
|
description: Turn off script
|
||||||
target:
|
target:
|
||||||
|
|
||||||
toggle:
|
toggle:
|
||||||
|
name: Toggle
|
||||||
description: Toggle script
|
description: Toggle script
|
||||||
target:
|
target:
|
||||||
|
|
|
@ -306,6 +306,7 @@ async def test_service_descriptions(hass):
|
||||||
|
|
||||||
service_descriptions1 = (
|
service_descriptions1 = (
|
||||||
"hello:\n"
|
"hello:\n"
|
||||||
|
" name: ABC\n"
|
||||||
" description: Description of hello.py.\n"
|
" description: Description of hello.py.\n"
|
||||||
" fields:\n"
|
" fields:\n"
|
||||||
" fake_param:\n"
|
" fake_param:\n"
|
||||||
|
@ -333,6 +334,7 @@ async def test_service_descriptions(hass):
|
||||||
|
|
||||||
assert len(descriptions) == 1
|
assert len(descriptions) == 1
|
||||||
|
|
||||||
|
assert descriptions[DOMAIN]["hello"]["name"] == "ABC"
|
||||||
assert descriptions[DOMAIN]["hello"]["description"] == "Description of hello.py."
|
assert descriptions[DOMAIN]["hello"]["description"] == "Description of hello.py."
|
||||||
assert (
|
assert (
|
||||||
descriptions[DOMAIN]["hello"]["fields"]["fake_param"]["description"]
|
descriptions[DOMAIN]["hello"]["fields"]["fake_param"]["description"]
|
||||||
|
@ -343,6 +345,8 @@ async def test_service_descriptions(hass):
|
||||||
== "This is a test of python_script.hello"
|
== "This is a test of python_script.hello"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Verify default name = file name
|
||||||
|
assert descriptions[DOMAIN]["world_beer"]["name"] == "world_beer"
|
||||||
assert descriptions[DOMAIN]["world_beer"]["description"] == ""
|
assert descriptions[DOMAIN]["world_beer"]["description"] == ""
|
||||||
assert bool(descriptions[DOMAIN]["world_beer"]["fields"]) is False
|
assert bool(descriptions[DOMAIN]["world_beer"]["fields"]) is False
|
||||||
|
|
||||||
|
|
|
@ -269,6 +269,7 @@ async def test_service_descriptions(hass):
|
||||||
|
|
||||||
descriptions = await async_get_all_descriptions(hass)
|
descriptions = await async_get_all_descriptions(hass)
|
||||||
|
|
||||||
|
assert descriptions[DOMAIN]["test"]["name"] == "test"
|
||||||
assert descriptions[DOMAIN]["test"]["description"] == "test description"
|
assert descriptions[DOMAIN]["test"]["description"] == "test description"
|
||||||
assert not descriptions[DOMAIN]["test"]["fields"]
|
assert not descriptions[DOMAIN]["test"]["fields"]
|
||||||
|
|
||||||
|
@ -303,6 +304,27 @@ async def test_service_descriptions(hass):
|
||||||
== "test_param example"
|
== "test_param example"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test 3: has "alias" that will be used as "name"
|
||||||
|
with patch(
|
||||||
|
"homeassistant.config.load_yaml_config_file",
|
||||||
|
return_value={
|
||||||
|
"script": {
|
||||||
|
"test_name": {
|
||||||
|
"alias": "ABC",
|
||||||
|
"sequence": [{"delay": {"seconds": 5}}],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
):
|
||||||
|
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
|
||||||
|
|
||||||
|
descriptions = await async_get_all_descriptions(hass)
|
||||||
|
|
||||||
|
assert descriptions[DOMAIN]["test_name"]["name"] == "ABC"
|
||||||
|
|
||||||
|
# Test 4: verify that names from YAML are taken into account as well
|
||||||
|
assert descriptions[DOMAIN]["turn_on"]["name"] == "Turn on"
|
||||||
|
|
||||||
|
|
||||||
async def test_shared_context(hass):
|
async def test_shared_context(hass):
|
||||||
"""Test that the shared context is passed down the chain."""
|
"""Test that the shared context is passed down the chain."""
|
||||||
|
|
Loading…
Reference in New Issue