mijiaAPI 1.3.13__tar.gz → 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mijiaapi-1.4.0/PKG-INFO +238 -0
- mijiaapi-1.4.0/README.md +213 -0
- mijiaapi-1.4.0/mijiaAPI/__main__.py +286 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/apis.py +3 -2
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/devices.py +17 -1
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/pyproject.toml +8 -2
- mijiaapi-1.3.13/PKG-INFO +0 -81
- mijiaapi-1.3.13/README.md +0 -56
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/LICENSE +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/__init__.py +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/code.py +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/logger.py +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/login.py +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/urls.py +0 -0
- {mijiaapi-1.3.13 → mijiaapi-1.4.0}/mijiaAPI/utils.py +0 -0
mijiaapi-1.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mijiaAPI
|
|
3
|
+
Version: 1.4.0
|
|
4
|
+
Summary: A Python API for Xiaomi Mijia
|
|
5
|
+
License: GPLv3
|
|
6
|
+
Author: Do1e
|
|
7
|
+
Author-email: dpj.email@qq.com
|
|
8
|
+
Requires-Python: >=3.9,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
10
|
+
Classifier: License :: Other/Proprietary License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Requires-Dist: pillow (>=11.0.0,<12.0.0)
|
|
19
|
+
Requires-Dist: qrcode (>=8.0,<9.0)
|
|
20
|
+
Requires-Dist: requests (>=2.32.3,<3.0.0)
|
|
21
|
+
Project-URL: Homepage, https://github.com/Do1e/mijia-api
|
|
22
|
+
Project-URL: Repository, https://github.com/Do1e/mijia-api
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# mijiaAPI
|
|
26
|
+
|
|
27
|
+
小米米家设备的API,可以使用代码直接控制米家设备。
|
|
28
|
+
|
|
29
|
+
[](https://github.com/Do1e/mijia-api)
|
|
30
|
+
[](https://pypi.org/project/mijiaAPI/)
|
|
31
|
+
[](https://opensource.org/licenses/GPL-3.0)
|
|
32
|
+
|
|
33
|
+
## 安装
|
|
34
|
+
|
|
35
|
+
### 从 PyPI 安装(推荐)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install mijiaAPI
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 从源码安装
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
git clone https://github.com/Do1e/mijia-api.git
|
|
45
|
+
cd mijia-api
|
|
46
|
+
pip install .
|
|
47
|
+
# Or `pip install -e .` for editable mode
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
或者使用 poetry:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
poetry install
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 使用
|
|
57
|
+
|
|
58
|
+
使用实例可以参考 `demos` 文件夹下的示例代码,以下是基本使用说明。
|
|
59
|
+
|
|
60
|
+
### 登录
|
|
61
|
+
|
|
62
|
+
`mijiaLogin`:登录小米账号,获取控制设备必须的 `userId`, `ssecurity`, `deviceId`, `serviceToken` 等信息。
|
|
63
|
+
|
|
64
|
+
#### 登录方法:
|
|
65
|
+
|
|
66
|
+
* `QRlogin() -> dict`:扫描二维码登录(推荐)
|
|
67
|
+
- 在支持 tty 的终端直接显示二维码
|
|
68
|
+
- 或在当前目录查看生成的 `qr.png` 文件
|
|
69
|
+
|
|
70
|
+
* `login(username: str, password: str) -> dict`:账号密码登录
|
|
71
|
+
- **注意:此方法大概率需要手机验证码验证,建议优先使用二维码登录**
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
### API
|
|
75
|
+
|
|
76
|
+
`mijiaAPI`:核心API实现,使用 `mijiaLogin` 登录后返回的信息进行初始化。
|
|
77
|
+
|
|
78
|
+
#### 初始化与状态检查:
|
|
79
|
+
|
|
80
|
+
* `__init__(auth_data: dict)`:初始化 API 对象
|
|
81
|
+
- `auth_data` 必须包含 `userId`, `deviceId`, `ssecurity`, `serviceToken` 四个字段
|
|
82
|
+
|
|
83
|
+
* `available -> bool`:检查传入的 `auth_data` 是否有效,根据 `auth_data` 中的 `expireTime` 字段判断
|
|
84
|
+
|
|
85
|
+
#### 设备与场景获取与控制:
|
|
86
|
+
|
|
87
|
+
* `get_devices_list() -> list`:获取设备列表
|
|
88
|
+
* `get_homes_list() -> list`:获取家庭列表(包含房间信息)
|
|
89
|
+
* `get_scenes_list(home_id: str) -> list`:获取手动场景列表
|
|
90
|
+
- 在米家 App 中通过 **米家→添加→手动控制** 设置
|
|
91
|
+
* `run_scene(scene_id: str) -> bool`:运行指定场景
|
|
92
|
+
* `get_consumable_items(home_id: str) -> list`:获取设备的耗材信息
|
|
93
|
+
* `get_devices_prop(data: list) -> list`:获取设备属性
|
|
94
|
+
* `set_devices_prop(data: list) -> list`:设置设备属性
|
|
95
|
+
* `run_action(data: dict) -> dict`:执行设备的特定动作
|
|
96
|
+
|
|
97
|
+
设备属性和动作的相关参数(`siid`, `piid`, `aiid`)可以从 [米家产品库](https://home.miot-spec.com) 查询:
|
|
98
|
+
* 访问 `https://home.miot-spec.com/spec/{model}`(`model` 在设备列表中获取)
|
|
99
|
+
* 例如:[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
|
|
100
|
+
|
|
101
|
+
**注意**:并非所有米家产品库中列出的方法都可用,需要自行测试验证。
|
|
102
|
+
|
|
103
|
+
### 设备信息获取
|
|
104
|
+
|
|
105
|
+
使用 `get_device_info()` 函数可从米家规格平台在线获取设备属性字典:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from mijiaAPI import get_device_info
|
|
109
|
+
|
|
110
|
+
# 获取设备规格信息
|
|
111
|
+
device_info = get_device_info('yeelink.light.lamp4') # 米家台灯 1S 的 model
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
详细示例:[demos/test_get_device_info.py](demos/test_get_device_info.py)
|
|
115
|
+
|
|
116
|
+
### 设备控制封装
|
|
117
|
+
|
|
118
|
+
`mijiaDevices`:基于 `mijiaAPI` 的高级封装,提供更简便的设备控制方式。
|
|
119
|
+
|
|
120
|
+
#### 初始化:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
mijiaDevices(api: mijiaAPI, dev_info: dict = None, dev_name: str = None, did: str = None, sleep_time: float = 0.5)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
* `api`:已初始化的 `mijiaAPI` 对象
|
|
127
|
+
* `dev_info`:设备属性字典(可选)
|
|
128
|
+
- 可通过 `get_device_info()` 函数获取
|
|
129
|
+
- **注意**:如果提供了 `dev_info`,则不需要提供 `dev_name`
|
|
130
|
+
* `dev_name`:设备名称,用于自动查找设备(可选)
|
|
131
|
+
- 例如:`dev_name='台灯'`,会自动查找名称包含“台灯”的设备
|
|
132
|
+
- **注意**:如果提供了 `dev_name`,则不需要提供 `dev_info` 和 `did`
|
|
133
|
+
* `did`:设备ID,便于直接通过属性名访问(可选)
|
|
134
|
+
- 如果初始化时未提供,无法使用属性样式访问,需要使用 `get()` 和 `set()` 方法指定 `did`
|
|
135
|
+
- 使用 `dev_name` 初始化时,`did` 会自动获取
|
|
136
|
+
* `sleep_time`:属性操作间隔时间,单位秒(默认0.5秒)
|
|
137
|
+
- **重要**:设置属性后立即获取可能不符合预期,需设置适当延迟
|
|
138
|
+
|
|
139
|
+
#### 使用方法控制:
|
|
140
|
+
|
|
141
|
+
* `set(name: str, did: str, value: Union[bool, int]) -> bool`:设置设备属性
|
|
142
|
+
* `get(name: str, did: str) -> Union[bool, int, float, str]`:获取设备属性
|
|
143
|
+
* `run_action(name: str, did: str = None, value: Any = None, **kwargs) -> bool`:执行设备动作
|
|
144
|
+
|
|
145
|
+
#### 属性样式访问:
|
|
146
|
+
|
|
147
|
+
需在初始化时提供 `did` 或者使用 `dev_name` 初始化
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
# 示例:控制台灯
|
|
151
|
+
device = mijiaDevices(api, dev_name='台灯')
|
|
152
|
+
device.on = True # 打开灯
|
|
153
|
+
device.brightness = 60 # 设置亮度
|
|
154
|
+
current_temp = device.color_temperature # 获取色温
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
属性名规则:使用下划线替代连字符(如 `color-temperature` 变为 `color_temperature`)
|
|
158
|
+
|
|
159
|
+
#### 示例:
|
|
160
|
+
|
|
161
|
+
* 使用自然语言让小爱音箱执行:[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
162
|
+
* 通过属性直接控制台灯:[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)
|
|
163
|
+
|
|
164
|
+
### Mijia API CLI
|
|
165
|
+
`mijiaAPI` 还提供了一个命令行工具,可以直接在终端中使用。
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
> python -m mijiaAPI --help
|
|
169
|
+
> mijiaAPI --help
|
|
170
|
+
usage: __main__.py [-h] [-p AUTH_PATH] [-l] [--list_homes] [--list_scenes] [--list_consumable_items]
|
|
171
|
+
[--run_scene SCENE_ID/SCENE_NAME [SCENE_ID/SCENE_NAME ...]] [--get_device_info DEVICE_MODEL]
|
|
172
|
+
{get,set} ...
|
|
173
|
+
|
|
174
|
+
Mijia API CLI
|
|
175
|
+
|
|
176
|
+
positional arguments:
|
|
177
|
+
{get,set}
|
|
178
|
+
get 获取设备属性
|
|
179
|
+
set 设置设备属性
|
|
180
|
+
|
|
181
|
+
options:
|
|
182
|
+
-h, --help show this help message and exit
|
|
183
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
184
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
185
|
+
-l, --list_devices 列出所有米家设备
|
|
186
|
+
--list_homes 列出家庭列表
|
|
187
|
+
--list_scenes 列出场景列表
|
|
188
|
+
--list_consumable_items
|
|
189
|
+
列出耗材列表
|
|
190
|
+
--run_scene SCENE_ID/SCENE_NAME [SCENE_ID/SCENE_NAME ...]
|
|
191
|
+
运行场景,指定场景ID或名称
|
|
192
|
+
--get_device_info DEVICE_MODEL
|
|
193
|
+
获取设备信息,指定设备model,先使用 --list_devices 获取
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
> python -m mijiaAPI get --help
|
|
198
|
+
> mijiaAPI get --help
|
|
199
|
+
usage: __main__.py get [-h] [-p AUTH_PATH] --dev_name DEV_NAME --prop_name PROP_NAME
|
|
200
|
+
|
|
201
|
+
options:
|
|
202
|
+
-h, --help show this help message and exit
|
|
203
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
204
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
205
|
+
--dev_name DEV_NAME 设备名称,指定为米家APP中设定的名称
|
|
206
|
+
--prop_name PROP_NAME
|
|
207
|
+
属性名称,先使用 --get_device_info 获取
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```
|
|
211
|
+
> python -m mijiaAPI set --help
|
|
212
|
+
> mijiaAPI set --help
|
|
213
|
+
usage: __main__.py set [-h] [-p AUTH_PATH] --dev_name DEV_NAME --prop_name PROP_NAME --value VALUE
|
|
214
|
+
|
|
215
|
+
options:
|
|
216
|
+
-h, --help show this help message and exit
|
|
217
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
218
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
219
|
+
--dev_name DEV_NAME 设备名称,指定为米家APP中设定的名称
|
|
220
|
+
--prop_name PROP_NAME
|
|
221
|
+
属性名称,先使用 --get_device_info 获取
|
|
222
|
+
--value VALUE 需要设定的属性值
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## 致谢
|
|
226
|
+
|
|
227
|
+
* [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
|
|
228
|
+
|
|
229
|
+
## 开源许可
|
|
230
|
+
|
|
231
|
+
本项目采用 [GPL-3.0](LICENSE) 开源许可证。
|
|
232
|
+
|
|
233
|
+
## 免责声明
|
|
234
|
+
|
|
235
|
+
* 本项目仅供学习交流使用,不得用于商业用途,如有侵权请联系删除
|
|
236
|
+
* 用户使用本项目所产生的任何后果,需自行承担风险
|
|
237
|
+
* 开发者不对使用本项目产生的任何直接或间接损失负责
|
|
238
|
+
|
mijiaapi-1.4.0/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# mijiaAPI
|
|
2
|
+
|
|
3
|
+
小米米家设备的API,可以使用代码直接控制米家设备。
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Do1e/mijia-api)
|
|
6
|
+
[](https://pypi.org/project/mijiaAPI/)
|
|
7
|
+
[](https://opensource.org/licenses/GPL-3.0)
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
### 从 PyPI 安装(推荐)
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install mijiaAPI
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 从源码安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
git clone https://github.com/Do1e/mijia-api.git
|
|
21
|
+
cd mijia-api
|
|
22
|
+
pip install .
|
|
23
|
+
# Or `pip install -e .` for editable mode
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
或者使用 poetry:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
poetry install
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 使用
|
|
33
|
+
|
|
34
|
+
使用实例可以参考 `demos` 文件夹下的示例代码,以下是基本使用说明。
|
|
35
|
+
|
|
36
|
+
### 登录
|
|
37
|
+
|
|
38
|
+
`mijiaLogin`:登录小米账号,获取控制设备必须的 `userId`, `ssecurity`, `deviceId`, `serviceToken` 等信息。
|
|
39
|
+
|
|
40
|
+
#### 登录方法:
|
|
41
|
+
|
|
42
|
+
* `QRlogin() -> dict`:扫描二维码登录(推荐)
|
|
43
|
+
- 在支持 tty 的终端直接显示二维码
|
|
44
|
+
- 或在当前目录查看生成的 `qr.png` 文件
|
|
45
|
+
|
|
46
|
+
* `login(username: str, password: str) -> dict`:账号密码登录
|
|
47
|
+
- **注意:此方法大概率需要手机验证码验证,建议优先使用二维码登录**
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### API
|
|
51
|
+
|
|
52
|
+
`mijiaAPI`:核心API实现,使用 `mijiaLogin` 登录后返回的信息进行初始化。
|
|
53
|
+
|
|
54
|
+
#### 初始化与状态检查:
|
|
55
|
+
|
|
56
|
+
* `__init__(auth_data: dict)`:初始化 API 对象
|
|
57
|
+
- `auth_data` 必须包含 `userId`, `deviceId`, `ssecurity`, `serviceToken` 四个字段
|
|
58
|
+
|
|
59
|
+
* `available -> bool`:检查传入的 `auth_data` 是否有效,根据 `auth_data` 中的 `expireTime` 字段判断
|
|
60
|
+
|
|
61
|
+
#### 设备与场景获取与控制:
|
|
62
|
+
|
|
63
|
+
* `get_devices_list() -> list`:获取设备列表
|
|
64
|
+
* `get_homes_list() -> list`:获取家庭列表(包含房间信息)
|
|
65
|
+
* `get_scenes_list(home_id: str) -> list`:获取手动场景列表
|
|
66
|
+
- 在米家 App 中通过 **米家→添加→手动控制** 设置
|
|
67
|
+
* `run_scene(scene_id: str) -> bool`:运行指定场景
|
|
68
|
+
* `get_consumable_items(home_id: str) -> list`:获取设备的耗材信息
|
|
69
|
+
* `get_devices_prop(data: list) -> list`:获取设备属性
|
|
70
|
+
* `set_devices_prop(data: list) -> list`:设置设备属性
|
|
71
|
+
* `run_action(data: dict) -> dict`:执行设备的特定动作
|
|
72
|
+
|
|
73
|
+
设备属性和动作的相关参数(`siid`, `piid`, `aiid`)可以从 [米家产品库](https://home.miot-spec.com) 查询:
|
|
74
|
+
* 访问 `https://home.miot-spec.com/spec/{model}`(`model` 在设备列表中获取)
|
|
75
|
+
* 例如:[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
|
|
76
|
+
|
|
77
|
+
**注意**:并非所有米家产品库中列出的方法都可用,需要自行测试验证。
|
|
78
|
+
|
|
79
|
+
### 设备信息获取
|
|
80
|
+
|
|
81
|
+
使用 `get_device_info()` 函数可从米家规格平台在线获取设备属性字典:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from mijiaAPI import get_device_info
|
|
85
|
+
|
|
86
|
+
# 获取设备规格信息
|
|
87
|
+
device_info = get_device_info('yeelink.light.lamp4') # 米家台灯 1S 的 model
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
详细示例:[demos/test_get_device_info.py](demos/test_get_device_info.py)
|
|
91
|
+
|
|
92
|
+
### 设备控制封装
|
|
93
|
+
|
|
94
|
+
`mijiaDevices`:基于 `mijiaAPI` 的高级封装,提供更简便的设备控制方式。
|
|
95
|
+
|
|
96
|
+
#### 初始化:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
mijiaDevices(api: mijiaAPI, dev_info: dict = None, dev_name: str = None, did: str = None, sleep_time: float = 0.5)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
* `api`:已初始化的 `mijiaAPI` 对象
|
|
103
|
+
* `dev_info`:设备属性字典(可选)
|
|
104
|
+
- 可通过 `get_device_info()` 函数获取
|
|
105
|
+
- **注意**:如果提供了 `dev_info`,则不需要提供 `dev_name`
|
|
106
|
+
* `dev_name`:设备名称,用于自动查找设备(可选)
|
|
107
|
+
- 例如:`dev_name='台灯'`,会自动查找名称包含“台灯”的设备
|
|
108
|
+
- **注意**:如果提供了 `dev_name`,则不需要提供 `dev_info` 和 `did`
|
|
109
|
+
* `did`:设备ID,便于直接通过属性名访问(可选)
|
|
110
|
+
- 如果初始化时未提供,无法使用属性样式访问,需要使用 `get()` 和 `set()` 方法指定 `did`
|
|
111
|
+
- 使用 `dev_name` 初始化时,`did` 会自动获取
|
|
112
|
+
* `sleep_time`:属性操作间隔时间,单位秒(默认0.5秒)
|
|
113
|
+
- **重要**:设置属性后立即获取可能不符合预期,需设置适当延迟
|
|
114
|
+
|
|
115
|
+
#### 使用方法控制:
|
|
116
|
+
|
|
117
|
+
* `set(name: str, did: str, value: Union[bool, int]) -> bool`:设置设备属性
|
|
118
|
+
* `get(name: str, did: str) -> Union[bool, int, float, str]`:获取设备属性
|
|
119
|
+
* `run_action(name: str, did: str = None, value: Any = None, **kwargs) -> bool`:执行设备动作
|
|
120
|
+
|
|
121
|
+
#### 属性样式访问:
|
|
122
|
+
|
|
123
|
+
需在初始化时提供 `did` 或者使用 `dev_name` 初始化
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
# 示例:控制台灯
|
|
127
|
+
device = mijiaDevices(api, dev_name='台灯')
|
|
128
|
+
device.on = True # 打开灯
|
|
129
|
+
device.brightness = 60 # 设置亮度
|
|
130
|
+
current_temp = device.color_temperature # 获取色温
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
属性名规则:使用下划线替代连字符(如 `color-temperature` 变为 `color_temperature`)
|
|
134
|
+
|
|
135
|
+
#### 示例:
|
|
136
|
+
|
|
137
|
+
* 使用自然语言让小爱音箱执行:[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
138
|
+
* 通过属性直接控制台灯:[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)
|
|
139
|
+
|
|
140
|
+
### Mijia API CLI
|
|
141
|
+
`mijiaAPI` 还提供了一个命令行工具,可以直接在终端中使用。
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
> python -m mijiaAPI --help
|
|
145
|
+
> mijiaAPI --help
|
|
146
|
+
usage: __main__.py [-h] [-p AUTH_PATH] [-l] [--list_homes] [--list_scenes] [--list_consumable_items]
|
|
147
|
+
[--run_scene SCENE_ID/SCENE_NAME [SCENE_ID/SCENE_NAME ...]] [--get_device_info DEVICE_MODEL]
|
|
148
|
+
{get,set} ...
|
|
149
|
+
|
|
150
|
+
Mijia API CLI
|
|
151
|
+
|
|
152
|
+
positional arguments:
|
|
153
|
+
{get,set}
|
|
154
|
+
get 获取设备属性
|
|
155
|
+
set 设置设备属性
|
|
156
|
+
|
|
157
|
+
options:
|
|
158
|
+
-h, --help show this help message and exit
|
|
159
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
160
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
161
|
+
-l, --list_devices 列出所有米家设备
|
|
162
|
+
--list_homes 列出家庭列表
|
|
163
|
+
--list_scenes 列出场景列表
|
|
164
|
+
--list_consumable_items
|
|
165
|
+
列出耗材列表
|
|
166
|
+
--run_scene SCENE_ID/SCENE_NAME [SCENE_ID/SCENE_NAME ...]
|
|
167
|
+
运行场景,指定场景ID或名称
|
|
168
|
+
--get_device_info DEVICE_MODEL
|
|
169
|
+
获取设备信息,指定设备model,先使用 --list_devices 获取
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
> python -m mijiaAPI get --help
|
|
174
|
+
> mijiaAPI get --help
|
|
175
|
+
usage: __main__.py get [-h] [-p AUTH_PATH] --dev_name DEV_NAME --prop_name PROP_NAME
|
|
176
|
+
|
|
177
|
+
options:
|
|
178
|
+
-h, --help show this help message and exit
|
|
179
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
180
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
181
|
+
--dev_name DEV_NAME 设备名称,指定为米家APP中设定的名称
|
|
182
|
+
--prop_name PROP_NAME
|
|
183
|
+
属性名称,先使用 --get_device_info 获取
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
> python -m mijiaAPI set --help
|
|
188
|
+
> mijiaAPI set --help
|
|
189
|
+
usage: __main__.py set [-h] [-p AUTH_PATH] --dev_name DEV_NAME --prop_name PROP_NAME --value VALUE
|
|
190
|
+
|
|
191
|
+
options:
|
|
192
|
+
-h, --help show this help message and exit
|
|
193
|
+
-p AUTH_PATH, --auth_path AUTH_PATH
|
|
194
|
+
认证文件保存路径,默认保存在~/.config/mijia-api-auth.json
|
|
195
|
+
--dev_name DEV_NAME 设备名称,指定为米家APP中设定的名称
|
|
196
|
+
--prop_name PROP_NAME
|
|
197
|
+
属性名称,先使用 --get_device_info 获取
|
|
198
|
+
--value VALUE 需要设定的属性值
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## 致谢
|
|
202
|
+
|
|
203
|
+
* [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
|
|
204
|
+
|
|
205
|
+
## 开源许可
|
|
206
|
+
|
|
207
|
+
本项目采用 [GPL-3.0](LICENSE) 开源许可证。
|
|
208
|
+
|
|
209
|
+
## 免责声明
|
|
210
|
+
|
|
211
|
+
* 本项目仅供学习交流使用,不得用于商业用途,如有侵权请联系删除
|
|
212
|
+
* 用户使用本项目所产生的任何后果,需自行承担风险
|
|
213
|
+
* 开发者不对使用本项目产生的任何直接或间接损失负责
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from .apis import mijiaAPI
|
|
9
|
+
from .devices import mijiaDevices, get_device_info
|
|
10
|
+
from .login import mijiaLogin
|
|
11
|
+
|
|
12
|
+
def parse_args(args):
|
|
13
|
+
parser = argparse.ArgumentParser(description="Mijia API CLI")
|
|
14
|
+
subparsers = parser.add_subparsers(dest='command')
|
|
15
|
+
parser.add_argument(
|
|
16
|
+
'-p', '--auth_path',
|
|
17
|
+
type=str,
|
|
18
|
+
default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
|
|
19
|
+
help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
|
|
20
|
+
)
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
'-l', '--list_devices',
|
|
23
|
+
action='store_true',
|
|
24
|
+
help="列出所有米家设备",
|
|
25
|
+
)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
'--list_homes',
|
|
28
|
+
action='store_true',
|
|
29
|
+
help="列出家庭列表",
|
|
30
|
+
)
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
'--list_scenes',
|
|
33
|
+
action='store_true',
|
|
34
|
+
help="列出场景列表",
|
|
35
|
+
)
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
'--list_consumable_items',
|
|
38
|
+
action='store_true',
|
|
39
|
+
help="列出耗材列表",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
'--run_scene',
|
|
43
|
+
type=str,
|
|
44
|
+
help="运行场景,指定场景ID或名称",
|
|
45
|
+
nargs='+',
|
|
46
|
+
metavar='SCENE_ID/SCENE_NAME',
|
|
47
|
+
)
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
'--get_device_info',
|
|
50
|
+
type=str,
|
|
51
|
+
help="获取设备信息,指定设备model,先使用 --list_devices 获取",
|
|
52
|
+
metavar='DEVICE_MODEL',
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
get = subparsers.add_parser(
|
|
56
|
+
'get',
|
|
57
|
+
help="获取设备属性",
|
|
58
|
+
)
|
|
59
|
+
get.set_defaults(func='get')
|
|
60
|
+
get.add_argument(
|
|
61
|
+
'-p', '--auth_path',
|
|
62
|
+
type=str,
|
|
63
|
+
default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
|
|
64
|
+
help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
|
|
65
|
+
)
|
|
66
|
+
get.add_argument(
|
|
67
|
+
'--dev_name',
|
|
68
|
+
type=str,
|
|
69
|
+
help="设备名称,指定为米家APP中设定的名称",
|
|
70
|
+
required=True,
|
|
71
|
+
)
|
|
72
|
+
get.add_argument(
|
|
73
|
+
'--prop_name',
|
|
74
|
+
type=str,
|
|
75
|
+
help="属性名称,先使用 --get_device_info 获取",
|
|
76
|
+
required=True,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
set = subparsers.add_parser(
|
|
80
|
+
'set',
|
|
81
|
+
help="设置设备属性",
|
|
82
|
+
)
|
|
83
|
+
set.set_defaults(func='set')
|
|
84
|
+
set.add_argument(
|
|
85
|
+
'-p', '--auth_path',
|
|
86
|
+
type=str,
|
|
87
|
+
default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
|
|
88
|
+
help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
|
|
89
|
+
)
|
|
90
|
+
set.add_argument(
|
|
91
|
+
'--dev_name',
|
|
92
|
+
type=str,
|
|
93
|
+
help="设备名称,指定为米家APP中设定的名称",
|
|
94
|
+
required=True,
|
|
95
|
+
)
|
|
96
|
+
set.add_argument(
|
|
97
|
+
'--prop_name',
|
|
98
|
+
type=str,
|
|
99
|
+
help="属性名称,先使用 --get_device_info 获取",
|
|
100
|
+
required=True,
|
|
101
|
+
)
|
|
102
|
+
set.add_argument(
|
|
103
|
+
'--value',
|
|
104
|
+
type=str,
|
|
105
|
+
help="需要设定的属性值",
|
|
106
|
+
required=True,
|
|
107
|
+
)
|
|
108
|
+
return parser.parse_args(args)
|
|
109
|
+
|
|
110
|
+
def init_api(auth_path: str) -> mijiaAPI:
|
|
111
|
+
if os.path.isdir(auth_path):
|
|
112
|
+
auth_path = os.path.join(auth_path, "mijia-api-auth.json")
|
|
113
|
+
if os.path.exists(auth_path):
|
|
114
|
+
try:
|
|
115
|
+
with open(auth_path, 'r') as f:
|
|
116
|
+
auth = json.load(f)
|
|
117
|
+
api = mijiaAPI(auth_data=auth)
|
|
118
|
+
if not api.available:
|
|
119
|
+
raise ValueError("Saved auth is no longer valid")
|
|
120
|
+
except (json.JSONDecodeError, ValueError):
|
|
121
|
+
api = mijiaLogin(save_path=auth_path)
|
|
122
|
+
auth = api.QRlogin()
|
|
123
|
+
api = mijiaAPI(auth_data=auth)
|
|
124
|
+
else:
|
|
125
|
+
api = mijiaLogin(save_path=auth_path)
|
|
126
|
+
auth = api.QRlogin()
|
|
127
|
+
api = mijiaAPI(auth_data=auth)
|
|
128
|
+
return api
|
|
129
|
+
|
|
130
|
+
def get_devices_list(api: mijiaAPI, verbose: bool = True) -> dict:
|
|
131
|
+
devices = api.get_devices_list()
|
|
132
|
+
if 'list' in devices:
|
|
133
|
+
devices = devices['list']
|
|
134
|
+
else:
|
|
135
|
+
devices = []
|
|
136
|
+
if verbose:
|
|
137
|
+
print("Devices:")
|
|
138
|
+
for device in devices:
|
|
139
|
+
print(f" - {device['name']}\n"
|
|
140
|
+
f" did: {device['did']}\n"
|
|
141
|
+
f" model: {device['model']}\n"
|
|
142
|
+
f" online: {device['isOnline']}")
|
|
143
|
+
device_mapping = {device['did']: device for device in devices}
|
|
144
|
+
return device_mapping
|
|
145
|
+
|
|
146
|
+
def get_homes_list(api: mijiaAPI, verbose: bool = True, device_mapping: Optional[dict] = None) -> dict:
|
|
147
|
+
if verbose:
|
|
148
|
+
if device_mapping is None:
|
|
149
|
+
device_mapping = get_devices_list(api, verbose=False)
|
|
150
|
+
homes = api.get_homes_list()
|
|
151
|
+
if 'homelist' in homes:
|
|
152
|
+
homes = homes['homelist']
|
|
153
|
+
else:
|
|
154
|
+
homes = []
|
|
155
|
+
if verbose:
|
|
156
|
+
print("Homes:")
|
|
157
|
+
for home in homes:
|
|
158
|
+
print(f" - {home['name']}\n"
|
|
159
|
+
f" id: {home['id']}\n"
|
|
160
|
+
f" address: {home['address']}\n"
|
|
161
|
+
f" room count: {len(home['roomlist'])}\n"
|
|
162
|
+
f" create time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(home['create_time']))}")
|
|
163
|
+
print(" Rooms:")
|
|
164
|
+
for room in home['roomlist']:
|
|
165
|
+
devices_name = []
|
|
166
|
+
if room['dids']:
|
|
167
|
+
for did in room['dids']:
|
|
168
|
+
if did in device_mapping:
|
|
169
|
+
devices_name.append(device_mapping[did]['name'])
|
|
170
|
+
else:
|
|
171
|
+
devices_name.append(did)
|
|
172
|
+
dids = ', '.join(devices_name)
|
|
173
|
+
print(f" - {room['name']}\n"
|
|
174
|
+
f" id: {room['id']}\n"
|
|
175
|
+
f" dids: {dids}\n"
|
|
176
|
+
f" create time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(room['create_time']))}")
|
|
177
|
+
home_mapping = {home['id']: home for home in homes}
|
|
178
|
+
return home_mapping
|
|
179
|
+
|
|
180
|
+
def get_scenes_list(api: mijiaAPI, verbose: bool = True, home_mapping: Optional[dict] = None) -> dict:
|
|
181
|
+
if home_mapping is None:
|
|
182
|
+
home_mapping = get_homes_list(api, verbose=False)
|
|
183
|
+
scene_mapping = {}
|
|
184
|
+
for home_id, home in home_mapping.items():
|
|
185
|
+
scenes = api.get_scenes_list(home_id)
|
|
186
|
+
if 'scene_info_list' in scenes:
|
|
187
|
+
scenes = scenes['scene_info_list']
|
|
188
|
+
else:
|
|
189
|
+
scenes = []
|
|
190
|
+
if scenes and verbose:
|
|
191
|
+
print(f"Scenes in {home['name']} ({home_id}):")
|
|
192
|
+
for scene in scenes:
|
|
193
|
+
print(f" - {scene['name']}\n"
|
|
194
|
+
f" id: {scene['scene_id']}\n"
|
|
195
|
+
f" create time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(scene['create_time'])))}\n"
|
|
196
|
+
f" update time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(scene['update_time'])))}")
|
|
197
|
+
scene_mapping.update({scene['scene_id']: scene for scene in scenes})
|
|
198
|
+
return scene_mapping
|
|
199
|
+
|
|
200
|
+
def get_consumable_items(api: mijiaAPI, home_mapping: Optional[dict] = None):
|
|
201
|
+
if home_mapping is None:
|
|
202
|
+
home_mapping = get_homes_list(api, verbose=False)
|
|
203
|
+
for home_id, home in home_mapping.items():
|
|
204
|
+
items = api.get_consumable_items(home_id)
|
|
205
|
+
if 'items' in items:
|
|
206
|
+
items = items['items'][0]['consumes_data']
|
|
207
|
+
else:
|
|
208
|
+
items = []
|
|
209
|
+
print(f"Consumable items in {home['name']} ({home_id}):")
|
|
210
|
+
for item in items:
|
|
211
|
+
print(f" - {item['details'][0]['description']} in {item['name']}({item['did']})\n"
|
|
212
|
+
f" value: {item['details'][0]['value']}")
|
|
213
|
+
|
|
214
|
+
def run_scene(api: mijiaAPI, scene_id: str, scene_mapping: Optional[dict] = None) -> bool:
|
|
215
|
+
if scene_mapping is None:
|
|
216
|
+
scene_mapping = get_scenes_list(api, verbose=False)
|
|
217
|
+
if scene_id not in scene_mapping:
|
|
218
|
+
scene_name_to_find = scene_id
|
|
219
|
+
found = False
|
|
220
|
+
for sid, scene in scene_mapping.items():
|
|
221
|
+
if scene['name'] == scene_name_to_find:
|
|
222
|
+
scene_id = sid
|
|
223
|
+
found = True
|
|
224
|
+
break
|
|
225
|
+
if not found:
|
|
226
|
+
print(f"Scene {scene_name_to_find} not found")
|
|
227
|
+
return False
|
|
228
|
+
scene_name = scene_mapping[scene_id]['name']
|
|
229
|
+
ret = api.run_scene(scene_id)
|
|
230
|
+
if ret:
|
|
231
|
+
print(f"Scene {scene_name}({scene_id}) run successfully")
|
|
232
|
+
return True
|
|
233
|
+
else:
|
|
234
|
+
print(f"Failed to run scene {scene_name}({scene_id})")
|
|
235
|
+
return False
|
|
236
|
+
|
|
237
|
+
def get(args):
|
|
238
|
+
api = init_api(args.auth_path)
|
|
239
|
+
device = mijiaDevices(api, dev_name=args.dev_name)
|
|
240
|
+
value = device.get(args.prop_name)
|
|
241
|
+
unit = device.prop_list[args.prop_name].unit
|
|
242
|
+
print(f"The {args.prop_name} of {args.dev_name} is {value} {unit if unit else ''}")
|
|
243
|
+
|
|
244
|
+
def set(args):
|
|
245
|
+
api = init_api(args.auth_path)
|
|
246
|
+
device = mijiaDevices(api, dev_name=args.dev_name)
|
|
247
|
+
ret = device.set_v2(args.prop_name, args.value)
|
|
248
|
+
unit = device.prop_list[args.prop_name].unit
|
|
249
|
+
if ret:
|
|
250
|
+
print(f"The {args.prop_name} of {args.dev_name} is set to {args.value} {unit if unit else ''}")
|
|
251
|
+
else:
|
|
252
|
+
print(f"Failed to set the {args.prop_name} of {args.dev_name} to {args.value}")
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def main(args):
|
|
256
|
+
args = parse_args(args)
|
|
257
|
+
api = init_api(args.auth_path)
|
|
258
|
+
device_mapping = None
|
|
259
|
+
home_mapping = None
|
|
260
|
+
scenes_mapping = None
|
|
261
|
+
|
|
262
|
+
if args.list_devices:
|
|
263
|
+
device_mapping = get_devices_list(api)
|
|
264
|
+
if args.list_homes:
|
|
265
|
+
home_mapping = get_homes_list(api, device_mapping=device_mapping)
|
|
266
|
+
if args.list_scenes:
|
|
267
|
+
scenes_mapping = get_scenes_list(api, home_mapping=home_mapping)
|
|
268
|
+
if args.list_consumable_items:
|
|
269
|
+
get_consumable_items(api, home_mapping=home_mapping)
|
|
270
|
+
if args.run_scene:
|
|
271
|
+
for scene_id in args.run_scene:
|
|
272
|
+
run_scene(api, scene_id, scene_mapping=scenes_mapping)
|
|
273
|
+
if args.get_device_info:
|
|
274
|
+
device_info = get_device_info(args.get_device_info)
|
|
275
|
+
print(json.dumps(device_info, indent=2, ensure_ascii=False))
|
|
276
|
+
if hasattr(args, 'func') and args.func is not None:
|
|
277
|
+
if args.func == 'get':
|
|
278
|
+
get(args)
|
|
279
|
+
if args.func == 'set':
|
|
280
|
+
set(args)
|
|
281
|
+
|
|
282
|
+
def cli():
|
|
283
|
+
main(sys.argv[1:])
|
|
284
|
+
|
|
285
|
+
if __name__ == "__main__":
|
|
286
|
+
main(sys.argv[1:])
|
|
@@ -23,6 +23,7 @@ class mijiaAPI(object):
|
|
|
23
23
|
self.userId = auth_data['userId']
|
|
24
24
|
self.ssecurity = auth_data['ssecurity']
|
|
25
25
|
self.session = requests.Session()
|
|
26
|
+
self.expireTime = auth_data.get('expireTime', None)
|
|
26
27
|
self.session.headers.update({
|
|
27
28
|
'User-Agent': defaultUA,
|
|
28
29
|
'x-xiaomi-protocal-flag-cli': 'PROTOCAL-HTTP2',
|
|
@@ -45,8 +46,8 @@ class mijiaAPI(object):
|
|
|
45
46
|
Returns:
|
|
46
47
|
bool: API可用返回True,否则返回False。
|
|
47
48
|
"""
|
|
48
|
-
if
|
|
49
|
-
expire_time = datetime.strptime(self.
|
|
49
|
+
if self.expireTime:
|
|
50
|
+
expire_time = datetime.strptime(self.expireTime, '%Y-%m-%d %H:%M:%S')
|
|
50
51
|
if expire_time < datetime.now():
|
|
51
52
|
return False
|
|
52
53
|
return True
|
|
@@ -174,7 +174,23 @@ class mijiaDevices(object):
|
|
|
174
174
|
if value not in [item['value'] for item in prop.value_list]:
|
|
175
175
|
raise ValueError(f'Invalid value: {value}, should be in {prop.value_list}')
|
|
176
176
|
if prop.type == 'bool':
|
|
177
|
-
if
|
|
177
|
+
if isinstance(value, str):
|
|
178
|
+
if value.lower() == 'true':
|
|
179
|
+
value = True
|
|
180
|
+
elif value.lower() == 'false':
|
|
181
|
+
value = False
|
|
182
|
+
elif value in ['0', '1']:
|
|
183
|
+
value = bool(int(value))
|
|
184
|
+
else:
|
|
185
|
+
raise ValueError(f'Invalid value for bool: {value}, should be True or False')
|
|
186
|
+
elif isinstance(value, int):
|
|
187
|
+
if value == 0:
|
|
188
|
+
value = False
|
|
189
|
+
elif value == 1:
|
|
190
|
+
value = True
|
|
191
|
+
else:
|
|
192
|
+
raise ValueError(f'Invalid value for bool: {value}, should be True or False')
|
|
193
|
+
elif not isinstance(value, bool):
|
|
178
194
|
raise ValueError(f'Invalid value for bool: {value}, should be True or False')
|
|
179
195
|
elif prop.type in ['int', 'uint']:
|
|
180
196
|
value = int(value)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "mijiaAPI"
|
|
3
|
-
version = "1.
|
|
3
|
+
version = "1.4.0"
|
|
4
4
|
description = "A Python API for Xiaomi Mijia"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.9,<4.0"
|
|
@@ -10,9 +10,12 @@ dependencies = [
|
|
|
10
10
|
"requests>=2.32.3,<3.0.0",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
|
+
[project.scripts]
|
|
14
|
+
mijiaAPI = "mijiaAPI.__main__:cli"
|
|
15
|
+
|
|
13
16
|
[tool.poetry]
|
|
14
17
|
name = "mijiaAPI"
|
|
15
|
-
version = "1.
|
|
18
|
+
version = "1.4.0"
|
|
16
19
|
description = "A Python API for Xiaomi Mijia"
|
|
17
20
|
authors = ["Do1e <dpj.email@qq.com>"]
|
|
18
21
|
license = "GPLv3"
|
|
@@ -34,6 +37,9 @@ requests = "^2.32.3"
|
|
|
34
37
|
qrcode = "^8.0"
|
|
35
38
|
pillow = "^11.0.0"
|
|
36
39
|
|
|
40
|
+
[tool.poetry.scripts]
|
|
41
|
+
mijiaAPI = "mijiaAPI.__main__:cli"
|
|
42
|
+
|
|
37
43
|
[tool.poetry-dynamic-versioning]
|
|
38
44
|
enable = false
|
|
39
45
|
|
mijiaapi-1.3.13/PKG-INFO
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: mijiaAPI
|
|
3
|
-
Version: 1.3.13
|
|
4
|
-
Summary: A Python API for Xiaomi Mijia
|
|
5
|
-
License: GPLv3
|
|
6
|
-
Author: Do1e
|
|
7
|
-
Author-email: dpj.email@qq.com
|
|
8
|
-
Requires-Python: >=3.9,<4.0
|
|
9
|
-
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
|
|
10
|
-
Classifier: License :: Other/Proprietary License
|
|
11
|
-
Classifier: Operating System :: OS Independent
|
|
12
|
-
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
-
Requires-Dist: pillow (>=11.0.0,<12.0.0)
|
|
19
|
-
Requires-Dist: qrcode (>=8.0,<9.0)
|
|
20
|
-
Requires-Dist: requests (>=2.32.3,<3.0.0)
|
|
21
|
-
Project-URL: Homepage, https://github.com/Do1e/mijia-api
|
|
22
|
-
Project-URL: Repository, https://github.com/Do1e/mijia-api
|
|
23
|
-
Description-Content-Type: text/markdown
|
|
24
|
-
|
|
25
|
-
# mijiaAPI
|
|
26
|
-
小米米家设备的api,可以使用代码直接控制米家设备的功能,[Github link](https://github.com/Do1e/mijia-api),[PyPI link](https://pypi.org/project/mijiaAPI/)。
|
|
27
|
-
|
|
28
|
-
## 安装
|
|
29
|
-
```bash
|
|
30
|
-
poetry install
|
|
31
|
-
```
|
|
32
|
-
或者
|
|
33
|
-
```bash
|
|
34
|
-
pip install mijiaAPI
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## 使用
|
|
38
|
-
使用实例可以参考`demos`文件夹下的示例代码,以下是简单的使用说明
|
|
39
|
-
|
|
40
|
-
### 登录
|
|
41
|
-
|
|
42
|
-
`mijiaLogin`:登录小米账号,获取控制设备必须的`userId`, `ssecurity`, `deviceId`, `serviceToken`,方法列表:
|
|
43
|
-
* `login(username: str, password: str) -> dict`:账号密码登录,返回上述信息。**注意,目前这一方法大概率遇到手机验证码,请尽可能使用`QRlogin`。**
|
|
44
|
-
* `QRlogin() -> dict`:扫描二维码登录,返回上述信息(会在支持tty的终端打印二维码,若打印识别可查看当前文件夹下的`qr.png`)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
### API
|
|
48
|
-
|
|
49
|
-
`mijiaAPI`:API的实现,使用`mijiaLogin`登录后返回的信息进行初始化
|
|
50
|
-
* `__init__(auth_data: dict)`:初始化
|
|
51
|
-
* `available -> bool`:传入的`auth_data`是否有效
|
|
52
|
-
* `get_devices_list() -> list`:获取设备列表
|
|
53
|
-
* `get_homes_list() -> list`:获取家庭列表,家庭字典中包含房间列表
|
|
54
|
-
* `get_scenes_list(home_id: str) -> list`:获取手动场景列表,在 *米家->添加->手动控制* 中设置
|
|
55
|
-
* `run_scene(scene_id: str) -> bool`:运行手动场景
|
|
56
|
-
* `get_consumable_items(home_id: str) -> list`:获取设备的耗材信息
|
|
57
|
-
* `get_devices_prop(data: list) -> list`:获取设备的属性
|
|
58
|
-
* `set_devices_prop(data: list) -> list`:设置设备的属性
|
|
59
|
-
* `data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)。
|
|
60
|
-
* 网站上的方法并非全部可用,需要自行测试
|
|
61
|
-
* `run_action(data: dict) -> dict`:执行设备的action
|
|
62
|
-
* `data`为一个字典,需要包含`did`, `siid`, `aiid`,获取方法同上
|
|
63
|
-
|
|
64
|
-
### 针对设备的封装
|
|
65
|
-
|
|
66
|
-
最简单的使用方法是直接使用自然语言调用小爱音箱,参见[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
67
|
-
|
|
68
|
-
`mijiaDevices`:使用`mijiaAPI`和设备属性字典初始化,以便更方便地调用设备属性
|
|
69
|
-
* `__init__(api: mijiaAPI, dev_info: dict. did: str = None, sleep_time: float = 0.5)`:初始化,`dev_info`为设备属性,参考[demos/dev_info_example](demos/dev_info_example),`sleep_time`为每次调用设备属性的间隔时间(注:设置属性后立刻获取属性会不符合预期,需要延迟一段时间)
|
|
70
|
-
* `set(name: str, did: str, value: Union[bool, int]) -> Union[bool, int]`:设置设备属性
|
|
71
|
-
* `get(name: str, did: str) -> Union[bool, int]`:获取设备属性
|
|
72
|
-
* v1.2.0 新增直接通过名称设置/获取属性,需要在初始化时传入`did`,详见[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)。名称中包含`-`的属性需要替换为`_`。
|
|
73
|
-
* 可以调用`get_device_info(device_model: str) -> dict`函数从[米家设备列表](https://home.miot-spec.com/)在线获取设备属性字典,详见[demos/test_get_device_info.py](demos/test_get_device_info.py)
|
|
74
|
-
|
|
75
|
-
## 致谢
|
|
76
|
-
* [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
|
|
77
|
-
|
|
78
|
-
## 声明
|
|
79
|
-
* 本项目仅供学习交流使用,不得用于商业用途,如有侵权请联系删除。
|
|
80
|
-
* 本项目作者不对使用本项目产生的任何后果负责,请用户自行承担使用本项目的风险。
|
|
81
|
-
|
mijiaapi-1.3.13/README.md
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
# mijiaAPI
|
|
2
|
-
小米米家设备的api,可以使用代码直接控制米家设备的功能,[Github link](https://github.com/Do1e/mijia-api),[PyPI link](https://pypi.org/project/mijiaAPI/)。
|
|
3
|
-
|
|
4
|
-
## 安装
|
|
5
|
-
```bash
|
|
6
|
-
poetry install
|
|
7
|
-
```
|
|
8
|
-
或者
|
|
9
|
-
```bash
|
|
10
|
-
pip install mijiaAPI
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## 使用
|
|
14
|
-
使用实例可以参考`demos`文件夹下的示例代码,以下是简单的使用说明
|
|
15
|
-
|
|
16
|
-
### 登录
|
|
17
|
-
|
|
18
|
-
`mijiaLogin`:登录小米账号,获取控制设备必须的`userId`, `ssecurity`, `deviceId`, `serviceToken`,方法列表:
|
|
19
|
-
* `login(username: str, password: str) -> dict`:账号密码登录,返回上述信息。**注意,目前这一方法大概率遇到手机验证码,请尽可能使用`QRlogin`。**
|
|
20
|
-
* `QRlogin() -> dict`:扫描二维码登录,返回上述信息(会在支持tty的终端打印二维码,若打印识别可查看当前文件夹下的`qr.png`)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
### API
|
|
24
|
-
|
|
25
|
-
`mijiaAPI`:API的实现,使用`mijiaLogin`登录后返回的信息进行初始化
|
|
26
|
-
* `__init__(auth_data: dict)`:初始化
|
|
27
|
-
* `available -> bool`:传入的`auth_data`是否有效
|
|
28
|
-
* `get_devices_list() -> list`:获取设备列表
|
|
29
|
-
* `get_homes_list() -> list`:获取家庭列表,家庭字典中包含房间列表
|
|
30
|
-
* `get_scenes_list(home_id: str) -> list`:获取手动场景列表,在 *米家->添加->手动控制* 中设置
|
|
31
|
-
* `run_scene(scene_id: str) -> bool`:运行手动场景
|
|
32
|
-
* `get_consumable_items(home_id: str) -> list`:获取设备的耗材信息
|
|
33
|
-
* `get_devices_prop(data: list) -> list`:获取设备的属性
|
|
34
|
-
* `set_devices_prop(data: list) -> list`:设置设备的属性
|
|
35
|
-
* `data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)。
|
|
36
|
-
* 网站上的方法并非全部可用,需要自行测试
|
|
37
|
-
* `run_action(data: dict) -> dict`:执行设备的action
|
|
38
|
-
* `data`为一个字典,需要包含`did`, `siid`, `aiid`,获取方法同上
|
|
39
|
-
|
|
40
|
-
### 针对设备的封装
|
|
41
|
-
|
|
42
|
-
最简单的使用方法是直接使用自然语言调用小爱音箱,参见[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
43
|
-
|
|
44
|
-
`mijiaDevices`:使用`mijiaAPI`和设备属性字典初始化,以便更方便地调用设备属性
|
|
45
|
-
* `__init__(api: mijiaAPI, dev_info: dict. did: str = None, sleep_time: float = 0.5)`:初始化,`dev_info`为设备属性,参考[demos/dev_info_example](demos/dev_info_example),`sleep_time`为每次调用设备属性的间隔时间(注:设置属性后立刻获取属性会不符合预期,需要延迟一段时间)
|
|
46
|
-
* `set(name: str, did: str, value: Union[bool, int]) -> Union[bool, int]`:设置设备属性
|
|
47
|
-
* `get(name: str, did: str) -> Union[bool, int]`:获取设备属性
|
|
48
|
-
* v1.2.0 新增直接通过名称设置/获取属性,需要在初始化时传入`did`,详见[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)。名称中包含`-`的属性需要替换为`_`。
|
|
49
|
-
* 可以调用`get_device_info(device_model: str) -> dict`函数从[米家设备列表](https://home.miot-spec.com/)在线获取设备属性字典,详见[demos/test_get_device_info.py](demos/test_get_device_info.py)
|
|
50
|
-
|
|
51
|
-
## 致谢
|
|
52
|
-
* [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
|
|
53
|
-
|
|
54
|
-
## 声明
|
|
55
|
-
* 本项目仅供学习交流使用,不得用于商业用途,如有侵权请联系删除。
|
|
56
|
-
* 本项目作者不对使用本项目产生的任何后果负责,请用户自行承担使用本项目的风险。
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|