I set up a script to slowly dim a light, where the light entity_id is passed as a field.
The script works fine when invoking the script directly, and when using script.turn_on.
However, I’d like to have a button in my dashboard to trigger the fade script for a given light, but also to stop it. script.toggle seems like a good fit for what I want to do, but doesn’t currently support passing variables to the script.
Compare the code behind script.turn_on:
async def turn_on_service(service: ServiceCall) -> None:
"""Call a service to turn script on."""
variables = service.data.get(ATTR_VARIABLES)
script_entities = await component.async_extract_from_service(service)
for script_entity in script_entities:
await script_entity.async_turn_on(
variables=variables, context=service.context, wait=False
)
to that of script.toggle:
async def toggle_service(service: ServiceCall) -> None:
"""Toggle a script."""
script_entities = await component.async_extract_from_service(service)
for script_entity in script_entities:
await script_entity.async_toggle(context=service.context, wait=False)
I think adding this feature would be as simple as this:
diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py
index dd293726484..8eb75373fd9 100644
--- a/homeassistant/components/script/__init__.py
+++ b/homeassistant/components/script/__init__.py
@@ -264,9 +264,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def toggle_service(service: ServiceCall) -> None:
"""Toggle a script."""
+ variables = service.data.get(ATTR_VARIABLES)
script_entities = await component.async_extract_from_service(service)
for script_entity in script_entities:
- await script_entity.async_toggle(context=service.context, wait=False)
+ await script_entity.async_toggle(
+ variables=variables, context=service.context, wait=False)
hass.services.async_register(
DOMAIN, SERVICE_RELOAD, reload_service, schema=RELOAD_SERVICE_SCHEMA
Does this sound reasonable? Is there a reason that script.toggle should not pass variables when turning a script on?
If I am able to find some free time later, I’ll try to make this change and test it locally, and submit a PR if I’m satisfied.