2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.imperihome.internal.action;
16 import java.util.Map.Entry;
17 import java.util.Objects;
19 import org.openhab.core.events.EventPublisher;
20 import org.openhab.core.items.Item;
21 import org.openhab.core.items.events.ItemCommandEvent;
22 import org.openhab.core.items.events.ItemEventFactory;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
27 * Action setting a choice from a selection list, e.g. MultiSwitch.
29 * @author Pepijn de Geus - Initial contribution
31 public class SetChoiceAction extends Action {
33 public SetChoiceAction(EventPublisher eventPublisher) {
34 super(eventPublisher);
38 public boolean supports(AbstractDevice device, Item item) {
39 Map<String, String> mapping = device.getMapping();
40 return mapping != null && !mapping.isEmpty() && item.getAcceptedCommandTypes().contains(DecimalType.class);
44 public void perform(AbstractDevice device, Item item, String value) {
45 String newValue = null;
47 for (Entry<String, String> entry : device.getMapping().entrySet()) {
48 if (Objects.equals(entry.getValue(), value)) {
49 newValue = entry.getKey();
54 if (newValue == null) {
55 logger.warn("Could not find selection '{}' in mapping {} of device {}", value, device.getMapping(), device);
59 ItemCommandEvent event = ItemEventFactory.createCommandEvent(item.getName(), new DecimalType(newValue),
61 eventPublisher.post(event);