run_scene
Activate a Xiaomi smart home scene to run all preset device actions by supplying the scene ID.
Instructions
执行一个米家场景,触发该场景中预设的所有设备操作。
Args:
scene_id: 场景IDInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scene_id | Yes |
Implementation Reference
- mcp_server/server.py:119-126 (handler)MCP tool handler: registers 'run_scene' as an MCP tool that sends POST /api/scenes/{scene_id}/run to the backend API.
@mcp.tool() async def run_scene(scene_id: str) -> dict: """执行一个米家场景,触发该场景中预设的所有设备操作。 Args: scene_id: 场景ID """ return await _request("POST", f"/scenes/{scene_id}/run") - mcp_server/server.py:119-126 (schema)Input/Output schema: accepts a single 'scene_id' string parameter, returns a dict (the JSON response from the API).
@mcp.tool() async def run_scene(scene_id: str) -> dict: """执行一个米家场景,触发该场景中预设的所有设备操作。 Args: scene_id: 场景ID """ return await _request("POST", f"/scenes/{scene_id}/run") - mcp_server/server.py:119-120 (registration)Registration: the @mcp.tool() decorator registers 'run_scene' as an MCP tool on the FastMCP server.
@mcp.tool() async def run_scene(scene_id: str) -> dict: - app/api/scenes.py:44-68 (handler)Flask API handler for POST /scenes/<scene_id>/run, the REST endpoint that the MCP tool calls.
@scenes_ns.route("/<scene_id>/run", methods=["POST"]) @auth_required @limiter.limit("30 per minute") def run_scene(scene_id): """执行场景 --- tags: - 场景 security: - cookieAuth: [] - bearerAuth: [] parameters: - in: path name: scene_id type: string required: true responses: 200: description: 执行成功 """ try: result = SceneService.run_scene(get_current_user_id(), scene_id) return success(result) except Exception as e: return error(str(e), 500) - app/services/scene_service.py:32-37 (helper)Helper service: the SceneService.run_scene method that looks up the home_id from cache and calls the MiJia API to execute the scene.
def run_scene(user_id: int, scene_id: str) -> dict: api = api_pool.get_api(user_id) scene = SceneCache.query.filter_by(user_id=user_id, scene_id=scene_id).first() home_id = scene.home_id if scene else None api.run_scene(scene_id=scene_id, home_id=home_id) return {"scene_id": scene_id, "status": "executed"} - Automation helper: calls SceneService.run_scene when an automation rule's action_type is 'run_scene'.
elif rule.action_type == "run_scene": cfg = rule.action_config SceneService.run_scene(rule.user_id, cfg["scene_id"])