mijiaAPI 4.1.2__tar.gz → 4.1.3__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.1.2
3
+ Version: 4.1.3
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
@@ -23,7 +23,7 @@ Dynamic: license-file
23
23
 
24
24
  米家 API,可以使用代码、CLI、MCP 直接控制米家设备。
25
25
 
26
- > 🎉 **v4.0.0 版本已支持 MCP**,详见 [MCP 使用文档](https://mijia-api.do1e.com/usage/mcp)
26
+ > 🎉 **v4.0.0 版本已支持 MCP**,详见 [MCP 使用文档](https://mijia-api.do1e.com/usage/mcp)
27
27
  > 🎉 **v4.1.0 版本已支持 Agent Skill**,详见 [Agent Skill 使用文档](https://mijia-api.do1e.com/usage/skill)
28
28
 
29
29
  [![GitHub](https://img.shields.io/badge/GitHub-Do1e%2Fmijia--api-blue)](https://github.com/Do1e/mijia-api)
@@ -2,7 +2,7 @@
2
2
 
3
3
  米家 API,可以使用代码、CLI、MCP 直接控制米家设备。
4
4
 
5
- > 🎉 **v4.0.0 版本已支持 MCP**,详见 [MCP 使用文档](https://mijia-api.do1e.com/usage/mcp)
5
+ > 🎉 **v4.0.0 版本已支持 MCP**,详见 [MCP 使用文档](https://mijia-api.do1e.com/usage/mcp)
6
6
  > 🎉 **v4.1.0 版本已支持 Agent Skill**,详见 [Agent Skill 使用文档](https://mijia-api.do1e.com/usage/skill)
7
7
 
8
8
  [![GitHub](https://img.shields.io/badge/GitHub-Do1e%2Fmijia--api-blue)](https://github.com/Do1e/mijia-api)
@@ -19,6 +19,17 @@ if log_level_name not in ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']:
19
19
  log_level = getattr(logging, log_level_name, logging.INFO)
20
20
  logging.getLogger("mijiaAPI").setLevel(log_level)
21
21
 
22
+
23
+ def json_object(value: str) -> dict:
24
+ try:
25
+ result = json.loads(value)
26
+ except json.JSONDecodeError as e:
27
+ raise argparse.ArgumentTypeError(f"无效 JSON: {e.msg}") from e
28
+ if not isinstance(result, dict):
29
+ raise argparse.ArgumentTypeError("必须是 JSON 对象")
30
+ return result
31
+
32
+
22
33
  def parse_args(args):
23
34
  parser = argparse.ArgumentParser(description=f"Mijia API CLI (v{version})")
24
35
  subparsers = parser.add_subparsers(dest='command')
@@ -190,6 +201,87 @@ def parse_args(args):
190
201
  help="需要设定的属性值",
191
202
  required=True,
192
203
  )
204
+
205
+ action = subparsers.add_parser(
206
+ 'action',
207
+ help="执行设备动作",
208
+ )
209
+ action.set_defaults(func='action')
210
+ action.add_argument(
211
+ '-p', '--auth_path',
212
+ type=Path,
213
+ default=Path.home() / ".config" / "mijia-api" / "auth.json",
214
+ help="认证文件保存路径,默认保存在 ~/.config/mijia-api/auth.json",
215
+ )
216
+ action_device = action.add_mutually_exclusive_group(required=True)
217
+ action_device.add_argument(
218
+ '--did',
219
+ type=str,
220
+ help="设备did",
221
+ )
222
+ action_device.add_argument(
223
+ '--dev_name',
224
+ type=str,
225
+ help="设备名称,指定为米家APP中设定的名称",
226
+ )
227
+ action.add_argument(
228
+ '--action_name',
229
+ type=str,
230
+ help="动作名称,先使用 --get_device_info 获取",
231
+ required=True,
232
+ )
233
+ action.add_argument(
234
+ '--params',
235
+ type=json_object,
236
+ help='动作参数 JSON 对象,例如 {"value":[2]}',
237
+ default=None,
238
+ )
239
+
240
+ statistics = subparsers.add_parser(
241
+ 'statistics',
242
+ help="获取设备统计数据",
243
+ )
244
+ statistics.set_defaults(func='statistics')
245
+ statistics.add_argument(
246
+ '-p', '--auth_path',
247
+ type=Path,
248
+ default=Path.home() / ".config" / "mijia-api" / "auth.json",
249
+ help="认证文件保存路径,默认保存在 ~/.config/mijia-api/auth.json",
250
+ )
251
+ statistics.add_argument(
252
+ '--did',
253
+ type=str,
254
+ help="设备did,先使用 --list_devices 获取",
255
+ required=True,
256
+ )
257
+ statistics.add_argument(
258
+ '--key',
259
+ type=str,
260
+ help='设备相关的统计键,例如 lumi.acpartner.mcn04 的 "7.1"',
261
+ required=True,
262
+ )
263
+ statistics.add_argument(
264
+ '--data_type',
265
+ type=str,
266
+ help="统计类型,例如 stat_month_v3;旧设备可能不带 _v3",
267
+ required=True,
268
+ )
269
+ statistics.add_argument(
270
+ '--limit',
271
+ type=int,
272
+ help="返回的最大条目数,默认 6",
273
+ default=6,
274
+ )
275
+ statistics.add_argument(
276
+ '--time_start',
277
+ type=int,
278
+ help="开始时间戳(秒),默认结束时间前 30 天",
279
+ )
280
+ statistics.add_argument(
281
+ '--time_end',
282
+ type=int,
283
+ help="结束时间戳(秒),默认为当前时间",
284
+ )
193
285
  return parser.parse_args(args)
194
286
 
195
287
  def init_api(auth_path: Path) -> mijiaAPI:
@@ -333,6 +425,27 @@ def set(args):
333
425
  return
334
426
  print(f"{device.name} ({device.did}) 的 {args.prop_name} 值已设置为 {args.value}")
335
427
 
428
+
429
+ def run_action(api: mijiaAPI, args):
430
+ device = mijiaDevice(api, did=args.did, dev_name=args.dev_name)
431
+ device.run_action(args.action_name, **(args.params or {}))
432
+ print(f"{device.name} ({device.did}) 的动作 {args.action_name} 指令已发送")
433
+
434
+
435
+ def get_statistics(api: mijiaAPI, args):
436
+ time_end = args.time_end if args.time_end is not None else int(time.time())
437
+ time_start = args.time_start if args.time_start is not None else time_end - 30 * 24 * 3600
438
+ result = api.get_statistics({
439
+ "did": args.did,
440
+ "key": args.key,
441
+ "data_type": args.data_type,
442
+ "limit": args.limit,
443
+ "time_start": time_start,
444
+ "time_end": time_end,
445
+ })
446
+ print(json.dumps(result, indent=2, ensure_ascii=False))
447
+
448
+
336
449
  def main(args):
337
450
  args = parse_args(args)
338
451
 
@@ -390,6 +503,10 @@ def main(args):
390
503
  get(args)
391
504
  if args.func == 'set':
392
505
  set(args)
506
+ if args.func == 'action':
507
+ run_action(api, args)
508
+ if args.func == 'statistics':
509
+ get_statistics(api, args)
393
510
  if args.func == 'run':
394
511
  if device_mapping is None:
395
512
  device_mapping = get_devices_list(api, verbose=False)
@@ -68,7 +68,7 @@ def run(auth_path: Path) -> None:
68
68
  f"请调用 login 工具重新登录后再使用其他工具"
69
69
  )
70
70
 
71
- mcp.run()
71
+ mcp.run(show_banner=False)
72
72
 
73
73
 
74
74
  @mcp.tool
@@ -274,13 +274,20 @@ def get_statistics(
274
274
 
275
275
  参数:
276
276
  did: 设备ID,可从 list_devices 获取。
277
- key: 统计数据键,格式为 siid.piid(如 "7.1"),需根据设备型号确定。
278
- data_type: 统计粒度,可选 stat_hour_v3 / stat_day_v3 / stat_week_v3 / stat_month_v3
277
+ key: 设备相关的统计键,通常为 siid.piid。例如 lumi.acpartner.mcn04
278
+ 的耗电量使用 "7.1",lumi.acpartner.mcn02 使用 "powerCost"
279
+ data_type: 统计粒度:stat_hour_v3 / stat_day_v3 / stat_week_v3 /
280
+ stat_month_v3。较旧设备可能使用不带 _v3 的对应类型。
279
281
  limit: 返回最大条目数,默认6。
280
282
  time_start: 起始时间戳(秒),不传默认为30天前。
281
283
  time_end: 结束时间戳(秒),不传默认为当前时间。
282
284
 
283
- 返回统计条目列表(时间戳与数值)。
285
+ 返回 JSON 编码的统计条目列表。每项包含 time 时间戳和 value;value 通常仍是
286
+ JSON 数组字符串,例如 "[48.476]"。
287
+
288
+ 注意:统计接口仅支持部分设备,不同型号可能需要不同的 key、data_type,甚至使用
289
+ 不同 API。详见 https://github.com/Do1e/mijia-api/issues/46
290
+ 参考 https://iot.mi.com/new/doc/accesses/direct-access/extension-development/extension-functions/statistical-interface
284
291
  """
285
292
  import time
286
293
  api = _get_api()
@@ -0,0 +1 @@
1
+ version = "4.1.3"
@@ -1 +0,0 @@
1
- version = "4.1.2"
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