mijiaAPI 1.4.0__tar.gz → 1.4.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: mijiaAPI
3
- Version: 1.4.0
3
+ Version: 1.4.2
4
4
  Summary: A Python API for Xiaomi Mijia
5
5
  License: GPLv3
6
6
  Author: Do1e
@@ -167,9 +167,10 @@ current_temp = device.color_temperature # 获取色温
167
167
  ```
168
168
  > python -m mijiaAPI --help
169
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} ...
170
+ usage: mijiaAPI [-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] [--run PROMPT]
172
+ [--wifispeaker_name WIFISPEAKER_NAME] [--quiet]
173
+ {get,set} ...
173
174
 
174
175
  Mijia API CLI
175
176
 
@@ -191,6 +192,10 @@ options:
191
192
  运行场景,指定场景ID或名称
192
193
  --get_device_info DEVICE_MODEL
193
194
  获取设备信息,指定设备model,先使用 --list_devices 获取
195
+ --run PROMPT 使用自然语言描述你的需求,如果你有小爱音箱的话
196
+ --wifispeaker_name WIFISPEAKER_NAME
197
+ 指定小爱音箱名称,默认是获取到的第一个小爱音箱
198
+ --quiet 小爱音箱静默执行
194
199
  ```
195
200
 
196
201
  ```
@@ -222,6 +227,27 @@ options:
222
227
  --value VALUE 需要设定的属性值
223
228
  ```
224
229
 
230
+ 或者直接使用`uvx`忽略安装步骤:
231
+
232
+ ```bash
233
+ uvx mijiaAPI --help
234
+ ```
235
+
236
+ #### 示例:
237
+
238
+ ```bash
239
+ mijiaAPI -l # 列出所有米家设备
240
+ mijiaAPI --list_homes # 列出家庭列表
241
+ mijiaAPI --list_scenes # 列出场景列表
242
+ mijiaAPI --list_consumable_items # 列出耗材列表
243
+ mijiaAPI --run_scene SCENE_ID/SCENE_NAME # 运行场景,指定场景ID或名称
244
+ mijiaAPI --get_device_info DEVICE_MODEL # 获取设备信息,指定设备model,先使用 --list_devices 获取
245
+ mijiaAPI get --dev_name DEV_NAME --prop_name PROP_NAME # 获取设备属性
246
+ mijiaAPI set --dev_name DEV_NAME --prop_name PROP_NAME --value VALUE # 设置设备属性
247
+ mijiaAPI --run 明天天气如何
248
+ mijiaAPI --run 打开台灯并将亮度调至最大 --quiet
249
+ ```
250
+
225
251
  ## 致谢
226
252
 
227
253
  * [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
@@ -143,9 +143,10 @@ current_temp = device.color_temperature # 获取色温
143
143
  ```
144
144
  > python -m mijiaAPI --help
145
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} ...
146
+ usage: mijiaAPI [-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] [--run PROMPT]
148
+ [--wifispeaker_name WIFISPEAKER_NAME] [--quiet]
149
+ {get,set} ...
149
150
 
150
151
  Mijia API CLI
151
152
 
@@ -167,6 +168,10 @@ options:
167
168
  运行场景,指定场景ID或名称
168
169
  --get_device_info DEVICE_MODEL
169
170
  获取设备信息,指定设备model,先使用 --list_devices 获取
171
+ --run PROMPT 使用自然语言描述你的需求,如果你有小爱音箱的话
172
+ --wifispeaker_name WIFISPEAKER_NAME
173
+ 指定小爱音箱名称,默认是获取到的第一个小爱音箱
174
+ --quiet 小爱音箱静默执行
170
175
  ```
171
176
 
172
177
  ```
@@ -198,6 +203,27 @@ options:
198
203
  --value VALUE 需要设定的属性值
199
204
  ```
200
205
 
206
+ 或者直接使用`uvx`忽略安装步骤:
207
+
208
+ ```bash
209
+ uvx mijiaAPI --help
210
+ ```
211
+
212
+ #### 示例:
213
+
214
+ ```bash
215
+ mijiaAPI -l # 列出所有米家设备
216
+ mijiaAPI --list_homes # 列出家庭列表
217
+ mijiaAPI --list_scenes # 列出场景列表
218
+ mijiaAPI --list_consumable_items # 列出耗材列表
219
+ mijiaAPI --run_scene SCENE_ID/SCENE_NAME # 运行场景,指定场景ID或名称
220
+ mijiaAPI --get_device_info DEVICE_MODEL # 获取设备信息,指定设备model,先使用 --list_devices 获取
221
+ mijiaAPI get --dev_name DEV_NAME --prop_name PROP_NAME # 获取设备属性
222
+ mijiaAPI set --dev_name DEV_NAME --prop_name PROP_NAME --value VALUE # 设置设备属性
223
+ mijiaAPI --run 明天天气如何
224
+ mijiaAPI --run 打开台灯并将亮度调至最大 --quiet
225
+ ```
226
+
201
227
  ## 致谢
202
228
 
203
229
  * [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
@@ -15,8 +15,8 @@ def parse_args(args):
15
15
  parser.add_argument(
16
16
  '-p', '--auth_path',
17
17
  type=str,
18
- default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
19
- help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
18
+ default=os.path.join(os.path.expanduser("~"), ".config/mijia-api", "mijia-api-auth.json"),
19
+ help="认证文件保存路径,默认保存在~/.config/mijia-api/mijia-api-auth.json",
20
20
  )
21
21
  parser.add_argument(
22
22
  '-l', '--list_devices',
@@ -51,6 +51,23 @@ def parse_args(args):
51
51
  help="获取设备信息,指定设备model,先使用 --list_devices 获取",
52
52
  metavar='DEVICE_MODEL',
53
53
  )
54
+ parser.add_argument(
55
+ '--run',
56
+ type=str,
57
+ help="使用自然语言描述你的需求,如果你有小爱音箱的话",
58
+ metavar='PROMPT',
59
+ )
60
+ parser.add_argument(
61
+ '--wifispeaker_name',
62
+ type=str,
63
+ help="指定小爱音箱名称,默认是获取到的第一个小爱音箱",
64
+ default=None,
65
+ )
66
+ parser.add_argument(
67
+ '--quiet',
68
+ action='store_true',
69
+ help="小爱音箱静默执行",
70
+ )
54
71
 
55
72
  get = subparsers.add_parser(
56
73
  'get',
@@ -60,8 +77,8 @@ def parse_args(args):
60
77
  get.add_argument(
61
78
  '-p', '--auth_path',
62
79
  type=str,
63
- default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
64
- help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
80
+ default=os.path.join(os.path.expanduser("~"), ".config/mijia-api", "mijia-api-auth.json"),
81
+ help="认证文件保存路径,默认保存在~/.config/mijia-api/mijia-api-auth.json",
65
82
  )
66
83
  get.add_argument(
67
84
  '--dev_name',
@@ -84,8 +101,8 @@ def parse_args(args):
84
101
  set.add_argument(
85
102
  '-p', '--auth_path',
86
103
  type=str,
87
- default=os.path.join(os.path.expanduser("~"), ".config", "mijia-api-auth.json"),
88
- help="认证文件保存路径,默认保存在~/.config/mijia-api-auth.json",
104
+ default=os.path.join(os.path.expanduser("~"), ".config/mijia-api", "mijia-api-auth.json"),
105
+ help="认证文件保存路径,默认保存在~/.config/mijia-api/mijia-api-auth.json",
89
106
  )
90
107
  set.add_argument(
91
108
  '--dev_name',
@@ -273,6 +290,20 @@ def main(args):
273
290
  if args.get_device_info:
274
291
  device_info = get_device_info(args.get_device_info)
275
292
  print(json.dumps(device_info, indent=2, ensure_ascii=False))
293
+ if args.run:
294
+ if device_mapping is None:
295
+ device_mapping = get_devices_list(api, verbose=False)
296
+ if args.wifispeaker_name is None:
297
+ wifispeaker = None
298
+ for device in device_mapping.values():
299
+ if 'xiaomi.wifispeaker' in device['model']:
300
+ wifispeaker = mijiaDevices(api, dev_name=device['name'])
301
+ break
302
+ if wifispeaker is None:
303
+ raise ValueError("No wifispeaker found")
304
+ else:
305
+ wifispeaker = mijiaDevices(api, dev_name=args.wifispeaker_name)
306
+ wifispeaker.run_action('execute-text-directive', _in=[args.run, args.quiet])
276
307
  if hasattr(args, 'func') and args.func is not None:
277
308
  if args.func == 'get':
278
309
  get(args)
@@ -1,5 +1,6 @@
1
1
  from typing import Union, Optional
2
2
  import json
3
+ import os
3
4
  import re
4
5
  import requests
5
6
  from time import sleep
@@ -370,12 +371,13 @@ class mijiaDevices(object):
370
371
  return result['code'] == 0
371
372
 
372
373
 
373
- def get_device_info(device_model: str) -> dict:
374
+ def get_device_info(device_model: str, cache_path: Optional[str] = os.path.join(os.path.expanduser("~"), ".config/mijia-api")) -> dict:
374
375
  """
375
376
  获取设备信息,用于初始化mijiaDevices对象。
376
377
 
377
378
  Args:
378
379
  device_model (str): 设备型号,从get_devices_list获取。
380
+ cache_path (str, optional): 缓存文件路径。默认为~/.config/mijia-api,设置为None则不使用缓存。
379
381
 
380
382
  Returns:
381
383
  dict: 设备信息字典。
@@ -383,6 +385,11 @@ def get_device_info(device_model: str) -> dict:
383
385
  Raises:
384
386
  RuntimeError: 如果获取设备信息失败。
385
387
  """
388
+ if cache_path is not None:
389
+ cache_file = os.path.join(cache_path, f'{device_model}.json')
390
+ if os.path.exists(cache_file):
391
+ with open(cache_file, 'r', encoding='utf-8') as f:
392
+ return json.load(f)
386
393
  response = requests.get(deviceURL + device_model)
387
394
  if response.status_code != 200:
388
395
  raise RuntimeError(f'Failed to get device info')
@@ -446,4 +453,9 @@ def get_device_info(device_model: str) -> dict:
446
453
  'aiid': int(aiid)
447
454
  }
448
455
  })
456
+ if cache_path is not None:
457
+ cache_file = os.path.join(cache_path, f'{device_model}.json')
458
+ os.makedirs(cache_path, exist_ok=True)
459
+ with open(cache_file, 'w', encoding='utf-8') as f:
460
+ json.dump(result, f, indent=2, ensure_ascii=False)
449
461
  return result
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mijiaAPI"
3
- version = "1.4.0"
3
+ version = "1.4.2"
4
4
  description = "A Python API for Xiaomi Mijia"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.9,<4.0"
@@ -15,7 +15,7 @@ mijiaAPI = "mijiaAPI.__main__:cli"
15
15
 
16
16
  [tool.poetry]
17
17
  name = "mijiaAPI"
18
- version = "1.4.0"
18
+ version = "1.4.2"
19
19
  description = "A Python API for Xiaomi Mijia"
20
20
  authors = ["Do1e <dpj.email@qq.com>"]
21
21
  license = "GPLv3"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes