]> git.basschouten.com Git - openhab-addons.git/blob
8b97dc33cfbf5cf9af09f2b47df39650b3e86ac1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 org.openhab.core.events.EventPublisher;
16 import org.openhab.core.items.Item;
17 import org.openhab.core.items.events.ItemCommandEvent;
18 import org.openhab.core.items.events.ItemEventFactory;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
21 import org.openhab.io.imperihome.internal.model.device.DeviceType;
22 import org.openhab.io.imperihome.internal.processor.DeviceRegistry;
23 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
24
25 /**
26  * Action setting a thermostat mode.
27  *
28  * @author Pepijn de Geus - Initial contribution
29  */
30 public class SetModeAction extends Action {
31
32     private final DeviceRegistry deviceRegistry;
33
34     public SetModeAction(EventPublisher eventPublisher, DeviceRegistry deviceRegistry) {
35         super(eventPublisher);
36         this.deviceRegistry = deviceRegistry;
37     }
38
39     @Override
40     public boolean supports(AbstractDevice device, Item item) {
41         if (device.getType() != DeviceType.THERMOSTAT) {
42             return false;
43         }
44         String curmode = device.getLinks().get("curmode");
45         return curmode != null && !curmode.isBlank();
46     }
47
48     @Override
49     public void perform(AbstractDevice device, Item item, String value) {
50         String modeDeviceName = device.getLinks().get("curmode");
51         AbstractDevice modeDevice = deviceRegistry.getDevice(ItemProcessor.getDeviceId(modeDeviceName));
52         if (modeDevice == null) {
53             logger.error("Couldn't resolve linked CurMode device '{}', make sure the Item has iss tags",
54                     modeDeviceName);
55             return;
56         }
57
58         Item modeItem = modeDevice.getItem();
59
60         ItemCommandEvent event = ItemEventFactory.createCommandEvent(modeItem.getName(), new StringType(value),
61                 COMMAND_SOURCE);
62         eventPublisher.post(event);
63     }
64 }