merdok / homebridge-miot

Homebridge plugin for devices supporting the Xiaomi miot protocol
MIT License
399 stars 64 forks source link

Property poll does not check that the result property status may not be in the same order as the request is sent #641

Open sll1001 opened 1 week ago

sll1001 commented 1 week ago

Describe the bug In MiotDevice.js: requestPropertyChunk, the code assume the key-value pair in the result is in the same order as it is in the request. The fragment of code is:

this.logger.deepDebug(Successfully updated property chunk! RAW: ${JSON.stringify(result)}); // all good, process the data const obj = {}; for (let i = 0; i < result.length; i++) { this._updatePropertyValueFromDevice(obj, propKeys[i], result[i]); }

Notice that in this._updatePropertyValueFromDevice(obj,propKeys[i],result[i]), the assumption is the key-value pair in 'result' in the same order as the propKeys.

On a lumi.switch.acn032, which is a multi-switch with a touch LCD screen, it just does not return the result in the same order of the request. And that causes the update to the properties go wrong. I did not try to correct the bug but instead side-step it by removing screen:on and screen:brightness from commonProperties() so that only the switches are polled. See debugging log.

Expected behavior A clear and concise description of what you expected to happen.

Your config.json Provide the config.json entry for your device.

Debug log

Living SWS2 is a lumi.switch.acn032 device

  1. the system decided to use the following properties in property poll. Notice the order.

[11/19/2024, 7:15:55 PM] [miot] [Living SWS2] Preparing property poll! [11/19/2024, 7:15:55 PM] [miot] [Living SWS2] Splitting properties into chunks. Number of chunks: 1. Chunk size: 14 [11/19/2024, 7:15:55 PM] [miot] [Living SWS2] Chunks: [ [ "switch:on", "screen:on", "screen:brightness", "switch3:on", "switch4:on" ] ]

  1. Look at the result coming back. The property status returned by the device is in the order of increasing slid. This differs from the order of the request (as shown in 1). The bug described above causes the property update to go wrong. Notice that 'switch3:on:50', switch3 expects a boolean and get 50 (which is the screen brightness).

[11/19/2024, 7:15:55 PM] [miot] [Living SWS2] (Protocol) 172.16.88.84 -> Message: {"id":1886,"result":[{"code":0,"did":"551793135","piid":1,"siid":2," value":true},{"code":0,"did":"551793135","piid":1,"siid":3,"value":true},{"code":0,"did":"551793135","piid":1,"siid":4,"value":false},{"code":0,"did": "551793135","piid":2,"siid":7,"value":50},{"code":0,"did":"551793135","piid":8,"siid":7,"value":true}],"exe_time":41} [11/19/2024, 7:15:55 PM] [miot] [Living SWS2] Successfully updated property chunk! RAW: [{"code":0,"did":"551793135","piid":1,"siid":2,"value":true},{ "code":0,"did":"551793135","piid":1,"siid":3,"value":true},{"code":0,"did":"551793135","piid":1,"siid":4,"value":false},{"code":0,"did":"551793135","p iid":2,"siid":7,"value":50},{"code":0,"did":"551793135","piid":8,"siid":7,"value":true}] [11/19/2024, 7:15:55 PM] [miot] [Living SWS2] Device properties updated: [ "switch:on: true", "switch:default-power-on-state: 0 | (not synced)", "switch:fault: 0 | (not synced)", "switch3:on: 50", "switch4:on: true", "screen:brightness: false", "screen:auto-screen-off-time: 0 | (not synced)", "screen:on: true", "screen:on9: false | (not synced)", "speaker:mute: false | (not synced)", "speaker:volume: 0 | (not synced)", "no-disturb:no-disturb: false | (not synced)", "no-disturb:enable-time-period: | (not synced)", "power-consumption:power-consumption: 0 | (not synced)", "power-consumption:electric-power: 0 | (not synced)", "power-consumption:power-consumption-accumulation-way: false | (not synced)", "wireless-switch:switch-config: | (not synced)", "theme:screen-sequence: | (not synced)", "theme:screen-layout-info: | (not synced)", "theme:theme-version: 0 | (not synced)", "safety:password: | (not synced)" ]

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

merdok commented 1 week ago

Thanks for reporting the issue. I will check it out and see if there is something that I can do about it.

merdok commented 1 week ago

So i had a look and it is not an easy fix as i am not really sure what consequence it would have for other devices. In general the miot protocol should return in the response the values in the same order as they were requested, apparently some manufactures do not follow that guidelines hence you see the issue...

In the end we would need to search the response for the correct property by identifying it by the siid and piid. I would need to dig a big deeper into that, if you have some time then you could try to do that and propose a change.