From 4843d330f34f9732a01d3e7fa6ba164e0ee3967e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 28 Sep 2022 06:49:00 -0400 Subject: [PATCH] Allow fetching automation config (#79130) --- .../components/automation/__init__.py | 33 +++++++++++++++-- tests/components/automation/test_init.py | 36 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 841f015fa0f..3037d7cc3a7 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -8,7 +8,7 @@ from typing import Any, Protocol, cast import voluptuous as vol from voluptuous.humanize import humanize_error -from homeassistant.components import blueprint +from homeassistant.components import blueprint, websocket_api from homeassistant.components.blueprint import CONF_USE_BLUEPRINT from homeassistant.const import ( ATTR_ENTITY_ID, @@ -290,6 +290,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: schema=vol.Schema({}), ) + websocket_api.async_register_command(hass, websocket_config) + return True @@ -326,7 +328,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity): self._logger = LOGGER self._variables = variables self._trigger_variables = trigger_variables - self._raw_config = raw_config + self.raw_config = raw_config self._blueprint_inputs = blueprint_inputs self._trace_config = trace_config self._attr_unique_id = automation_id @@ -477,7 +479,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity): with trace_automation( self.hass, self.unique_id, - self._raw_config, + self.raw_config, self._blueprint_inputs, trigger_context, self._trace_config, @@ -853,3 +855,28 @@ def _trigger_extract_entities(trigger_conf: dict) -> list[str]: return [trigger_conf[CONF_EVENT_DATA][CONF_ENTITY_ID]] return [] + + +@websocket_api.websocket_command({"type": "automation/config", "entity_id": str}) +def websocket_config( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: + """Get automation config.""" + component: EntityComponent[AutomationEntity] = hass.data[DOMAIN] + + automation = component.get_entity(msg["entity_id"]) + + if automation is None: + connection.send_error( + msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + ) + return + + connection.send_result( + msg["id"], + { + "config": automation.raw_config, + }, + ) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 2c0e5bd5b6c..3cdf0c3a477 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -1848,3 +1848,39 @@ async def test_recursive_automation(hass: HomeAssistant, automation_mode, caplog await hass.async_block_till_done() assert "Disallowed recursion detected" not in caplog.text + + +async def test_websocket_config(hass, hass_ws_client): + """Test config command.""" + config = { + "alias": "hello", + "trigger": {"platform": "event", "event_type": "test_event"}, + "action": {"service": "test.automation", "data": 100}, + } + assert await async_setup_component( + hass, automation.DOMAIN, {automation.DOMAIN: config} + ) + client = await hass_ws_client(hass) + await client.send_json( + { + "id": 5, + "type": "automation/config", + "entity_id": "automation.hello", + } + ) + + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] == {"config": config} + + await client.send_json( + { + "id": 6, + "type": "automation/config", + "entity_id": "automation.not_exist", + } + ) + + msg = await client.receive_json() + assert not msg["success"] + assert msg["error"]["code"] == "not_found"