toggle_switch
Toggle a Domoticz switch or light by specifying its IDX or name, with IDX recommended for accuracy.
Instructions
Toggle a switch or light by IDX or Name. Prefer using IDX for precision.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| idx | No | ||
| name | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/domoticz_mcp/server.py:574-584 (handler)The main handler function for the toggle_switch tool. Accepts idx or name, resolves the device index, and sends a 'Toggle' command to the Domoticz API via switchlight endpoint.
@mcp.tool() async def toggle_switch(idx: int | None = None, name: str | None = None) -> str: """Toggle a switch or light by IDX or Name. Prefer using IDX for precision.""" if idx is None and name is None: return '{"status": "error", "message": "Must provide either idx or name"}' async with create_client() as client: resolved_idx = await _resolve_device_idx(client, idx, 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=switchlight&idx={resolved_idx}&switchcmd=Toggle") return response.text - src/domoticz_mcp/server.py:574-574 (registration)The tool is registered with the MCP server via the @mcp.tool() decorator on line 574.
@mcp.tool() - src/domoticz_mcp/server.py:373-376 (helper)Helper function used by toggle_switch to resolve a device idx from a provided idx or name (case-insensitive lookup from cached device list).
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)Generic helper that resolves an entity (device, scene, variable) to its idx by either direct idx or name-based lookup in a cached list.
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