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;
36 * Handler for smart light bulbs connected via Zigbee, e.g. Ledvance Smart+ bulbs
38 * @author David Pace - Initial contribution
42 public class SmartBulbHandler extends BoschSHCDeviceHandler {
44 private BinarySwitchService binarySwitchService;
45 private HSBColorActuatorService hsbColorActuatorService;
46 private MultiLevelSwitchService multiLevelSwitchService;
48 public SmartBulbHandler(Thing thing) {
51 this.binarySwitchService = new BinarySwitchService();
52 this.multiLevelSwitchService = new MultiLevelSwitchService();
53 this.hsbColorActuatorService = new HSBColorActuatorService();
57 protected void initializeServices() throws BoschSHCException {
58 super.initializeServices();
60 this.registerService(binarySwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH), true);
61 this.registerService(multiLevelSwitchService, this::updateChannels, List.of(CHANNEL_BRIGHTNESS), true);
62 this.registerService(hsbColorActuatorService, this::updateChannels, List.of(CHANNEL_COLOR), true);
66 public void handleCommand(ChannelUID channelUID, Command command) {
67 super.handleCommand(channelUID, command);
69 switch (channelUID.getId()) {
70 case CHANNEL_POWER_SWITCH:
71 if (command instanceof OnOffType) {
72 updateBinarySwitchState((OnOffType) command);
75 case CHANNEL_BRIGHTNESS:
76 if (command instanceof PercentType) {
77 updateMultiLevelSwitchState((PercentType) command);
81 if (command instanceof HSBType) {
82 updateColorState((HSBType) command);
88 private void updateBinarySwitchState(OnOffType command) {
89 BinarySwitchServiceState serviceState = new BinarySwitchServiceState();
90 serviceState.on = command == OnOffType.ON;
91 this.updateServiceState(binarySwitchService, serviceState);
94 private void updateMultiLevelSwitchState(PercentType command) {
95 MultiLevelSwitchServiceState serviceState = new MultiLevelSwitchServiceState();
96 serviceState.level = command.intValue();
97 this.updateServiceState(multiLevelSwitchService, serviceState);
100 private void updateColorState(HSBType command) {
101 HSBColorActuatorServiceState serviceState = new HSBColorActuatorServiceState();
102 serviceState.rgb = command.getRGB();
103 this.updateServiceState(hsbColorActuatorService, serviceState);
106 private void updateChannels(BinarySwitchServiceState serviceState) {
107 super.updateState(CHANNEL_POWER_SWITCH, serviceState.toOnOffType());
110 private void updateChannels(MultiLevelSwitchServiceState serviceState) {
111 super.updateState(CHANNEL_BRIGHTNESS, serviceState.toPercentType());
114 private void updateChannels(HSBColorActuatorServiceState serviceState) {
115 super.updateState(CHANNEL_COLOR, serviceState.toHSBType());