moroen / IKEA-Tradfri-plugin

A Python plugin for Domoticz to controll IKEA Gateway
68 stars 23 forks source link

CS and CWS switches not added to Groups in domoticz #92

Closed TheArk-guy closed 4 years ago

TheArk-guy commented 4 years ago

Great plugin, really enjoying it, thanks for creating and supporting it!

I am having a number of lights consisting of multiple GU10 size Ikea led lights and because of that I am making use of the 'Add groups as devices' option. At first, this created for me the Color Selector Switch devices in Domoticz for the groups, which was very convenient.

After adding a new set of lights recently, only the 'Switch' device was created for the group, not the WS Selector switch. After turning off the 'Add groups as devices' and then turning it on again, suddenly all selector switches for Groups where gone and not coming back.

After quite an amount of troubleshooting (i am/was completely new to Python), I think I found the cause of the issue and the solution. It is working again correctly in my case at least.

I added some print statements to the tradfricoap.py, ran the 'list --groups' option that runs the get_devices and noticed that when I printed the _group_members of each device, they all pointed to the same list. This is most of the times caused by variable scoping issues. In my case, the list shown was a list with of a group with one white only bulb in it, resulting in all the groups being considered as being non-colored (and therefor not creating the selector switch).

Looking into the scoping, I noticed that in 'class device:', '_group_members' is initialized on the level of the class, but not specifically in the def init of the class. I am not 100% on the variable scoping of Python, but assuming that the class device is only called once in the process lifetime and the init is called for every instance of the class that is created, this might result in all device instances pointing all to the same (empty) list after creating devices.

To test that, I have added the statement 'self._group_members = []' to the beginning of 'def init' of 'class device:' to tradfricoap.py. That solved the problem, after that, each instance was again showing it own list of members and the WS and CWS selector switches were created again for the correct groups in Domoticz.

It is a bit of an obscure problem, as it depends on luck if you run into it or not, but if I am correct, then it might be that other people are also having difficult to pinpoint problems that might be solved by adding the above.

moroen commented 4 years ago

Nice catch, even though I fail to see how this actually happens, and if it is something wrong with variable scope, why I've never seen this error. There must be some subtle series of occurrences... But anyway, explicit variabel initialization is never a bad idea, so I'll implement you fix in the next version...