2 * Copyright (c) 2010-2023 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.millheat.internal.handler;
15 import static org.openhab.binding.millheat.internal.MillheatBindingConstants.*;
17 import java.util.Optional;
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;
39 * The {@link MillheatRoomHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Arne Seime - Initial contribution
45 public class MillheatRoomHandler extends MillheatBaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(MillheatRoomHandler.class);
47 private @NonNullByDefault({}) MillheatRoomConfiguration config;
49 public MillheatRoomHandler(final Thing thing) {
54 public void handleCommand(final ChannelUID channelUID, final Command command) {
55 handleCommand(channelUID, command, getMillheatModel());
58 private void updateRoomTemperature(final Long roomId, final Command command, final ModeType modeType) {
59 getAccountHandler().ifPresent(handler -> {
60 handler.updateRoomTemperature(config.roomId, command, modeType);
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));
74 } else if (CHANNEL_CURRENT_MODE.equals(channelUID.getId())) {
75 if (command instanceof RefreshType) {
76 updateState(channelUID, new StringType(room.getMode().toString()));
78 } else if (CHANNEL_PROGRAM.equals(channelUID.getId())) {
79 if (command instanceof RefreshType) {
80 updateState(channelUID, new StringType(room.getRoomProgramName()));
82 } else if (CHANNEL_COMFORT_TEMPERATURE.equals(channelUID.getId())) {
83 if (command instanceof RefreshType) {
84 updateState(channelUID, new QuantityType<>(room.getComfortTemp(), SIUnits.CELSIUS));
86 updateRoomTemperature(config.roomId, command, ModeType.COMFORT);
88 } else if (CHANNEL_SLEEP_TEMPERATURE.equals(channelUID.getId())) {
89 if (command instanceof RefreshType) {
90 updateState(channelUID, new QuantityType<>(room.getSleepTemp(), SIUnits.CELSIUS));
92 updateRoomTemperature(config.roomId, command, ModeType.SLEEP);
94 } else if (CHANNEL_AWAY_TEMPERATURE.equals(channelUID.getId())) {
95 if (command instanceof RefreshType) {
96 updateState(channelUID, new QuantityType<>(room.getAwayTemp(), SIUnits.CELSIUS));
98 updateRoomTemperature(config.roomId, command, ModeType.AWAY);
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));
106 updateState(channelUID, UnDefType.UNDEF);
109 } else if (CHANNEL_HEATING_ACTIVE.equals(channelUID.getId())) {
110 if (command instanceof RefreshType) {
111 updateState(channelUID, OnOffType.from(room.isHeatingActive()));
114 logger.debug("Received command {} on channel {}, but this channel is not handled or supported by {}",
115 channelUID.getId(), command.toString(), this.getThing().getUID());
118 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
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);
130 updateStatus(ThingStatus.OFFLINE);