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.binding.boschshc.internal.devices.smartbulb;
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.devices.BoschSHCDeviceHandler;
21 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
22 import org.openhab.binding.boschshc.internal.services.binaryswitch.BinarySwitchService;
23 import org.openhab.binding.boschshc.internal.services.binaryswitch.dto.BinarySwitchServiceState;
24 import org.openhab.binding.boschshc.internal.services.hsbcoloractuator.HSBColorActuatorService;
25 import org.openhab.binding.boschshc.internal.services.hsbcoloractuator.dto.HSBColorActuatorServiceState;
26 import org.openhab.binding.boschshc.internal.services.multilevelswitch.MultiLevelSwitchService;
27 import org.openhab.binding.boschshc.internal.services.multilevelswitch.dto.MultiLevelSwitchServiceState;
28 import org.openhab.core.library.types.HSBType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.util.ColorUtil;
37 * Handler for smart light bulbs connected via Zigbee, e.g. Ledvance Smart+ bulbs
39 * @author David Pace - Initial contribution
43 public class SmartBulbHandler extends BoschSHCDeviceHandler {
45 private BinarySwitchService binarySwitchService;
46 private HSBColorActuatorService hsbColorActuatorService;
47 private MultiLevelSwitchService multiLevelSwitchService;
49 public SmartBulbHandler(Thing thing) {
52 this.binarySwitchService = new BinarySwitchService();
53 this.multiLevelSwitchService = new MultiLevelSwitchService();
54 this.hsbColorActuatorService = new HSBColorActuatorService();
58 protected void initializeServices() throws BoschSHCException {
59 super.initializeServices();
61 this.registerService(binarySwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH), true);
62 this.registerService(multiLevelSwitchService, this::updateChannels, List.of(CHANNEL_BRIGHTNESS), true);
63 this.registerService(hsbColorActuatorService, this::updateChannels, List.of(CHANNEL_COLOR), true);
67 public void handleCommand(ChannelUID channelUID, Command command) {
68 super.handleCommand(channelUID, command);
70 switch (channelUID.getId()) {
71 case CHANNEL_POWER_SWITCH:
72 if (command instanceof OnOffType onOffCommand) {
73 updateBinarySwitchState(onOffCommand);
76 case CHANNEL_BRIGHTNESS:
77 if (command instanceof PercentType percentCommand) {
78 updateMultiLevelSwitchState(percentCommand);
82 if (command instanceof HSBType hsbCommand) {
83 updateColorState(hsbCommand);
89 private void updateBinarySwitchState(OnOffType onOffCommand) {
90 BinarySwitchServiceState serviceState = new BinarySwitchServiceState();
91 serviceState.on = onOffCommand == OnOffType.ON;
92 this.updateServiceState(binarySwitchService, serviceState);
95 private void updateMultiLevelSwitchState(PercentType percentCommand) {
96 MultiLevelSwitchServiceState serviceState = new MultiLevelSwitchServiceState();
97 serviceState.level = percentCommand.intValue();
98 this.updateServiceState(multiLevelSwitchService, serviceState);
101 private void updateColorState(HSBType hsbCommand) {
102 HSBColorActuatorServiceState serviceState = new HSBColorActuatorServiceState();
103 serviceState.rgb = ColorUtil.hsbTosRgb(hsbCommand);
104 this.updateServiceState(hsbColorActuatorService, serviceState);
107 private void updateChannels(BinarySwitchServiceState serviceState) {
108 super.updateState(CHANNEL_POWER_SWITCH, serviceState.toOnOffType());
111 private void updateChannels(MultiLevelSwitchServiceState serviceState) {
112 super.updateState(CHANNEL_BRIGHTNESS, serviceState.toPercentType());
115 private void updateChannels(HSBColorActuatorServiceState serviceState) {
116 super.updateState(CHANNEL_COLOR, serviceState.toHSBType());