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