Allow fetching automation config (#79130)

This commit is contained in:
Paulus Schoutsen 2022-09-28 06:49:00 -04:00 committed by GitHub
parent 5389ff3253
commit 4843d330f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 3 deletions

View File

@ -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,
},
)

View File

@ -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"