]> git.basschouten.com Git - openhab-addons.git/blob
b7c916067cdc9ca98b886b6e620a488f2b4828eb
[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 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.HSBType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.io.imperihome.internal.model.device.AbstractDevice;
22
23 /**
24  * Items setting RGB color value.
25  *
26  * @author Pepijn de Geus - Initial contribution
27  */
28 public class SetColorAction extends Action {
29
30     public SetColorAction(EventPublisher eventPublisher) {
31         super(eventPublisher);
32     }
33
34     @Override
35     public boolean supports(AbstractDevice device, Item item) {
36         return item.getAcceptedCommandTypes().contains(HSBType.class);
37     }
38
39     @Override
40     public void perform(AbstractDevice device, Item item, String value) {
41         if (value == null || value.length() != 8) {
42             logger.error("Invalid parameter: '{}'. Format must be 'aarrggbb'.", value);
43             return;
44         }
45
46         int r = Integer.parseInt(value.substring(2, 4), 16);
47         int g = Integer.parseInt(value.substring(4, 6), 16);
48         int b = Integer.parseInt(value.substring(6, 8), 16);
49
50         ItemCommandEvent event;
51         if (r == 0 && g == 0 && b == 0) {
52             event = ItemEventFactory.createCommandEvent(item.getName(), OnOffType.OFF, COMMAND_SOURCE);
53         } else {
54             HSBType hsbValue = HSBType.fromRGB(r, g, b);
55             event = ItemEventFactory.createCommandEvent(item.getName(), hsbValue, COMMAND_SOURCE);
56         }
57
58         eventPublisher.post(event);
59     }
60 }