]> git.basschouten.com Git - openhab-addons.git/blob
b5a4ebd449ca5841faeb0a583fbc8a844381c54c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.inwallswitch;
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.devices.inwallswitch.dto.PowerMeterState;
25 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
26 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchService;
27 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
28 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.openhab.core.types.State;
37
38 import com.google.gson.JsonElement;
39
40 /**
41  * Represents Bosch in-wall switches.
42  *
43  * @author Stefan Kästle - Initial contribution
44  */
45 @NonNullByDefault
46 public class BoschInWallSwitchHandler extends BoschSHCHandler {
47
48     private final PowerSwitchService powerSwitchService;
49
50     public BoschInWallSwitchHandler(Thing thing) {
51         super(thing);
52         this.powerSwitchService = new PowerSwitchService();
53     }
54
55     @Override
56     protected void initializeServices() throws BoschSHCException {
57         super.initializeServices();
58
59         this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH));
60     }
61
62     @Override
63     public void handleCommand(ChannelUID channelUID, Command command) {
64         super.handleCommand(channelUID, command);
65
66         logger.debug("Handle command for: {} - {}", channelUID.getThingUID(), command);
67
68         if (command instanceof RefreshType) {
69             switch (channelUID.getId()) {
70                 case CHANNEL_POWER_CONSUMPTION: {
71                     PowerMeterState state = this.getState("PowerMeter", PowerMeterState.class);
72                     if (state != null) {
73                         updatePowerMeterState(state);
74                     }
75                     break;
76                 }
77                 case CHANNEL_ENERGY_CONSUMPTION:
78                     // Nothing to do here, since the same update is received from POWER_CONSUMPTION
79                     break;
80                 default:
81                     logger.warn("Received refresh request for unsupported channel: {}", channelUID);
82             }
83         } else {
84             switch (channelUID.getId()) {
85                 case CHANNEL_POWER_SWITCH:
86                     if (command instanceof OnOffType) {
87                         updatePowerSwitchState((OnOffType) command);
88                     }
89                     break;
90             }
91         }
92     }
93
94     void updatePowerMeterState(PowerMeterState state) {
95         logger.debug("Parsed power meter state of {}: energy {} - power {}", this.getBoschID(), state.energyConsumption,
96                 state.energyConsumption);
97
98         updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<Power>(state.powerConsumption, Units.WATT));
99         updateState(CHANNEL_ENERGY_CONSUMPTION, new QuantityType<Energy>(state.energyConsumption, Units.WATT_HOUR));
100     }
101
102     /**
103      * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
104      * 
105      * @param state Current state of {@link PowerSwitchService}.
106      */
107     private void updateChannels(PowerSwitchServiceState state) {
108         State powerState = OnOffType.from(state.switchState.toString());
109         super.updateState(CHANNEL_POWER_SWITCH, powerState);
110     }
111
112     private void updatePowerSwitchState(OnOffType command) {
113         PowerSwitchServiceState state = new PowerSwitchServiceState();
114         state.switchState = PowerSwitchState.valueOf(command.toFullString());
115         this.updateServiceState(this.powerSwitchService, state);
116     }
117
118     @Override
119     public void processUpdate(String id, JsonElement state) {
120         super.processUpdate(id, state);
121
122         logger.debug("in-wall switch: received update: ID {} state {}", id, state);
123
124         if (id.equals("PowerMeter")) {
125             PowerMeterState powerMeterState = GSON.fromJson(state, PowerMeterState.class);
126             if (powerMeterState == null) {
127                 logger.warn("Received unknown update in in-wall switch: {}", state);
128             } else {
129                 updatePowerMeterState(powerMeterState);
130             }
131         }
132     }
133 }