]> git.basschouten.com Git - openhab-addons.git/blob
038e24009df5024e18228edefe6b9e8845003cb5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.lightcontrol;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
19 import javax.measure.quantity.Energy;
20 import javax.measure.quantity.Power;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
24 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
25 import org.openhab.binding.boschshc.internal.services.powermeter.PowerMeterService;
26 import org.openhab.binding.boschshc.internal.services.powermeter.dto.PowerMeterServiceState;
27 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchService;
28 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
29 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.State;
37
38 /**
39  * A simple light control.
40  *
41  * @author Stefan Kästle - Initial contribution
42  */
43 @NonNullByDefault
44 public class LightControlHandler extends BoschSHCHandler {
45
46     private final PowerSwitchService powerSwitchService;
47
48     public LightControlHandler(Thing thing) {
49         super(thing);
50         this.powerSwitchService = new PowerSwitchService();
51     }
52
53     @Override
54     protected void initializeServices() throws BoschSHCException {
55         super.initializeServices();
56
57         this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH));
58         this.createService(PowerMeterService::new, this::updateChannels,
59                 List.of(CHANNEL_POWER_CONSUMPTION, CHANNEL_ENERGY_CONSUMPTION));
60     }
61
62     @Override
63     public void handleCommand(ChannelUID channelUID, Command command) {
64         super.handleCommand(channelUID, command);
65
66         switch (channelUID.getId()) {
67             case CHANNEL_POWER_SWITCH:
68                 if (command instanceof OnOffType) {
69                     updatePowerSwitchState((OnOffType) command);
70                 }
71                 break;
72         }
73     }
74
75     /**
76      * Updates the channels which are linked to the {@link PowerMeterService} of the device.
77      * 
78      * @param state Current state of {@link PowerMeterService}.
79      */
80     private void updateChannels(PowerMeterServiceState state) {
81         super.updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<Power>(state.powerConsumption, Units.WATT));
82         super.updateState(CHANNEL_ENERGY_CONSUMPTION,
83                 new QuantityType<Energy>(state.energyConsumption, Units.WATT_HOUR));
84     }
85
86     /**
87      * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
88      * 
89      * @param state Current state of {@link PowerSwitchService}.
90      */
91     private void updateChannels(PowerSwitchServiceState state) {
92         State powerState = OnOffType.from(state.switchState.toString());
93         super.updateState(CHANNEL_POWER_SWITCH, powerState);
94     }
95
96     private void updatePowerSwitchState(OnOffType command) {
97         PowerSwitchServiceState state = new PowerSwitchServiceState();
98         state.switchState = PowerSwitchState.valueOf(command.toFullString());
99         this.updateServiceState(this.powerSwitchService, state);
100     }
101 }