]> git.basschouten.com Git - openhab-addons.git/blob
eab3edabd2caff5a28e51f06a1f2834f7a370b0c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.imperihome.internal.action;
14
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Objects;
18
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;
25
26 /**
27  * Action setting a choice from a selection list, e.g. MultiSwitch.
28  *
29  * @author Pepijn de Geus - Initial contribution
30  */
31 public class SetChoiceAction extends Action {
32
33     public SetChoiceAction(EventPublisher eventPublisher) {
34         super(eventPublisher);
35     }
36
37     @Override
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);
41     }
42
43     @Override
44     public void perform(AbstractDevice device, Item item, String value) {
45         String newValue = null;
46
47         for (Entry<String, String> entry : device.getMapping().entrySet()) {
48             if (Objects.equals(entry.getValue(), value)) {
49                 newValue = entry.getKey();
50                 break;
51             }
52         }
53
54         if (newValue == null) {
55             logger.warn("Could not find selection '{}' in mapping {} of device {}", value, device.getMapping(), device);
56             return;
57         }
58
59         ItemCommandEvent event = ItemEventFactory.createCommandEvent(item.getName(), new DecimalType(newValue),
60                 COMMAND_SOURCE);
61         eventPublisher.post(event);
62     }
63 }