]> git.basschouten.com Git - openhab-addons.git/blob
30eef0409e0a5baae8de4dfda1344e73080bb039
[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.List;
16
17 import org.openhab.core.events.EventPublisher;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.items.events.ItemCommandEvent;
20 import org.openhab.core.items.events.ItemEventFactory;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.Command;
24 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
25 import org.openhab.io.imperihome.internal.model.device.DeviceType;
26
27 /**
28  * Action setting a thermostat setpoint.
29  *
30  * @author Pepijn de Geus - Initial contribution
31  */
32 public class SetSetPointAction extends Action {
33
34     public SetSetPointAction(EventPublisher eventPublisher) {
35         super(eventPublisher);
36     }
37
38     @Override
39     public boolean supports(AbstractDevice device, Item item) {
40         if (device.getType() != DeviceType.THERMOSTAT) {
41             return false;
42         }
43         List<Class<? extends Command>> acceptedCommandTypes = item.getAcceptedCommandTypes();
44         return acceptedCommandTypes.contains(DecimalType.class) || acceptedCommandTypes.contains(StringType.class);
45     }
46
47     @Override
48     public void perform(AbstractDevice device, Item item, String value) {
49         List<Class<? extends Command>> acceptedCommandTypes = item.getAcceptedCommandTypes();
50
51         Command command;
52         if (acceptedCommandTypes.contains(DecimalType.class)) {
53             command = new DecimalType(value);
54         } else if (acceptedCommandTypes.contains(StringType.class)) {
55             command = new StringType(value);
56         } else {
57             logger.error("Item {} doesn't support Decimal or String type", item);
58             return;
59         }
60
61         ItemCommandEvent event = ItemEventFactory.createCommandEvent(item.getName(), command, COMMAND_SOURCE);
62         eventPublisher.post(event);
63     }
64 }