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;
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;
24 * Items setting RGB color value.
26 * @author Pepijn de Geus - Initial contribution
28 public class SetColorAction extends Action {
30 public SetColorAction(EventPublisher eventPublisher) {
31 super(eventPublisher);
35 public boolean supports(AbstractDevice device, Item item) {
36 return item.getAcceptedCommandTypes().contains(HSBType.class);
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);
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);
50 ItemCommandEvent event;
51 if (r == 0 && g == 0 && b == 0) {
52 event = ItemEventFactory.createCommandEvent(item.getName(), OnOffType.OFF, COMMAND_SOURCE);
54 HSBType hsbValue = HSBType.fromRGB(r, g, b);
55 event = ItemEventFactory.createCommandEvent(item.getName(), hsbValue, COMMAND_SOURCE);
58 eventPublisher.post(event);