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