Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: perf cloud set prop #3

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ generated-members=
[FORMAT]

# Maximum number of characters on a single line.
max-line-length=120
max-line-length=80

# TODO(https://github.com/pylint-dev/pylint/issues/3352): Direct pylint to exempt
# lines made too long by directives to pytype.
Expand Down
26 changes: 26 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# TODO

If you encounter problems during the integration process, you can create an issue in the ISSUE section of the repository. Similarly, if you have other requirements, you can create a topic for discussion in the DISCUSSION section of the repository. If everyone deems the requirements reasonable, we will provide support in the future.

## Features
- [ ] Support cross-device entity connection. For example, the air conditioner companion supports binding other entities with temperature and humidity sensors, or other connection rules.
- [ ] Supports converting Xiaoai audio playback to a media player entity and can attempt to parse and display the played audio information.
- [ ] Support entity filtering, and you can select the entities to be displayed.
- [ ] Support multi-language translation configuration.

## Bugs

## Improvements
- [ ] Reconstruct the climate entity.
- [ ] When integrating overloading, events need to be avoided from being triggered. You can try to adjust the entity state refresh logic to solve this problem.
- [ ] Optimize the logic of the equipment control link, such as information like exception capture and error prompts.
- [ ] Optimize the device event logic.
- [ ] Optimize the device online and offline logic, such as creating a timer to refresh the device status regularly.

## Documentation
- [ ] Improve the usage documentation, such as the introduction of various configuration parameters.

## Testing
- [ ] Improve the LAN service test cases. Simulated device testing can be used.

## Miscellaneous
23 changes: 12 additions & 11 deletions custom_components/xiaomi_home/miot/miot_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ async def set_prop_async(
else:
result = await mips.set_prop_async(
did=did, siid=siid, piid=piid, value=value)
_LOGGER.info(
'gw set prop, %s.%s.%s=%s -> %s',
did, siid, piid, value, result)
rc = (result or {}).get(
'code', MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value)
if rc in [0, 1]:
Expand All @@ -645,8 +648,9 @@ async def set_prop_async(
if device_lan and device_lan.get('online', False):
result = await self._miot_lan.set_prop_async(
did=did, siid=siid, piid=piid, value=value)
_LOGGER.debug(
'lan set prop, %s, %s, %s -> %s', did, siid, piid, result)
_LOGGER.info(
'lan set prop, %s.%s.%s=%s -> %s',
did, siid, piid, value, result)
rc = (result or {}).get(
'code', MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value)
if rc in [0, 1]:
Expand All @@ -658,14 +662,12 @@ async def set_prop_async(
device_cloud = self._device_list_cloud.get(did, None)
if device_cloud and device_cloud.get('online', False):
result = await self._http.set_prop_async(
params=[
{'did': did, 'siid': siid, 'piid': piid, 'value': value}
])
_LOGGER.debug(
'set prop response, %s.%d.%d, %s, result, %s',
did=did, siid=siid, piid=piid, value=value)
_LOGGER.info(
'cloud set prop, %s.%d.%d=%s -> result, %s',
did, siid, piid, value, result)
if result and len(result) == 1:
rc = result[0].get(
if result:
rc = result.get(
'code', MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value)
if rc in [0, 1]:
return True
Expand All @@ -675,8 +677,7 @@ async def set_prop_async(
self._main_loop.create_task(
await self.__refresh_cloud_device_with_dids_async(
dids=[did]))
raise MIoTClientError(
self.__get_exec_error_with_rc(rc=rc))
raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc))

# Show error message
raise MIoTClientError(
Expand Down
28 changes: 23 additions & 5 deletions custom_components/xiaomi_home/miot/miot_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ async def __mihome_api_get_async(
res_obj: dict = json.loads(res_str)
if res_obj.get('code', None) != 0:
raise MIoTHttpError(
f'invalid response code, {res_obj.get("code",None)}, '
f'{res_obj.get("message","")}')
f'invalid response code, {res_obj.get("code", None)}, '
f'{res_obj.get("message", "")}')
_LOGGER.debug(
'mihome api get, %s%s, %s -> %s',
self._base_url, url_path, params, res_obj)
Expand All @@ -354,8 +354,8 @@ async def __mihome_api_post_async(
res_obj: dict = json.loads(res_str)
if res_obj.get('code', None) != 0:
raise MIoTHttpError(
f'invalid response code, {res_obj.get("code",None)}, '
f'{res_obj.get("message","")}')
f'invalid response code, {res_obj.get("code", None)}, '
f'{res_obj.get("message", "")}')
_LOGGER.debug(
'mihome api post, %s%s, %s -> %s',
self._base_url, url_path, data, res_obj)
Expand Down Expand Up @@ -777,7 +777,25 @@ async def get_prop_async(

return await fut

async def set_prop_async(self, params: list) -> list:
async def set_prop_async(
self, did: str, siid: int, piid: int, value: Any
) -> dict:
res_obj = await self.__mihome_api_post_async(
url_path='/app/v2/miotspec/prop/set',
data={
'params': [
{'did': did, 'siid': siid, 'piid': piid, 'value': value}]
},
timeout=15
)
if 'result' not in res_obj and len(res_obj['result']) != 1:
raise MIoTHttpError(
f'invalid response result, {res_obj}',
MIoTErrorCode.CODE_MIPS_INVALID_RESULT)

return res_obj['result'][0]

async def set_props_async(self, params: list) -> list:
"""
params = [{"did": "xxxx", "siid": 2, "piid": 1, "value": False}]
"""
Expand Down
Loading