feat: replace script args with typed named parameters
- Change execute_script API from positional args list to named params dict - Update service schema, API client, and constants - Add execute_script service documentation to README
This commit is contained in:
@@ -15,8 +15,8 @@ from homeassistant.helpers import config_validation as cv
|
||||
from .api_client import MediaServerClient, MediaServerError
|
||||
from .const import (
|
||||
ATTR_FILE_PATH,
|
||||
ATTR_SCRIPT_ARGS,
|
||||
ATTR_SCRIPT_NAME,
|
||||
ATTR_SCRIPT_PARAMS,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_TOKEN,
|
||||
@@ -33,9 +33,7 @@ PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.BUTTON, Platform.NU
|
||||
SERVICE_EXECUTE_SCRIPT_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_SCRIPT_NAME): cv.string,
|
||||
vol.Optional(ATTR_SCRIPT_ARGS, default=[]): vol.All(
|
||||
cv.ensure_list, [cv.string]
|
||||
),
|
||||
vol.Optional(ATTR_SCRIPT_PARAMS, default={}): dict,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -83,10 +81,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_execute_script(call: ServiceCall) -> dict[str, Any]:
|
||||
"""Execute a script on the media server."""
|
||||
script_name = call.data[ATTR_SCRIPT_NAME]
|
||||
script_args = call.data.get(ATTR_SCRIPT_ARGS, [])
|
||||
script_params = call.data.get(ATTR_SCRIPT_PARAMS, {})
|
||||
|
||||
_LOGGER.debug(
|
||||
"Executing script '%s' with args: %s", script_name, script_args
|
||||
"Executing script '%s' with params: %s", script_name, script_params
|
||||
)
|
||||
|
||||
# Get all clients and execute on all of them
|
||||
@@ -94,7 +92,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
for entry_id, data in hass.data[DOMAIN].items():
|
||||
client: MediaServerClient = data["client"]
|
||||
try:
|
||||
result = await client.execute_script(script_name, script_args)
|
||||
result = await client.execute_script(script_name, script_params)
|
||||
results[entry_id] = result
|
||||
_LOGGER.info(
|
||||
"Script '%s' executed on %s: success=%s",
|
||||
|
||||
Reference in New Issue
Block a user