2 * Copyright (c) 2010-2022 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.lightcontrol;
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.BoschSHCDeviceHandler;
24 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
25 import org.openhab.binding.boschshc.internal.services.powermeter.PowerMeterService;
26 import org.openhab.binding.boschshc.internal.services.powermeter.dto.PowerMeterServiceState;
27 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchService;
28 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
29 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.State;
39 * A simple light control.
41 * @author Stefan Kästle - Initial contribution
44 public class LightControlHandler extends BoschSHCDeviceHandler {
46 private final PowerSwitchService powerSwitchService;
48 public LightControlHandler(Thing thing) {
50 this.powerSwitchService = new PowerSwitchService();
54 protected void initializeServices() throws BoschSHCException {
55 super.initializeServices();
57 this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH));
58 this.createService(PowerMeterService::new, this::updateChannels,
59 List.of(CHANNEL_POWER_CONSUMPTION, CHANNEL_ENERGY_CONSUMPTION));
63 public void handleCommand(ChannelUID channelUID, Command command) {
64 super.handleCommand(channelUID, command);
66 switch (channelUID.getId()) {
67 case CHANNEL_POWER_SWITCH:
68 if (command instanceof OnOffType) {
69 updatePowerSwitchState((OnOffType) command);
76 * Updates the channels which are linked to the {@link PowerMeterService} of the device.
78 * @param state Current state of {@link PowerMeterService}.
80 private void updateChannels(PowerMeterServiceState state) {
81 super.updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<Power>(state.powerConsumption, Units.WATT));
82 super.updateState(CHANNEL_ENERGY_CONSUMPTION,
83 new QuantityType<Energy>(state.energyConsumption, Units.WATT_HOUR));
87 * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
89 * @param state Current state of {@link PowerSwitchService}.
91 private void updateChannels(PowerSwitchServiceState state) {
92 State powerState = OnOffType.from(state.switchState.toString());
93 super.updateState(CHANNEL_POWER_SWITCH, powerState);
96 private void updatePowerSwitchState(OnOffType command) {
97 PowerSwitchServiceState state = new PowerSwitchServiceState();
98 state.switchState = PowerSwitchState.valueOf(command.toFullString());
99 this.updateServiceState(this.powerSwitchService, state);