]> git.basschouten.com Git - openhab-addons.git/blob
d02a8d42fbe10d4969f2cc2b76e3fb4a38bb2a5f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
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;
33
34 /**
35  * Abstract handler implementation for devices with power switches and energy monitoring.
36  * <p>
37  * This implementation provides the functionality to
38  * <ul>
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>
41  * service</li>
42  * </ul>
43  *
44  * @author David Pace - Initial contribution (extracted from LightControlHandler)
45  */
46 @NonNullByDefault
47 public abstract class AbstractPowerSwitchHandler extends BoschSHCDeviceHandler {
48
49     /**
50      * Service for switching the device on and off
51      */
52     private final PowerSwitchService powerSwitchService;
53
54     protected AbstractPowerSwitchHandler(Thing thing) {
55         super(thing);
56         this.powerSwitchService = new PowerSwitchService();
57     }
58
59     @Override
60     protected void initializeServices() throws BoschSHCException {
61         super.initializeServices();
62
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);
66     }
67
68     @Override
69     public void handleCommand(ChannelUID channelUID, Command command) {
70         super.handleCommand(channelUID, command);
71
72         switch (channelUID.getId()) {
73             case CHANNEL_POWER_SWITCH:
74                 if (command instanceof OnOffType onOffCommand) {
75                     updatePowerSwitchState(onOffCommand);
76                 }
77                 break;
78         }
79     }
80
81     /**
82      * Updates the channels which are linked to the {@link PowerMeterService} of the device.
83      *
84      * @param state Current state of {@link PowerMeterService}.
85      */
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));
89     }
90
91     /**
92      * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
93      *
94      * @param state Current state of {@link PowerSwitchService}.
95      */
96     private void updateChannels(PowerSwitchServiceState state) {
97         State powerState = OnOffType.from(state.switchState.toString());
98         super.updateState(CHANNEL_POWER_SWITCH, powerState);
99     }
100
101     private void updatePowerSwitchState(OnOffType command) {
102         PowerSwitchServiceState state = new PowerSwitchServiceState();
103         state.switchState = PowerSwitchState.valueOf(command.toFullString());
104         this.updateServiceState(this.powerSwitchService, state);
105     }
106 }