I found the solution myself:
public function __construct(
\Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement,
\Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory $optionLabelFactory,
\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory,
\Magento\Framework\Message\ManagerInterface $messageManager
) {
$this->attributeOptionManagement = $attributeOptionManagement;
$this->optionLabelFactory = $optionLabelFactory;
$this->optionFactory = $optionFactory;
$this->messageManager = $messageManager;
}
public function addOptions() {
$eavConfig = $this->_objectManager->create(\Magento\Eav\Model\Config::class);
$attribute = $eavConfig->getAttribute('catalog_product', 'brand');
$existingValues = $attribute->getSource()->getAllOptions(); // get existing attribute option values
$existingLabels = [];
foreach ($existingValues as $existingValue) {
$existingLabels[] = explode(',', $existingValue['label']);
}
$labels = ['testvalue34']; // add new attribute value here
if (!in_array($labels, $existingLabels)) {
foreach ($labels as $label) {
$optionLabel = $this->optionLabelFactory->create();
$optionLabel->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
$optionLabel->setLabel($label);
$option = $this->optionFactory->create();
$option->setLabel($label);
$option->setStoreLabels([$optionLabel]);
$option->setSortOrder(0);
$option->setIsDefault(false);
$this->attributeOptionManagement->add(
\Magento\Catalog\Model\Product::ENTITY,
'manufacturer',
$option
);
}
$messageManager->addSuccess(__("New attribute value saved"));
} else {
$messageManager->addError(__("Attribute value already exists"));
}
}