Skip to content

Commit 3e9d0c8

Browse files
kingpanther13claude
andcommitted
fix: guard string error values, use shared apply_entity_category for helpers
- apply_entity_category: add isinstance guard before .get() on error value, matching pattern in tools_config_helpers.py - helpers: replace three inline category constructions with shared apply_entity_category (create path, config-store-types update, standard update), consistent with automations/scripts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7a47902 commit 3e9d0c8

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/ha_mcp/tools/tools_config_helpers.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ..errors import ErrorCode, create_error_response
1717
from .helpers import exception_to_structured_error, log_tool_usage, raise_tool_error
1818
from .util_helpers import (
19+
apply_entity_category,
1920
coerce_bool_param,
2021
parse_string_list_param,
2122
wait_for_entity_registered,
@@ -653,8 +654,8 @@ async def ha_config_set_helper(
653654
except Exception as e:
654655
helper_data["warning"] = f"Helper created but verification failed: {e}"
655656

656-
# Update entity registry if area_id, labels, or category specified
657-
if (area_id or labels or category) and entity_id:
657+
# Update entity registry if area_id or labels specified
658+
if (area_id or labels) and entity_id:
658659
update_message: dict[str, Any] = {
659660
"type": "config/entity_registry/update",
660661
"entity_id": entity_id,
@@ -663,24 +664,26 @@ async def ha_config_set_helper(
663664
update_message["area_id"] = area_id
664665
if labels:
665666
update_message["labels"] = labels
666-
if category:
667-
update_message["categories"] = {"helpers": category}
668667

669668
update_result = await client.send_websocket_message(
670669
update_message
671670
)
672671
if update_result.get("success"):
673672
helper_data["area_id"] = area_id
674673
helper_data["labels"] = labels
675-
if category:
676-
helper_data["category"] = category
677674
else:
678675
error_detail = update_result.get("error", {})
679676
error_msg = error_detail.get("message", "Unknown error") if isinstance(error_detail, dict) else str(error_detail)
680677
helper_data["warning"] = (
681678
f"Helper created but entity registry update failed: {error_msg}"
682679
)
683680

681+
# Apply category via shared helper (consistent with automations/scripts)
682+
if category and entity_id:
683+
await apply_entity_category(
684+
client, entity_id, category, "helpers", helper_data, "helper"
685+
)
686+
684687
return {
685688
"success": True,
686689
"action": "create",
@@ -898,8 +901,8 @@ async def ha_config_set_helper(
898901
))
899902
updated_data = result.get("result", {})
900903

901-
# Also update entity registry for icon, area, labels, and category
902-
if icon or area_id or labels or category:
904+
# Also update entity registry for icon, area, and labels
905+
if icon or area_id or labels:
903906
registry_update: dict[str, Any] = {
904907
"type": "config/entity_registry/update",
905908
"entity_id": entity_id,
@@ -910,8 +913,6 @@ async def ha_config_set_helper(
910913
registry_update["area_id"] = area_id
911914
if labels:
912915
registry_update["labels"] = labels
913-
if category:
914-
registry_update["categories"] = {"helpers": category}
915916
reg_result = await client.send_websocket_message(registry_update)
916917
if not reg_result.get("success"):
917918
error_detail = reg_result.get("error", {})
@@ -921,6 +922,12 @@ async def ha_config_set_helper(
921922
f"Config updated but entity registry update failed: {error_msg}"
922923
)
923924

925+
# Apply category via shared helper
926+
if category:
927+
await apply_entity_category(
928+
client, entity_id, category, "helpers", updated_data, "helper"
929+
)
930+
924931
else:
925932
# Standard helpers: entity registry update only
926933
update_msg = {
@@ -936,8 +943,6 @@ async def ha_config_set_helper(
936943
update_msg["area_id"] = area_id
937944
if labels:
938945
update_msg["labels"] = labels
939-
if category:
940-
update_msg["categories"] = {"helpers": category}
941946

942947
result = await client.send_websocket_message(update_msg)
943948

@@ -950,6 +955,12 @@ async def ha_config_set_helper(
950955
context={"helper_type": helper_type, "entity_id": entity_id},
951956
))
952957

958+
# Apply category via shared helper
959+
if category:
960+
await apply_entity_category(
961+
client, entity_id, category, "helpers", updated_data, "helper"
962+
)
963+
953964
# Wait for entity to reflect the update
954965
wait_bool = coerce_bool_param(wait, "wait", default=True)
955966
response: dict[str, Any] = {

src/ha_mcp/tools/util_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ async def apply_entity_category(
473473
if ws_result.get("success"):
474474
result_dict["category"] = category
475475
else:
476-
error_msg = ws_result.get("error", {}).get("message", "Unknown error")
476+
error_detail = ws_result.get("error", {})
477+
error_msg = error_detail.get("message", "Unknown error") if isinstance(error_detail, dict) else str(error_detail)
477478
logger.warning(f"Failed to set category for {entity_id}: {error_msg}")
478479
result_dict["category_warning"] = (
479480
f"{entity_type.capitalize()} saved but failed to set category: {error_msg}"

0 commit comments

Comments
 (0)