canonical/matter-bridge-tapo

KeyError: '-1008' when setting saturation to larger than 100

Closed this issue · 5 comments

Please refer to the details on the PyP100 package side: fishbigger/TapoP100#110

$ sudo chip-tool colorcontrol move-to-saturation 200 0 0 0 110 1
(No error)
$ journalctl -f | grep matter-bridge-tapo-lighting
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]: Exception ignored on calling ctypes callback function: <function attributeChangeCallback at 0x7f688f5f3f40>
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]: Traceback (most recent call last):
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:   File "/snap/matter-bridge-tapo-lighting/1/bin/lighting.py", line 134, in attributeChangeCallback
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:     set_saturation(value[0])
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:   File "/snap/matter-bridge-tapo-lighting/1/bin/lighting.py", line 87, in set_saturation
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:     dev.setColor(hue, saturation)
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:   File "/snap/matter-bridge-tapo-lighting/1/connectedhomeip/python_env/lib/python3.10/site-packages/PyP100/PyL530.py", line 109, in setColor
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]:     errorMessage = self.errorCodes[str(errorCode)]
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]: KeyError: '-1008'
Apr 18 15:32:31 ubuntu matter-bridge-tapo-lighting.tapo-bridge[144089]: [1681824751.710664][144089:144098] CHIP:ZCL: Hue 200 Saturation 200 endpoint 1

The library has a setColor function which takes both hue and saturation as input. There is no function to set saturation or hue separately so the bridge tries to work around that by querying the device state (incl. current hue and saturation) during initialization and then using those values whenever one isn't given by the controller. When either of the values are given by the controller, the bridge updates the local device state for next calls. See: https://github.com/farshidtz/matter-bridge-tapo/blob/bad3f64df21f12b40b44451a114db82a135badb2/lighting.py#L68-L87

The device state in memory should stay consistent with the actual state as long as the user doesn't manually change the values via the Kasa app.

With that said, the reported bug shouldn't exist. Need to investigate further.

Upon trying to set the color temperature, the identical error code presents itself once more:

$ sudo chip-tool colorcontrol move-color-temperature 1 16319 0 65279 0 0 110 1
(No error)
$ journalctl -f | grep matter-bridge-tapo-lighting
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]: Exception ignored on calling ctypes callback function: <function attributeChangeCallback at 0x7f30bb7dbf40>
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]: Traceback (most recent call last):
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:   File "/snap/matter-bridge-tapo-lighting/1/bin/lighting.py", line 138, in attributeChangeCallback
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:     set_color_temperature(value[0])
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:   File "/snap/matter-bridge-tapo-lighting/1/bin/lighting.py", line 94, in set_color_temperature
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:     dev.setColorTemp(kelvin)
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:   File "/snap/matter-bridge-tapo-lighting/1/connectedhomeip/python_env/lib/python3.10/site-packages/PyP100/PyL530.py", line 74, in setColorTemp
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]:     errorMessage = self.errorCodes[str(errorCode)]
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]: KeyError: '-1008'
Apr 19 11:11:13 ubuntu matter-bridge-tapo-lighting.tapo-bridge[190983]: [1681895473.024545][190983:190990] CHIP:ZCL: Color Temperature 65279

The saturation setting will only function correctly if the value falls within the range of 1 to 100:

# sudo chip-tool colorcontrol move-to-saturation <Saturation> <TransitionTime> <OptionsMask> <OptionsOverride> <destination-id> <endpoint-id-ignored-for-group-commands>
sudo chip-tool colorcontrol move-to-saturation 50 0 0 0 110 1

So the error is really because an out of range value is being sent to the library. The following references also send larger values:

In seems like the byte input from the controller should be mapped to 0-100 which is the value expected by the library.

Fixed by #3