mijiaAPI 4.2.1__tar.gz → 4.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mijiaAPI
3
- Version: 4.2.1
3
+ Version: 4.3.0
4
4
  Summary: A Python API for Xiaomi Mijia
5
5
  Author-email: Do1e <i@do1e.cn>
6
6
  License-Expression: GPL-3.0-or-later
@@ -112,8 +112,33 @@ def parse_args(args):
112
112
  )
113
113
  run.add_argument(
114
114
  '--quiet',
115
- action='store_true',
116
- help="小爱音箱静默执行",
115
+ action=argparse.BooleanOptionalAction,
116
+ default=True,
117
+ help="小爱音箱静默执行(默认开启;使用 --no-quiet 播报回复)",
118
+ )
119
+
120
+ play = subparsers.add_parser(
121
+ 'play',
122
+ help="通过小爱音箱播放指定文本",
123
+ )
124
+ play.set_defaults(func='play')
125
+ play.add_argument(
126
+ '-p', '--auth_path',
127
+ type=Path,
128
+ default=Path.home() / ".config" / "mijia-api" / "auth.json",
129
+ help="认证文件保存路径,默认保存在 ~/.config/mijia-api/auth.json",
130
+ )
131
+ play.add_argument(
132
+ 'text',
133
+ type=str,
134
+ help="要通过小爱音箱播放的文本",
135
+ metavar='TEXT',
136
+ )
137
+ play.add_argument(
138
+ '--wifispeaker_name',
139
+ type=str,
140
+ help="指定小爱音箱名称,默认是获取到的第一个小爱音箱",
141
+ default=None,
117
142
  )
118
143
 
119
144
  mcp_cmd = subparsers.add_parser(
@@ -507,7 +532,7 @@ def main(args):
507
532
  run_action(api, args)
508
533
  if args.func == 'statistics':
509
534
  get_statistics(api, args)
510
- if args.func == 'run':
535
+ if args.func in ('run', 'play'):
511
536
  if device_mapping is None:
512
537
  device_mapping = get_devices_list(api, verbose=False)
513
538
  if args.wifispeaker_name is None:
@@ -520,7 +545,10 @@ def main(args):
520
545
  raise ValueError("未找到小爱音箱设备")
521
546
  else:
522
547
  wifispeaker = mijiaDevice(api, dev_name=args.wifispeaker_name)
523
- wifispeaker.run_action('execute-text-directive', _in=[args.prompt, 1 if args.quiet else 0])
548
+ if args.func == 'run':
549
+ wifispeaker.run_action('execute-text-directive', _in=[args.prompt, 1 if args.quiet else 0])
550
+ else:
551
+ wifispeaker.run_action('play-text', _in=[args.text])
524
552
 
525
553
  def cli():
526
554
  main(sys.argv[1:])
@@ -1,4 +1,4 @@
1
- # https://github.com/kekeandzeyu/ha_xiaomi_home/blob/main/custom_components/xiaomi_home/miot/i18n/zh-Hans.json
1
+ # https://github.com/XiaoMi/ha_xiaomi_home/blob/main/custom_components/xiaomi_home/miot/i18n/zh-Hans.json
2
2
  ERROR_CODE = {
3
3
  "-10000": "未知错误",
4
4
  "-10001": "服务不可用",
@@ -305,42 +305,54 @@ def get_statistics(
305
305
  return json.dumps(ret, ensure_ascii=False)
306
306
 
307
307
 
308
+ def _find_speaker(api: mijiaAPI, speaker_name: Optional[str]) -> Optional[dict]:
309
+ devices = api.get_devices_list()
310
+ if speaker_name is not None:
311
+ return next((d for d in devices if d["name"] == speaker_name), None)
312
+ return next((d for d in devices if "xiaomi.wifispeaker" in d["model"]), None)
313
+
314
+
308
315
  @mcp.tool
309
316
  def run_speaker_command(
310
317
  prompt: str,
311
318
  speaker_name: Optional[str] = None,
312
- quiet: bool = False,
319
+ quiet: bool = True,
313
320
  ) -> str:
314
321
  """通过小爱音箱执行自然语言指令。
315
322
 
316
323
  参数:
317
324
  prompt: 自然语言指令,如 "打开卧室台灯"、"把亮度调到50%"。
318
325
  speaker_name: 可选,指定小爱音箱名称,默认使用获取到的第一个小爱音箱。
319
- quiet: 是否静默执行(不语音播报),默认 False
326
+ quiet: 是否静默执行(不语音播报),默认 True
320
327
 
321
328
  返回执行结果。
322
329
  """
323
330
  api = _get_api()
324
331
  _refresh_if_needed(api)
325
- devices = api.get_devices_list()
326
- if speaker_name is None:
327
- match = None
328
- for device in devices:
329
- if "xiaomi.wifispeaker" in device["model"]:
330
- match = device
331
- break
332
- if match is None:
333
- return "未找到小爱音箱设备"
334
- else:
335
- matches = [d for d in devices if d["name"] == speaker_name]
336
- if not matches:
337
- return f"未找到名为 {speaker_name} 的小爱音箱"
338
- match = matches[0]
332
+ match = _find_speaker(api, speaker_name)
333
+ if match is None:
334
+ return "未找到小爱音箱设备" if speaker_name is None else f"未找到名为 {speaker_name} 的小爱音箱"
339
335
  speaker = mijiaDevice(api, did=match["did"])
340
336
  speaker.run_action("execute-text-directive", _in=[prompt, 1 if quiet else 0])
341
337
  return f"已通过 {match['name']} 执行: {prompt}"
342
338
 
343
339
 
340
+ @mcp.tool
341
+ def speaker_play(
342
+ text: str,
343
+ speaker_name: Optional[str] = None,
344
+ ) -> str:
345
+ """通过小爱音箱朗读指定文本(play-text),不是执行设备控制指令。"""
346
+ api = _get_api()
347
+ _refresh_if_needed(api)
348
+ match = _find_speaker(api, speaker_name)
349
+ if match is None:
350
+ return "未找到小爱音箱设备" if speaker_name is None else f"未找到名为 {speaker_name} 的小爱音箱"
351
+ speaker = mijiaDevice(api, did=match["did"])
352
+ speaker.run_action("play-text", _in=[text])
353
+ return f"已通过 {match['name']} 播放: {text}"
354
+
355
+
344
356
  def _login_worker(api: mijiaAPI, login_data: dict) -> None:
345
357
  try:
346
358
  api._complete_qr_login(login_data)
@@ -0,0 +1 @@
1
+ version = "4.3.0"
@@ -1 +0,0 @@
1
- version = "4.2.1"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes