mijiaAPI 1.3.14__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.3.14 → mijiaapi-1.4.0}/PKG-INFO +76 -10
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/README.md +75 -9
- mijiaapi-1.4.0/mijiaAPI/__main__.py +286 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/devices.py +17 -1
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/pyproject.toml +8 -2
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/LICENSE +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/__init__.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/apis.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/code.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/logger.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/login.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/urls.py +0 -0
- {mijiaapi-1.3.14 → mijiaapi-1.4.0}/mijiaAPI/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: mijiaAPI
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4.0
|
|
4
4
|
Summary: A Python API for Xiaomi Mijia
|
|
5
5
|
License: GPLv3
|
|
6
6
|
Author: Do1e
|
|
@@ -100,6 +100,19 @@ poetry install
|
|
|
100
100
|
|
|
101
101
|
**注意**:并非所有米家产品库中列出的方法都可用,需要自行测试验证。
|
|
102
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
|
+
|
|
103
116
|
### 设备控制封装
|
|
104
117
|
|
|
105
118
|
`mijiaDevices`:基于 `mijiaAPI` 的高级封装,提供更简便的设备控制方式。
|
|
@@ -112,8 +125,14 @@ mijiaDevices(api: mijiaAPI, dev_info: dict = None, dev_name: str = None, did: st
|
|
|
112
125
|
|
|
113
126
|
* `api`:已初始化的 `mijiaAPI` 对象
|
|
114
127
|
* `dev_info`:设备属性字典(可选)
|
|
128
|
+
- 可通过 `get_device_info()` 函数获取
|
|
129
|
+
- **注意**:如果提供了 `dev_info`,则不需要提供 `dev_name`
|
|
115
130
|
* `dev_name`:设备名称,用于自动查找设备(可选)
|
|
131
|
+
- 例如:`dev_name='台灯'`,会自动查找名称包含“台灯”的设备
|
|
132
|
+
- **注意**:如果提供了 `dev_name`,则不需要提供 `dev_info` 和 `did`
|
|
116
133
|
* `did`:设备ID,便于直接通过属性名访问(可选)
|
|
134
|
+
- 如果初始化时未提供,无法使用属性样式访问,需要使用 `get()` 和 `set()` 方法指定 `did`
|
|
135
|
+
- 使用 `dev_name` 初始化时,`did` 会自动获取
|
|
117
136
|
* `sleep_time`:属性操作间隔时间,单位秒(默认0.5秒)
|
|
118
137
|
- **重要**:设置属性后立即获取可能不符合预期,需设置适当延迟
|
|
119
138
|
|
|
@@ -142,19 +161,66 @@ current_temp = device.color_temperature # 获取色温
|
|
|
142
161
|
* 使用自然语言让小爱音箱执行:[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
143
162
|
* 通过属性直接控制台灯:[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)
|
|
144
163
|
|
|
145
|
-
###
|
|
164
|
+
### Mijia API CLI
|
|
165
|
+
`mijiaAPI` 还提供了一个命令行工具,可以直接在终端中使用。
|
|
146
166
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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 获取
|
|
154
194
|
```
|
|
155
195
|
|
|
156
|
-
|
|
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
|
+
```
|
|
157
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
|
+
```
|
|
158
224
|
|
|
159
225
|
## 致谢
|
|
160
226
|
|
|
@@ -76,6 +76,19 @@ poetry install
|
|
|
76
76
|
|
|
77
77
|
**注意**:并非所有米家产品库中列出的方法都可用,需要自行测试验证。
|
|
78
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
|
+
|
|
79
92
|
### 设备控制封装
|
|
80
93
|
|
|
81
94
|
`mijiaDevices`:基于 `mijiaAPI` 的高级封装,提供更简便的设备控制方式。
|
|
@@ -88,8 +101,14 @@ mijiaDevices(api: mijiaAPI, dev_info: dict = None, dev_name: str = None, did: st
|
|
|
88
101
|
|
|
89
102
|
* `api`:已初始化的 `mijiaAPI` 对象
|
|
90
103
|
* `dev_info`:设备属性字典(可选)
|
|
104
|
+
- 可通过 `get_device_info()` 函数获取
|
|
105
|
+
- **注意**:如果提供了 `dev_info`,则不需要提供 `dev_name`
|
|
91
106
|
* `dev_name`:设备名称,用于自动查找设备(可选)
|
|
107
|
+
- 例如:`dev_name='台灯'`,会自动查找名称包含“台灯”的设备
|
|
108
|
+
- **注意**:如果提供了 `dev_name`,则不需要提供 `dev_info` 和 `did`
|
|
92
109
|
* `did`:设备ID,便于直接通过属性名访问(可选)
|
|
110
|
+
- 如果初始化时未提供,无法使用属性样式访问,需要使用 `get()` 和 `set()` 方法指定 `did`
|
|
111
|
+
- 使用 `dev_name` 初始化时,`did` 会自动获取
|
|
93
112
|
* `sleep_time`:属性操作间隔时间,单位秒(默认0.5秒)
|
|
94
113
|
- **重要**:设置属性后立即获取可能不符合预期,需设置适当延迟
|
|
95
114
|
|
|
@@ -118,19 +137,66 @@ current_temp = device.color_temperature # 获取色温
|
|
|
118
137
|
* 使用自然语言让小爱音箱执行:[demos/test_devices_wifispeaker.py](demos/test_devices_wifispeaker.py)
|
|
119
138
|
* 通过属性直接控制台灯:[demos/test_devices_v2_light.py](demos/test_devices_v2_light.py)
|
|
120
139
|
|
|
121
|
-
###
|
|
140
|
+
### Mijia API CLI
|
|
141
|
+
`mijiaAPI` 还提供了一个命令行工具,可以直接在终端中使用。
|
|
122
142
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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 获取
|
|
130
170
|
```
|
|
131
171
|
|
|
132
|
-
|
|
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
|
+
```
|
|
133
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
|
+
```
|
|
134
200
|
|
|
135
201
|
## 致谢
|
|
136
202
|
|
|
@@ -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:])
|
|
@@ -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
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|