Skip to content

Commit f758cfa

Browse files
Add get_conditions_for_target websocket command (home-assistant#157344)
Co-authored-by: Erik Montnemery <[email protected]>
1 parent 9c7a928 commit f758cfa

File tree

3 files changed

+206
-154
lines changed

3 files changed

+206
-154
lines changed

homeassistant/components/websocket_api/automation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from homeassistant.const import CONF_TARGET
1010
from homeassistant.core import HomeAssistant
1111
from homeassistant.helpers import target as target_helpers
12+
from homeassistant.helpers.condition import (
13+
async_get_all_descriptions as async_get_all_condition_descriptions,
14+
)
1215
from homeassistant.helpers.entity import (
1316
entity_sources,
1417
get_device_class,
@@ -199,6 +202,16 @@ async def async_get_triggers_for_target(
199202
)
200203

201204

205+
async def async_get_conditions_for_target(
206+
hass: HomeAssistant, target_selector: ConfigType, expand_group: bool
207+
) -> set[str]:
208+
"""Get conditions for a target."""
209+
descriptions = await async_get_all_condition_descriptions(hass)
210+
return _async_get_automation_components_for_target(
211+
hass, target_selector, expand_group, descriptions
212+
)
213+
214+
202215
async def async_get_services_for_target(
203216
hass: HomeAssistant, target_selector: ConfigType, expand_group: bool
204217
) -> set[str]:

homeassistant/components/websocket_api/commands.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@
8787
from homeassistant.util.json import format_unserializable_data
8888

8989
from . import const, decorators, messages
90-
from .automation import async_get_services_for_target, async_get_triggers_for_target
90+
from .automation import (
91+
async_get_conditions_for_target,
92+
async_get_services_for_target,
93+
async_get_triggers_for_target,
94+
)
9195
from .connection import ActiveConnection
9296
from .messages import construct_event_message, construct_result_message
9397

@@ -109,6 +113,7 @@ def async_register_commands(
109113
async_reg(hass, handle_execute_script)
110114
async_reg(hass, handle_extract_from_target)
111115
async_reg(hass, handle_fire_event)
116+
async_reg(hass, handle_get_conditions_for_target)
112117
async_reg(hass, handle_get_config)
113118
async_reg(hass, handle_get_services)
114119
async_reg(hass, handle_get_services_for_target)
@@ -903,6 +908,29 @@ async def handle_get_triggers_for_target(
903908
connection.send_result(msg["id"], triggers)
904909

905910

911+
@decorators.websocket_command(
912+
{
913+
vol.Required("type"): "get_conditions_for_target",
914+
vol.Required("target"): cv.TARGET_FIELDS,
915+
vol.Optional("expand_group", default=True): bool,
916+
}
917+
)
918+
@decorators.async_response
919+
async def handle_get_conditions_for_target(
920+
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
921+
) -> None:
922+
"""Handle get conditions for target command.
923+
924+
This command returns all conditions that can be used with any entities that are currently
925+
part of a target.
926+
"""
927+
conditions = await async_get_conditions_for_target(
928+
hass, msg["target"], msg["expand_group"]
929+
)
930+
931+
connection.send_result(msg["id"], conditions)
932+
933+
906934
@decorators.websocket_command(
907935
{
908936
vol.Required("type"): "get_services_for_target",

0 commit comments

Comments
 (0)