Skip to content

异常与错误码

异常类层级

mijiaAPI 定义了以下异常类:

异常类说明
LoginError登录失败
APIErrorAPI 通用调用失败
DeviceNotFoundError设备未找到
MultipleDevicesFoundError找到多个匹配的设备
DeviceGetError获取设备属性失败
DeviceSetError设置设备属性失败
DeviceActionError执行设备动作失败
GetDeviceInfoError获取设备规格信息失败

异常处理示例

python
from mijiaAPI import (
    mijiaAPI,
    mijiaDevice,
    LoginError,
    DeviceNotFoundError,
    MultipleDevicesFoundError,
    DeviceGetError,
    DeviceSetError,
    DeviceActionError,
    APIError,
)

api = mijiaAPI()

try:
    api.login()
except LoginError as e:
    print(f"登录失败: {e}")

try:
    device = mijiaDevice(api, dev_name="我的台灯")
except DeviceNotFoundError as e:
    print(f"设备未找到: {e}")
except MultipleDevicesFoundError as e:
    print(f"找到多个匹配的设备: {e}")

try:
    device.get('brightness')
except DeviceGetError as e:
    print(f"获取属性失败: {e}")
except ValueError as e:
    print(f"属性名称不支持: {e}")

try:
    device.set('brightness', 150)
except DeviceSetError as e:
    print(f"设置属性失败: {e}")

try:
    device.run_action('toggle')
except DeviceActionError as e:
    print(f"执行动作失败: {e}")

try:
    api.get_devices_list()
except APIError as e:
    print(f"API调用失败: {e}")

错误码表

以下是米家 API 返回的错误码及对应描述:

错误码描述
-10000未知错误
-10001服务不可用
-10002参数无效
-10003资源不足
-10004内部错误
-10005权限不足
-10006执行超时
-10007设备离线或者不存在
-10020未授权OAuth2
-10030无效的token(HTTP)
-10040无效的消息格式
-10050无效的证书
-704000000未知错误
-704010000未授权(设备可能被删除)
-704014006没找到设备描述
-704030013Property不可读
-704030023Property不可写
-704030033Property不可订阅
-704040002Service不存在
-704040003Property不存在
-704040004Event不存在
-704040005Action不存在
-704040999功能未上线
-704042001Device不存在
-704042011设备离线
-704053036设备操作超时
-704053100设备在当前状态下无法执行此操作
-704083036设备操作超时
-704090001Device不存在
-704220008无效的ID
-704220025Action参数个数不匹配
-704220035Action参数错误
-704220043Property值错误
-704222034Action返回值错误
-705004000未知错误
-705004501未知错误
-705201013Property不可读
-705201015Action执行错误
-705201023Property不可写
-705201033Property不可订阅
-706012000未知错误
-706012013Property不可读
-706012015Action执行错误
-706012023Property不可写
-706012033Property不可订阅
-706012043Property值错误
-706014006没找到设备描述

基于 GPL-3.0 许可证发布