]> git.basschouten.com Git - openhab-addons.git/blob
1d584c9ef921181e86cbacc698eadd677102c4c5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.climatecontrol;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_SETPOINT_TEMPERATURE;
16 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_TEMPERATURE;
17
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.boschshc.internal.devices.BoschSHCDeviceHandler;
22 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
23 import org.openhab.binding.boschshc.internal.services.roomclimatecontrol.RoomClimateControlService;
24 import org.openhab.binding.boschshc.internal.services.roomclimatecontrol.dto.RoomClimateControlServiceState;
25 import org.openhab.binding.boschshc.internal.services.temperaturelevel.TemperatureLevelService;
26 import org.openhab.binding.boschshc.internal.services.temperaturelevel.dto.TemperatureLevelServiceState;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.types.Command;
32
33 /**
34  * A virtual device which controls up to six Bosch Smart Home radiator thermostats in a room.
35  * 
36  * @author Christian Oeing - Initial contribution
37  */
38 @NonNullByDefault
39 public final class ClimateControlHandler extends BoschSHCDeviceHandler {
40
41     private RoomClimateControlService roomClimateControlService;
42
43     /**
44      * Constructor.
45      * 
46      * @param thing The Bosch Smart Home device that should be handled.
47      */
48     public ClimateControlHandler(Thing thing) {
49         super(thing);
50         this.roomClimateControlService = new RoomClimateControlService();
51     }
52
53     @Override
54     protected void initializeServices() throws BoschSHCException {
55         super.createService(TemperatureLevelService::new, this::updateChannels, List.of(CHANNEL_TEMPERATURE));
56         super.registerService(this.roomClimateControlService, this::updateChannels,
57                 List.of(CHANNEL_SETPOINT_TEMPERATURE));
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         super.handleCommand(channelUID, command);
63         switch (channelUID.getId()) {
64             case CHANNEL_SETPOINT_TEMPERATURE:
65                 if (command instanceof QuantityType<?>) {
66                     updateSetpointTemperature((QuantityType<?>) command);
67                 }
68                 break;
69         }
70     }
71
72     /**
73      * Updates the channels which are linked to the {@link TemperatureLevelService} of the device.
74      * 
75      * @param state Current state of {@link TemperatureLevelService}.
76      */
77     private void updateChannels(TemperatureLevelServiceState state) {
78         super.updateState(CHANNEL_TEMPERATURE, state.getTemperatureState());
79     }
80
81     /**
82      * Updates the channels which are linked to the {@link RoomClimateControlService} of the device.
83      * 
84      * @param state Current state of {@link RoomClimateControlService}.
85      */
86     private void updateChannels(RoomClimateControlServiceState state) {
87         super.updateState(CHANNEL_SETPOINT_TEMPERATURE, state.getSetpointTemperatureState());
88     }
89
90     /**
91      * Sets the desired temperature for the device.
92      * 
93      * @param quantityType Command which contains the new desired temperature.
94      */
95     private void updateSetpointTemperature(QuantityType<?> quantityType) {
96         QuantityType<?> celsiusType = quantityType.toUnit(SIUnits.CELSIUS);
97         if (celsiusType == null) {
98             logger.debug("Could not convert quantity command to celsius");
99             return;
100         }
101
102         double setpointTemperature = celsiusType.doubleValue();
103         this.updateServiceState(this.roomClimateControlService,
104                 new RoomClimateControlServiceState(setpointTemperature));
105     }
106 }