2 * Copyright (c) 2010-2021 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.inwallswitch;
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
17 import java.util.List;
19 import javax.measure.quantity.Energy;
20 import javax.measure.quantity.Power;
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;
38 import com.google.gson.JsonElement;
41 * Represents Bosch in-wall switches.
43 * @author Stefan Kästle - Initial contribution
46 public class BoschInWallSwitchHandler extends BoschSHCHandler {
48 private final PowerSwitchService powerSwitchService;
50 public BoschInWallSwitchHandler(Thing thing) {
52 this.powerSwitchService = new PowerSwitchService();
56 protected void initializeServices() throws BoschSHCException {
57 super.initializeServices();
59 this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH));
63 public void handleCommand(ChannelUID channelUID, Command command) {
64 super.handleCommand(channelUID, command);
66 logger.debug("Handle command for: {} - {}", channelUID.getThingUID(), command);
68 if (command instanceof RefreshType) {
69 switch (channelUID.getId()) {
70 case CHANNEL_POWER_CONSUMPTION: {
71 PowerMeterState state = this.getState("PowerMeter", PowerMeterState.class);
73 updatePowerMeterState(state);
77 case CHANNEL_ENERGY_CONSUMPTION:
78 // Nothing to do here, since the same update is received from POWER_CONSUMPTION
81 logger.warn("Received refresh request for unsupported channel: {}", channelUID);
84 switch (channelUID.getId()) {
85 case CHANNEL_POWER_SWITCH:
86 if (command instanceof OnOffType) {
87 updatePowerSwitchState((OnOffType) command);
94 void updatePowerMeterState(PowerMeterState state) {
95 logger.debug("Parsed power meter state of {}: energy {} - power {}", this.getBoschID(), state.energyConsumption,
96 state.energyConsumption);
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));
103 * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
105 * @param state Current state of {@link PowerSwitchService}.
107 private void updateChannels(PowerSwitchServiceState state) {
108 State powerState = OnOffType.from(state.switchState.toString());
109 super.updateState(CHANNEL_POWER_SWITCH, powerState);
112 private void updatePowerSwitchState(OnOffType command) {
113 PowerSwitchServiceState state = new PowerSwitchServiceState();
114 state.switchState = PowerSwitchState.valueOf(command.toFullString());
115 this.updateServiceState(this.powerSwitchService, state);
119 public void processUpdate(String id, JsonElement state) {
120 super.processUpdate(id, state);
122 logger.debug("in-wall switch: received update: ID {} state {}", id, state);
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);
129 updatePowerMeterState(powerMeterState);