rename_device
Rename any Domoticz device by specifying its IDX or existing name to assign a new identifier.
Instructions
Rename a device by IDX or its old Name.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| new_name | Yes | ||
| idx | No | ||
| old_name | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/domoticz_mcp/server.py:960-970 (handler)The rename_device tool handler function. It takes new_name, idx (optional), and old_name (optional). Resolves the device by idx or old_name, sends a rename command to Domoticz via the API, invalidates the device cache, and returns the response.
async def rename_device(new_name: str, idx: int | None = None, old_name: str | None = None) -> str: """Rename a device by IDX or its old Name.""" if idx is None and old_name is None: return '{"status": "error", "message": "Must provide either idx or old_name"}' async with create_client() as client: resolved_idx = await _resolve_device_idx(client, idx, old_name) if resolved_idx is None: return '{"status": "error", "message": "Device not found"}' response = await _do_request(client, "GET", f"{DOMOTICZ_API_URL}?type=command¶m=renamedevice&name={urllib.parse.quote(new_name)}&idx={resolved_idx}") _device_cache["timestamp"] = 0 # Invalidate cache return response.text - src/domoticz_mcp/server.py:960-960 (registration)The @mcp.tool() decorator registers rename_device as an MCP tool, making it discoverable and callable by the MCP protocol.
async def rename_device(new_name: str, idx: int | None = None, old_name: str | None = None) -> str: - src/domoticz_mcp/server.py:959-960 (schema)The function signature serves as the input schema. Parameters: new_name (str, required), idx (int, optional), old_name (str, optional). The docstring 'Rename a device by IDX or its old Name' acts as the tool description.
@mcp.tool() async def rename_device(new_name: str, idx: int | None = None, old_name: str | None = None) -> str: - src/domoticz_mcp/server.py:373-375 (helper)The _resolve_device_idx helper is used by rename_device to convert an optional name or idx into a resolved device idx by looking up the device cache.
async def _resolve_device_idx(client: "httpx.AsyncClient", idx: Optional[int] = None, name: Optional[str] = None) -> Optional[int]: """Resolve a device to its idx.""" return await _resolve_idx(client, idx, name, _device_cache, f"{DOMOTICZ_API_URL}?type=command¶m=getdevices&filter=all&used=true") - src/domoticz_mcp/server.py:354-370 (helper)The generic _resolve_idx helper function used for resolving entity names to idx values. It either returns the provided idx or looks up the name in cached data.
async def _resolve_idx( client: "httpx.AsyncClient", idx: Optional[int], name: Optional[str], cache: Dict[str, Any], api_url: str ) -> Optional[int]: """Resolve an entity to its idx by either using the provided idx or looking up by name.""" if idx is not None: return idx if not name: return None items = await _get_cached_data(client, cache, api_url) for item in items: if item.get("Name", "").lower() == name.lower(): return int(str(item.get("idx"))) return None