2 * Copyright (c) 2010-2023 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;
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.exceptions.BoschSHCException;
24 import org.openhab.binding.boschshc.internal.services.powermeter.PowerMeterService;
25 import org.openhab.binding.boschshc.internal.services.powermeter.dto.PowerMeterServiceState;
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.State;
38 * Abstract handler implementation for devices with power switches and energy monitoring.
40 * This implementation provides the functionality to
42 * <li>Switch the device on and off using the <code>PowerSwitch</code> service</li>
43 * <li>Measuring the current power consumption and the overall energy consumption using the <code>PowerMeter</code>
47 * @author David Pace - Initial contribution (extracted from LightControlHandler)
50 public abstract class AbstractPowerSwitchHandler extends BoschSHCDeviceHandler {
53 * Service for switching the device on and off
55 private final PowerSwitchService powerSwitchService;
57 protected AbstractPowerSwitchHandler(Thing thing) {
59 this.powerSwitchService = new PowerSwitchService();
63 protected void initializeServices() throws BoschSHCException {
64 super.initializeServices();
66 this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH), true);
67 this.createService(PowerMeterService::new, this::updateChannels,
68 List.of(CHANNEL_POWER_CONSUMPTION, CHANNEL_ENERGY_CONSUMPTION), true);
72 public void handleCommand(ChannelUID channelUID, Command command) {
73 super.handleCommand(channelUID, command);
75 switch (channelUID.getId()) {
76 case CHANNEL_POWER_SWITCH:
77 if (command instanceof OnOffType) {
78 updatePowerSwitchState((OnOffType) command);
85 * Updates the channels which are linked to the {@link PowerMeterService} of the device.
87 * @param state Current state of {@link PowerMeterService}.
89 private void updateChannels(PowerMeterServiceState state) {
90 super.updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<Power>(state.powerConsumption, Units.WATT));
91 super.updateState(CHANNEL_ENERGY_CONSUMPTION,
92 new QuantityType<Energy>(state.energyConsumption, Units.WATT_HOUR));
96 * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
98 * @param state Current state of {@link PowerSwitchService}.
100 private void updateChannels(PowerSwitchServiceState state) {
101 State powerState = OnOffType.from(state.switchState.toString());
102 super.updateState(CHANNEL_POWER_SWITCH, powerState);
105 private void updatePowerSwitchState(OnOffType command) {
106 PowerSwitchServiceState state = new PowerSwitchServiceState();
107 state.switchState = PowerSwitchState.valueOf(command.toFullString());
108 this.updateServiceState(this.powerSwitchService, state);