Skip to content

Commit 278b7a5

Browse files
julienldclaude
andauthored
fix: normalize automation GET config for round-trip compatibility (#221)
Home Assistant automation GET API returns config with: - Plural field names (triggers, actions, conditions) - 'trigger' key inside trigger objects for platform type But the SET API expects: - Singular field names (trigger, action, condition) - 'platform' key inside trigger objects This fix applies _normalize_config_for_roundtrip() to the GET response, ensuring configs retrieved via ha_config_get_automation can be directly passed to ha_config_set_automation without manual transformation. Fixes #220 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent f9c512b commit 278b7a5

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

src/ha_mcp/tools/tools_config_automations.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,56 @@ def _normalize_automation_config(config: dict[str, Any]) -> dict[str, Any]:
5050
return normalized
5151

5252

53+
def _normalize_trigger_keys(triggers: list[dict[str, Any]]) -> list[dict[str, Any]]:
54+
"""
55+
Normalize trigger objects for round-trip compatibility.
56+
57+
Home Assistant GET API returns triggers with 'trigger' key for the platform type,
58+
but the SET API expects 'platform' key. This function converts between formats.
59+
60+
Args:
61+
triggers: List of trigger configuration dicts
62+
63+
Returns:
64+
List of triggers with 'platform' key instead of 'trigger' key
65+
"""
66+
normalized_triggers = []
67+
for trigger in triggers:
68+
normalized_trigger = trigger.copy()
69+
# Convert 'trigger' key to 'platform' if present and 'platform' is not
70+
if "trigger" in normalized_trigger and "platform" not in normalized_trigger:
71+
normalized_trigger["platform"] = normalized_trigger.pop("trigger")
72+
normalized_triggers.append(normalized_trigger)
73+
return normalized_triggers
74+
75+
76+
def _normalize_config_for_roundtrip(config: dict[str, Any]) -> dict[str, Any]:
77+
"""
78+
Normalize automation config from GET response for direct use in SET.
79+
80+
This ensures a config retrieved via ha_config_get_automation can be
81+
directly passed to ha_config_set_automation without modification.
82+
83+
Transformations:
84+
1. Field names: triggers -> trigger, actions -> action, conditions -> condition
85+
2. Trigger keys: trigger -> platform (inside each trigger object)
86+
87+
Args:
88+
config: Raw automation configuration from HA API
89+
90+
Returns:
91+
Normalized configuration compatible with SET API
92+
"""
93+
# First normalize field names (plural -> singular)
94+
normalized = _normalize_automation_config(config)
95+
96+
# Then normalize trigger keys (trigger -> platform)
97+
if "trigger" in normalized and isinstance(normalized["trigger"], list):
98+
normalized["trigger"] = _normalize_trigger_keys(normalized["trigger"])
99+
100+
return normalized
101+
102+
53103
def register_config_automation_tools(mcp: Any, client: Any, **kwargs: Any) -> None:
54104
"""Register Home Assistant automation configuration tools."""
55105

@@ -76,11 +126,13 @@ async def ha_config_get_automation(
76126
"""
77127
try:
78128
config_result = await client.get_automation_config(identifier)
129+
# Normalize config for round-trip compatibility (GET → SET)
130+
normalized_config = _normalize_config_for_roundtrip(config_result)
79131
return {
80132
"success": True,
81133
"action": "get",
82134
"identifier": identifier,
83-
"config": config_result,
135+
"config": normalized_config,
84136
}
85137
except Exception as e:
86138
# Handle 404 errors gracefully (often used to verify deletion)

0 commit comments

Comments
 (0)