Allow fetching script config (#79131)

This commit is contained in:
Paulus Schoutsen 2022-09-28 07:52:58 -04:00 committed by GitHub
parent 653e0917bb
commit d3f5ccfed8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 2 deletions

View file

@ -8,6 +8,7 @@ from typing import Any, cast
import voluptuous as vol
from voluptuous.humanize import humanize_error
from homeassistant.components import websocket_api
from homeassistant.components.blueprint import CONF_USE_BLUEPRINT, BlueprintInputs
from homeassistant.const import (
ATTR_ENTITY_ID,
@ -222,6 +223,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.services.async_register(
DOMAIN, SERVICE_TOGGLE, toggle_service, schema=SCRIPT_TURN_ONOFF_SCHEMA
)
websocket_api.async_register_command(hass, websocket_config)
return True
@ -325,7 +327,7 @@ class ScriptEntity(ToggleEntity, RestoreEntity):
variables=cfg.get(CONF_VARIABLES),
)
self._changed = asyncio.Event()
self._raw_config = raw_config
self.raw_config = raw_config
self._trace_config = cfg[CONF_TRACE]
self._blueprint_inputs = blueprint_inputs
@ -406,7 +408,7 @@ class ScriptEntity(ToggleEntity, RestoreEntity):
with trace_script(
self.hass,
self.unique_id,
self._raw_config,
self.raw_config,
self._blueprint_inputs,
context,
self._trace_config,
@ -439,3 +441,28 @@ class ScriptEntity(ToggleEntity, RestoreEntity):
# remove service
self.hass.services.async_remove(DOMAIN, self.unique_id)
@websocket_api.websocket_command({"type": "script/config", "entity_id": str})
def websocket_config(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Get script config."""
component: EntityComponent[ScriptEntity] = hass.data[DOMAIN]
script = component.get_entity(msg["entity_id"])
if script is None:
connection.send_error(
msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found"
)
return
connection.send_result(
msg["id"],
{
"config": script.raw_config,
},
)

View file

@ -1025,6 +1025,47 @@ async def test_setup_with_duplicate_scripts(
assert len(hass.states.async_entity_ids("script")) == 1
async def test_websocket_config(hass, hass_ws_client):
"""Test config command."""
config = {
"alias": "hello",
"sequence": [{"service": "light.turn_on"}],
}
assert await async_setup_component(
hass,
"script",
{
"script": {
"hello": config,
},
},
)
client = await hass_ws_client(hass)
await client.send_json(
{
"id": 5,
"type": "script/config",
"entity_id": "script.hello",
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] == {"config": config}
await client.send_json(
{
"id": 6,
"type": "script/config",
"entity_id": "script.not_exist",
}
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == "not_found"
async def test_script_service_changed_entity_id(hass: HomeAssistant) -> None:
"""Test the script service works for scripts with overridden entity_id."""
entity_reg = er.async_get(hass)