function mergeDevices(newDevice, existingDevice, updateAttribute = 'updatable') {
  // No existing device
  if (!existingDevice) {
    return newDevice;
  }

  const { features: newFeatures = [], params: newParams = [] } = newDevice;
  const { features: oldFeatures = [], params: oldParams = [], name, room_id: roomId } = existingDevice;

  let deviceChanged = oldFeatures.length !== newFeatures.length || oldParams.length !== newParams.length;

  // Group old features by external id
  const oldFeatureByExternalId = {};
  oldFeatures.forEach((oldFeature) => {
    oldFeatureByExternalId[oldFeature.external_id] = oldFeature;
  });

  // Merge matching features
  const features = newFeatures.map((newFeature) => {
    const oldFeature = oldFeatureByExternalId[newFeature.external_id];

    if (!oldFeature) {
      deviceChanged = true;
      return newFeature;
    }

    return mergeFeatures(newFeature, oldFeature);
  });

  let i = 0;
  while (!deviceChanged && newParams[i]) {
    deviceChanged = matchParam(oldParams, newParams[i]) < 0;
    i += 1;
  }

  return { ...existingDevice, ...newDevice, name, room_id: roomId, features, [updateAttribute]: deviceChanged };
}
@description Merge device attributes from existing with the new one.
It keeps 'name', 'room_id' attributes from existing.
@param {object} newDevice - Newly created device.
@param {object} existingDevice - Already existing device.
@param {string} updateAttribute - The attribute to add on the device to check if it is still new or update.
@returns {object} A new device merged with existing one, or the new device if it didn't change.
@example
mergeDevices({ name: 'Default name', features: [] }, { name: 'Overriden name', features: [] })