2 * Copyright (c) 2010-2024 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 org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
21 import org.openhab.binding.boschshc.internal.services.powermeter.PowerMeterService;
22 import org.openhab.binding.boschshc.internal.services.powermeter.dto.PowerMeterServiceState;
23 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchService;
24 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
25 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
35 * Abstract handler implementation for devices with power switches and energy monitoring.
37 * This implementation provides the functionality to
39 * <li>Switch the device on and off using the <code>PowerSwitch</code> service</li>
40 * <li>Measuring the current power consumption and the overall energy consumption using the <code>PowerMeter</code>
44 * @author David Pace - Initial contribution (extracted from LightControlHandler)
47 public abstract class AbstractPowerSwitchHandler extends BoschSHCDeviceHandler {
50 * Service for switching the device on and off
52 private final PowerSwitchService powerSwitchService;
54 protected AbstractPowerSwitchHandler(Thing thing) {
56 this.powerSwitchService = new PowerSwitchService();
60 protected void initializeServices() throws BoschSHCException {
61 super.initializeServices();
63 this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH), true);
64 this.createService(PowerMeterService::new, this::updateChannels,
65 List.of(CHANNEL_POWER_CONSUMPTION, CHANNEL_ENERGY_CONSUMPTION), true);
69 public void handleCommand(ChannelUID channelUID, Command command) {
70 super.handleCommand(channelUID, command);
72 switch (channelUID.getId()) {
73 case CHANNEL_POWER_SWITCH:
74 if (command instanceof OnOffType onOffCommand) {
75 updatePowerSwitchState(onOffCommand);
82 * Updates the channels which are linked to the {@link PowerMeterService} of the device.
84 * @param state Current state of {@link PowerMeterService}.
86 private void updateChannels(PowerMeterServiceState state) {
87 super.updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<>(state.powerConsumption, Units.WATT));
88 super.updateState(CHANNEL_ENERGY_CONSUMPTION, new QuantityType<>(state.energyConsumption, Units.WATT_HOUR));
92 * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
94 * @param state Current state of {@link PowerSwitchService}.
96 private void updateChannels(PowerSwitchServiceState state) {
97 State powerState = OnOffType.from(state.switchState.toString());
98 super.updateState(CHANNEL_POWER_SWITCH, powerState);
101 private void updatePowerSwitchState(OnOffType command) {
102 PowerSwitchServiceState state = new PowerSwitchServiceState();
103 state.switchState = PowerSwitchState.valueOf(command.toFullString());
104 this.updateServiceState(this.powerSwitchService, state);