From d3f5ccfed8d029bf7a346d2b1a5192f2f873116c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen <balloob@gmail.com> Date: Wed, 28 Sep 2022 07:52:58 -0400 Subject: [PATCH] Allow fetching script config (#79131) --- homeassistant/components/script/__init__.py | 31 +++++++++++++++- tests/components/script/test_init.py | 41 +++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index 16099c18ebe..f72e6dfc6ef 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -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, + }, + ) diff --git a/tests/components/script/test_init.py b/tests/components/script/test_init.py index 90b08d02b62..cc9c9a22302 100644 --- a/tests/components/script/test_init.py +++ b/tests/components/script/test_init.py @@ -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) -- GitLab