]> git.basschouten.com Git - openhab-addons.git/blob
97b6d4bd4247361218f0ec2f1c19b47ccc88ecab
[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.millheat.internal.handler;
14
15 import static org.openhab.binding.millheat.internal.MillheatBindingConstants.*;
16
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.millheat.internal.config.MillheatRoomConfiguration;
21 import org.openhab.binding.millheat.internal.model.MillheatModel;
22 import org.openhab.binding.millheat.internal.model.ModeType;
23 import org.openhab.binding.millheat.internal.model.Room;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
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.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link MillheatRoomHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Arne Seime - Initial contribution
43  */
44 @NonNullByDefault
45 public class MillheatRoomHandler extends MillheatBaseThingHandler {
46     private final Logger logger = LoggerFactory.getLogger(MillheatRoomHandler.class);
47     private @NonNullByDefault({}) MillheatRoomConfiguration config;
48
49     public MillheatRoomHandler(final Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void handleCommand(final ChannelUID channelUID, final Command command) {
55         handleCommand(channelUID, command, getMillheatModel());
56     }
57
58     private void updateRoomTemperature(final Long roomId, final Command command, final ModeType modeType) {
59         getAccountHandler().ifPresent(handler -> {
60             handler.updateRoomTemperature(config.roomId, command, modeType);
61         });
62     }
63
64     @Override
65     protected void handleCommand(final ChannelUID channelUID, final Command command, final MillheatModel model) {
66         final Optional<Room> optionalRoom = model.findRoomById(config.roomId);
67         if (optionalRoom.isPresent()) {
68             updateStatus(ThingStatus.ONLINE);
69             final Room room = optionalRoom.get();
70             if (CHANNEL_CURRENT_TEMPERATURE.equals(channelUID.getId())) {
71                 if (command instanceof RefreshType) {
72                     updateState(channelUID, new QuantityType<>(room.getCurrentTemp(), SIUnits.CELSIUS));
73                 }
74             } else if (CHANNEL_CURRENT_MODE.equals(channelUID.getId())) {
75                 if (command instanceof RefreshType) {
76                     updateState(channelUID, new StringType(room.getMode().toString()));
77                 }
78             } else if (CHANNEL_PROGRAM.equals(channelUID.getId())) {
79                 if (command instanceof RefreshType) {
80                     updateState(channelUID, new StringType(room.getRoomProgramName()));
81                 }
82             } else if (CHANNEL_COMFORT_TEMPERATURE.equals(channelUID.getId())) {
83                 if (command instanceof RefreshType) {
84                     updateState(channelUID, new QuantityType<>(room.getComfortTemp(), SIUnits.CELSIUS));
85                 } else {
86                     updateRoomTemperature(config.roomId, command, ModeType.COMFORT);
87                 }
88             } else if (CHANNEL_SLEEP_TEMPERATURE.equals(channelUID.getId())) {
89                 if (command instanceof RefreshType) {
90                     updateState(channelUID, new QuantityType<>(room.getSleepTemp(), SIUnits.CELSIUS));
91                 } else {
92                     updateRoomTemperature(config.roomId, command, ModeType.SLEEP);
93                 }
94             } else if (CHANNEL_AWAY_TEMPERATURE.equals(channelUID.getId())) {
95                 if (command instanceof RefreshType) {
96                     updateState(channelUID, new QuantityType<>(room.getAwayTemp(), SIUnits.CELSIUS));
97                 } else {
98                     updateRoomTemperature(config.roomId, command, ModeType.AWAY);
99                 }
100             } else if (CHANNEL_TARGET_TEMPERATURE.equals(channelUID.getId())) {
101                 if (command instanceof RefreshType) {
102                     final Integer targetTemperature = room.getTargetTemperature();
103                     if (targetTemperature != null) {
104                         updateState(channelUID, new QuantityType<>(targetTemperature, SIUnits.CELSIUS));
105                     } else {
106                         updateState(channelUID, UnDefType.UNDEF);
107                     }
108                 }
109             } else if (CHANNEL_HEATING_ACTIVE.equals(channelUID.getId())) {
110                 if (command instanceof RefreshType) {
111                     updateState(channelUID, room.isHeatingActive() ? OnOffType.ON : OnOffType.OFF);
112                 }
113             } else {
114                 logger.debug("Received command {} on channel {}, but this channel is not handled or supported by {}",
115                         channelUID.getId(), command.toString(), this.getThing().getUID());
116             }
117         } else {
118             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
119         }
120     }
121
122     @Override
123     public void initialize() {
124         config = getConfigAs(MillheatRoomConfiguration.class);
125         logger.debug("Initializing Millheat room using config {}", config);
126         final Optional<Room> room = getMillheatModel().findRoomById(config.roomId);
127         if (room.isPresent()) {
128             updateStatus(ThingStatus.ONLINE);
129         } else {
130             updateStatus(ThingStatus.OFFLINE);
131         }
132     }
133 }