如何通过ask-smapi-sdk或REST查询Alexa智能家居技能设备状态?
实现Alexa智能家居设备状态查询(对应控制台设备页操作)
你可以通过以下两种方式实现和开发者控制台设备页相同的设备查询操作:
一、使用ask-smapi-sdk调用
利用SDK内置的get_smarthome_endpoint_state_v1方法,直接获取指定设备的状态,代码示例如下:
# 构造设备状态查询参数 endpoint_state_request = { "skill_id": self.skill_id, "stage": self.stage, "endpoint_id": endpoint_id # 替换为目标设备的endpointId } # 调用SDK方法获取设备状态 state_response = self.skill_client.get_smarthome_endpoint_state_v1(**endpoint_state_request) # 提取设备状态数据 device_current_state = state_response.get("endpointState", {}) # 可根据需要解析具体能力的属性,比如电源状态 power_state = device_current_state.get("properties", {}).get("powerState", {})
该方法返回的结果与控制台设备页展示的设备状态完全一致,包含设备所有已上报的能力属性值。
二、直接调用REST API
如果需要更灵活的控制,也可以直接调用SMAPI的REST接口:
- 请求方法:GET
- 请求URL:
https://api.amazonalexa.com/v1/skills/{skillId}/stages/{stage}/endpoints/{endpointId}/state - 请求头:
Authorization: Bearer {你的SMAPI访问令牌}Content-Type: application/json
示例curl命令:
curl -X GET "https://api.amazonalexa.com/v1/skills/your-skill-id/stages/development/endpoints/your-endpoint-id/state" \ -H "Authorization: Bearer your-smapi-access-token" \ -H "Content-Type: application/json"
注意事项
- 确保你的SMAPI访问令牌拥有
alexa::smarthome:endpoint:read权限,否则会返回权限错误 stage参数需与技能部署阶段匹配(development或live)endpointId必须是已成功关联到目标技能的有效设备ID
内容的提问来源于stack exchange,提问作者shalama
相关产品推荐
相关产品推荐

