mijiaAPI 1.0.0__tar.gz → 1.1.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.1
2
2
  Name: mijiaAPI
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: A Python API for Xiaomi Mijia
5
5
  Home-page: https://github.com/Do1e/mijia-api
6
6
  Author: Do1e
@@ -23,7 +23,7 @@ python setup.py install
23
23
  ```
24
24
  或者
25
25
  ```bash
26
- pip install NJUlogin
26
+ pip install mijiaAPI
27
27
  ```
28
28
 
29
29
  ## 使用
@@ -44,7 +44,8 @@ pip install NJUlogin
44
44
  * `get_consumable_items(did: str) -> list`:获取设备的耗材信息,`did`为设备id,在设备列表中获取
45
45
  * `get_devices_prop(data: list) -> list`:获取设备的属性
46
46
  * `set_devices_prop(data: list) -> list`:设置设备的属性
47
- * ,`data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
47
+ * `data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
48
+ * 网站上的方法并非全部可用,需要自行测试
48
49
 
49
50
  ## 致谢
50
51
  * [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
@@ -7,7 +7,7 @@ python setup.py install
7
7
  ```
8
8
  或者
9
9
  ```bash
10
- pip install NJUlogin
10
+ pip install mijiaAPI
11
11
  ```
12
12
 
13
13
  ## 使用
@@ -28,7 +28,8 @@ pip install NJUlogin
28
28
  * `get_consumable_items(did: str) -> list`:获取设备的耗材信息,`did`为设备id,在设备列表中获取
29
29
  * `get_devices_prop(data: list) -> list`:获取设备的属性
30
30
  * `set_devices_prop(data: list) -> list`:设置设备的属性
31
- * ,`data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
31
+ * `data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
32
+ * 网站上的方法并非全部可用,需要自行测试
32
33
 
33
34
  ## 致谢
34
35
  * [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
@@ -1,5 +1,6 @@
1
1
  from .login import mijiaLogin
2
2
  from .apis import mijiaAPI
3
+ from .devices import mijiaDevices
3
4
 
4
5
  from .__version__ import (
5
6
  __title__,
@@ -1,5 +1,5 @@
1
1
  __title__ = "mijiaAPI"
2
- __version__ = "1.0.0"
2
+ __version__ = "1.1.0"
3
3
  __description__ = "A Python API for Xiaomi Mijia"
4
4
  __url__ = "https://github.com/Do1e/mijia-api"
5
5
  __author__ = "Do1e"
@@ -0,0 +1,80 @@
1
+ from typing import Union
2
+ from time import sleep
3
+ from .apis import mijiaAPI
4
+
5
+ class DevProp(object):
6
+ def __init__(self, prop_dict: dict):
7
+ self.name = prop_dict['name']
8
+ self.desc = prop_dict['description']
9
+ self.type = prop_dict['type']
10
+ if self.type not in ['bool', 'int', 'uint', 'float', 'string']:
11
+ raise ValueError(f'Unsupported type: {self.type}, available types: bool, int, uint, float, string')
12
+ self.rw = prop_dict['rw']
13
+ self.unit = prop_dict['unit']
14
+ self.range = prop_dict['range']
15
+ self.method = prop_dict['method']
16
+
17
+ def __str__(self):
18
+ return f'{self.name}: {self.desc}\n' \
19
+ f' valuetype: {self.type}, rw: {self.rw}, unit: {self.unit}, range: {self.range}'
20
+
21
+ class mijiaDevices(object):
22
+ def __init__(self, api: mijiaAPI, dev_info: dict, sleep_time: float = 0.5):
23
+ self.api = api
24
+ self.name = dev_info['name']
25
+ self.model = dev_info['model']
26
+ self.prop_list = {prop['name']: DevProp(prop) for prop in dev_info['properties']}
27
+ self.sleep_time = sleep_time
28
+
29
+ def __str__(self):
30
+ return f'{self.name} ({self.model})\n' \
31
+ f'Properties:\n' + '\n'.join([str(v) for v in self.prop_list.values()])
32
+
33
+ def set(self, name: str, did: str, value: Union[bool, int]) -> Union[bool, int]:
34
+ if name not in self.prop_list:
35
+ raise ValueError(f'Unsupported property: {name}, available properties: {list(self.prop_list.keys())}')
36
+ prop = self.prop_list[name]
37
+ if 'w' not in prop.rw:
38
+ raise ValueError(f'Property {name} is read-only')
39
+ if prop.type == 'bool':
40
+ if not isinstance(value, bool):
41
+ raise ValueError(f'Invalid value for bool: {value}, should be True or False')
42
+ elif prop.type in ['int', 'uint']:
43
+ try:
44
+ value = int(value)
45
+ if prop.range:
46
+ if value < prop.range[0] or value > prop.range[1]:
47
+ raise ValueError(f'Value out of range: {value}, should be in range {prop.range}')
48
+ except ValueError:
49
+ raise ValueError(f'Invalid value for int: {value}, should be an integer')
50
+ elif prop.type == 'float':
51
+ try:
52
+ value = float(value)
53
+ if prop.range:
54
+ if value < prop.range[0] or value > prop.range[1]:
55
+ raise ValueError(f'Value out of range: {value}, should be in range {prop.range}')
56
+ except ValueError:
57
+ raise ValueError(f'Invalid value for float: {value}, should be a float')
58
+ elif prop.type == 'string':
59
+ if not isinstance(value, str):
60
+ raise ValueError(f'Invalid value for string: {value}, should be a string')
61
+ else:
62
+ raise ValueError(f'Unsupported type: {prop.type}, available types: bool, int, uint, float, string')
63
+ method = prop.method.copy()
64
+ method['did'] = did
65
+ method['value'] = value
66
+ ret = self.api.set_devices_prop([method])[0]['code'] == 0
67
+ sleep(self.sleep_time)
68
+ return ret
69
+
70
+ def get(self, name: str, did: str) -> Union[bool, int]:
71
+ if name not in self.prop_list:
72
+ raise ValueError(f'Unsupported property: {name}, available properties: {list(self.prop_list.keys())}')
73
+ prop = self.prop_list[name]
74
+ if 'r' not in prop.rw:
75
+ raise ValueError(f'Property {name} is write-only')
76
+ method = prop.method.copy()
77
+ method['did'] = did
78
+ ret = self.api.get_devices_prop([method])[0]['value']
79
+ sleep(self.sleep_time)
80
+ return ret
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mijiaAPI
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: A Python API for Xiaomi Mijia
5
5
  Home-page: https://github.com/Do1e/mijia-api
6
6
  Author: Do1e
@@ -23,7 +23,7 @@ python setup.py install
23
23
  ```
24
24
  或者
25
25
  ```bash
26
- pip install NJUlogin
26
+ pip install mijiaAPI
27
27
  ```
28
28
 
29
29
  ## 使用
@@ -44,7 +44,8 @@ pip install NJUlogin
44
44
  * `get_consumable_items(did: str) -> list`:获取设备的耗材信息,`did`为设备id,在设备列表中获取
45
45
  * `get_devices_prop(data: list) -> list`:获取设备的属性
46
46
  * `set_devices_prop(data: list) -> list`:设置设备的属性
47
- * ,`data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
47
+ * `data`为一个字典的列表,字典需要包含`did`, `siid`, `piid`,后面两个键可从 *https://home.miot-spec.com/spec/{model}* 中获取,其中`model`为设备的model,在设备列表中获取,如[米家台灯 1S](https://home.miot-spec.com/spec/yeelink.light.lamp4)
48
+ * 网站上的方法并非全部可用,需要自行测试
48
49
 
49
50
  ## 致谢
50
51
  * [janzlan/mijia-api](https://gitee.com/janzlan/mijia-api/tree/master)
@@ -4,6 +4,7 @@ setup.py
4
4
  mijiaAPI/__init__.py
5
5
  mijiaAPI/__version__.py
6
6
  mijiaAPI/apis.py
7
+ mijiaAPI/devices.py
7
8
  mijiaAPI/login.py
8
9
  mijiaAPI/urls.py
9
10
  mijiaAPI/utils.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes