howanghk / homebridge-ewelink

Homebridge plugin to control Sonoff relays with OEM firmware
MIT License
81 stars 46 forks source link

Removal & Addition of devices #105

Closed bwp91 closed 3 years ago

bwp91 commented 4 years ago

Hi,

I've been tinkering around with this plugin trying to understand how it works. I've noticed a small issue with this release (which doesn't actually affect functionality).

I've been looking through my log files and it seems some of my devices are being removed from the cache and immediately re-added after restarting Homebridge.

I've whittled it down to the following piece of code (lines 149-171)

if (accessory.context.switches > 1) {
    realDeviceId = deviceId.replace('CH' + accessory.context.channel, "");
}

if (platform.devicesFromApi.has(realDeviceId) && (accessory.context.switches <= 1 || accessory.context.channel <= accessory.context.switches)) {
    if ((deviceId != realDeviceId) && platform.groups.has(realDeviceId)) {
        platform.log('Device [%s], ID : [%s] is now grouped. It will be removed.', accessory.displayName, accessory.UUID);
        platform.removeAccessory(accessory);
    } else if ((deviceId == realDeviceId) && !platform.groups.has(realDeviceId)) {
        platform.log('Device [%s], ID : [%s] is now splitted. It will be removed.', accessory.displayName, accessory.UUID);
        platform.removeAccessory(accessory);
    } else if (platform.getDeviceTypeByUiid(platform.devicesFromApi.get(realDeviceId).uiid) === 'FAN_LIGHT' && accessory.context.channel !== null) {
        platform.log('Device [%s], ID : [%s] is now grouped as a fan. It will be removed.', accessory.displayName, accessory.UUID);
        platform.removeAccessory(accessory);
    } else {
        platform.log('[%s] Device is registered with API. ID: (%s). Nothing to do.', accessory.displayName, accessory.UUID);
    }
} else if (platform.devicesFromApi.has(realDeviceId) && platform.getDeviceTypeByUiid(platform.devicesFromApi.get(realDeviceId).uiid) === 'FAN_LIGHT') {
    platform.log('[%s] Device is registered with API. ID: (%s). Nothing to do.', accessory.displayName, accessory.UUID);
} else {
    platform.log('Device [%s], ID : [%s] was not present in the response from the API. It will be removed.', accessory.displayName, accessory.UUID);
    platform.removeAccessory(accessory);
}

I am struggling to get my head round all this logic here and wondered if someone could help me.

The only devices I own are the wall light switches, I have both 1 channel and 2 channels around the house.

According to the logs, it is the single channel devices which are being removed and then re-added:

[11/06/2020, 13:16:53] [eWeLink] Device [Kitchen Lights], ID : [***] is now splitted. It will be removed.
[11/06/2020, 13:16:53] [eWeLink] Removing accessory [Kitchen Lights]
...
[11/06/2020, 13:16:53] [eWeLink] Device with ID [***] is not configured. Add accessory.
[11/06/2020, 13:16:53] [eWeLink] Device type for 6 is SWITCH
[11/06/2020, 13:16:53] [eWeLink] [Kitchen Lights] with ID [***] will be added.
[11/06/2020, 13:16:53] [eWeLink] Adding [Kitchen Lights], model [T1 1C], status [on], online [true].

Whereas the multi channel devices are not affected:

[11/06/2020, 13:16:53] [eWeLink] [Porch Light] Device is registered with API. ID: (***). Nothing to do.

Just wondered if anyone could help me understand the logic of the above code so I can try and make amendments.

Thanks