]> git.basschouten.com Git - openhab-addons.git/blob
3a8994563097d1c1c6e3ef86d84b77111d308907
[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.binding.boschshc.internal.devices.smartbulb;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
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;
35
36 /**
37  * Handler for smart light bulbs connected via Zigbee, e.g. Ledvance Smart+ bulbs
38  *
39  * @author David Pace - Initial contribution
40  *
41  */
42 @NonNullByDefault
43 public class SmartBulbHandler extends BoschSHCDeviceHandler {
44
45     private BinarySwitchService binarySwitchService;
46     private HSBColorActuatorService hsbColorActuatorService;
47     private MultiLevelSwitchService multiLevelSwitchService;
48
49     public SmartBulbHandler(Thing thing) {
50         super(thing);
51
52         this.binarySwitchService = new BinarySwitchService();
53         this.multiLevelSwitchService = new MultiLevelSwitchService();
54         this.hsbColorActuatorService = new HSBColorActuatorService();
55     }
56
57     @Override
58     protected void initializeServices() throws BoschSHCException {
59         super.initializeServices();
60
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);
64     }
65
66     @Override
67     public void handleCommand(ChannelUID channelUID, Command command) {
68         super.handleCommand(channelUID, command);
69
70         switch (channelUID.getId()) {
71             case CHANNEL_POWER_SWITCH:
72                 if (command instanceof OnOffType onOffCommand) {
73                     updateBinarySwitchState(onOffCommand);
74                 }
75                 break;
76             case CHANNEL_BRIGHTNESS:
77                 if (command instanceof PercentType percentCommand) {
78                     updateMultiLevelSwitchState(percentCommand);
79                 }
80                 break;
81             case CHANNEL_COLOR:
82                 if (command instanceof HSBType hsbCommand) {
83                     updateColorState(hsbCommand);
84                 }
85                 break;
86         }
87     }
88
89     private void updateBinarySwitchState(OnOffType onOffCommand) {
90         BinarySwitchServiceState serviceState = new BinarySwitchServiceState();
91         serviceState.on = onOffCommand == OnOffType.ON;
92         this.updateServiceState(binarySwitchService, serviceState);
93     }
94
95     private void updateMultiLevelSwitchState(PercentType percentCommand) {
96         MultiLevelSwitchServiceState serviceState = new MultiLevelSwitchServiceState();
97         serviceState.level = percentCommand.intValue();
98         this.updateServiceState(multiLevelSwitchService, serviceState);
99     }
100
101     private void updateColorState(HSBType hsbCommand) {
102         HSBColorActuatorServiceState serviceState = new HSBColorActuatorServiceState();
103         serviceState.rgb = ColorUtil.hsbTosRgb(hsbCommand);
104         this.updateServiceState(hsbColorActuatorService, serviceState);
105     }
106
107     private void updateChannels(BinarySwitchServiceState serviceState) {
108         super.updateState(CHANNEL_POWER_SWITCH, serviceState.toOnOffType());
109     }
110
111     private void updateChannels(MultiLevelSwitchServiceState serviceState) {
112         super.updateState(CHANNEL_BRIGHTNESS, serviceState.toPercentType());
113     }
114
115     private void updateChannels(HSBColorActuatorServiceState serviceState) {
116         super.updateState(CHANNEL_COLOR, serviceState.toHSBType());
117     }
118 }