]> git.basschouten.com Git - openhab-addons.git/blob
18b67d95aa21282f0b12c15acb8fab6ebfe800e9
[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 java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.millheat.internal.MillheatBindingConstants;
22 import org.openhab.binding.millheat.internal.config.MillheatHeaterConfiguration;
23 import org.openhab.binding.millheat.internal.model.Heater;
24 import org.openhab.binding.millheat.internal.model.MillheatModel;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.builder.ChannelBuilder;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.openhab.core.types.UnDefType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link MillheatHeaterHandler} is responsible for handling commands, which are
44  * sent to one of the channels.
45  *
46  * @author Arne Seime - Initial contribution
47  */
48 @NonNullByDefault
49 public class MillheatHeaterHandler extends MillheatBaseThingHandler {
50     private final Logger logger = LoggerFactory.getLogger(MillheatHeaterHandler.class);
51     private @NonNullByDefault({}) MillheatHeaterConfiguration config;
52
53     public MillheatHeaterHandler(final Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     public void handleCommand(final ChannelUID channelUID, final Command command) {
59         handleCommand(channelUID, command, getMillheatModel());
60     }
61
62     @Override
63     protected void handleCommand(final ChannelUID channelUID, final Command command, final MillheatModel model) {
64         final Optional<Heater> optionalHeater = model.findHeaterByMacOrId(config.macAddress, config.heaterId);
65         if (optionalHeater.isPresent()) {
66             updateStatus(ThingStatus.ONLINE);
67             final Heater heater = optionalHeater.get();
68             if (MillheatBindingConstants.CHANNEL_CURRENT_TEMPERATURE.equals(channelUID.getId())) {
69                 if (command instanceof RefreshType) {
70                     updateState(channelUID, new QuantityType<>(heater.getCurrentTemp(), SIUnits.CELSIUS));
71                 }
72             } else if (MillheatBindingConstants.CHANNEL_HEATING_ACTIVE.equals(channelUID.getId())) {
73                 if (command instanceof RefreshType) {
74                     updateState(channelUID, OnOffType.from(heater.isHeatingActive()));
75                 }
76             } else if (MillheatBindingConstants.CHANNEL_FAN_ACTIVE.equals(channelUID.getId())) {
77                 if (command instanceof RefreshType) {
78                     updateState(channelUID, OnOffType.from(heater.fanActive()));
79                 } else if (heater.canChangeTemp() && heater.getRoom() == null) {
80                     updateIndependentHeaterProperties(null, null, command);
81                 } else {
82                     logger.debug("Heater {} cannot change temperature and is in a room", getThing().getUID());
83                 }
84             } else if (MillheatBindingConstants.CHANNEL_WINDOW_STATE.equals(channelUID.getId())) {
85                 if (command instanceof RefreshType) {
86                     updateState(channelUID, heater.windowOpen() ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
87                 }
88             } else if (MillheatBindingConstants.CHANNEL_INDEPENDENT.equals(channelUID.getId())) {
89                 if (command instanceof RefreshType) {
90                     updateState(channelUID, OnOffType.from(heater.getRoom() == null));
91                 }
92             } else if (MillheatBindingConstants.CHANNEL_CURRENT_POWER.equals(channelUID.getId())) {
93                 if (command instanceof RefreshType) {
94                     if (config.power != null) {
95                         if (heater.isHeatingActive()) {
96                             updateState(channelUID, new QuantityType<>(config.power, Units.WATT));
97                         } else {
98                             updateState(channelUID, new QuantityType<>(0, Units.WATT));
99                         }
100                     } else {
101                         updateState(channelUID, UnDefType.UNDEF);
102                         logger.debug(
103                                 "Cannot update power for heater as the nominal power has not been configured for thing {}",
104                                 getThing().getUID());
105                     }
106                 }
107             } else if (MillheatBindingConstants.CHANNEL_TARGET_TEMPERATURE.equals(channelUID.getId())) {
108                 if (command instanceof RefreshType) {
109                     if (heater.canChangeTemp() && heater.getTargetTemp() != null) {
110                         updateState(channelUID, new QuantityType<>(heater.getTargetTemp(), SIUnits.CELSIUS));
111                     } else if (heater.getRoom() != null) {
112                         final Integer targetTemperature = heater.getRoom().getTargetTemperature();
113                         if (targetTemperature != null) {
114                             updateState(channelUID, new QuantityType<>(targetTemperature, SIUnits.CELSIUS));
115                         } else {
116                             updateState(channelUID, UnDefType.UNDEF);
117                         }
118                     } else {
119                         logger.debug(
120                                 "Heater {} is neither connected to a room nor marked as standalone. Someting is wrong, heater data: {}",
121                                 getThing().getUID(), heater);
122                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
123                     }
124                 } else {
125                     if (heater.canChangeTemp() && heater.getRoom() == null) {
126                         updateIndependentHeaterProperties(command, null, null);
127                     }
128                 }
129             } else if (MillheatBindingConstants.CHANNEL_MASTER_SWITCH.equals(channelUID.getId())) {
130                 if (command instanceof RefreshType) {
131                     updateState(channelUID, OnOffType.from(heater.powerStatus()));
132                 } else {
133                     if (heater.canChangeTemp() && heater.getRoom() == null) {
134                         updateIndependentHeaterProperties(null, command, null);
135                     } else {
136                         // Just overwrite with old state
137                         updateState(channelUID, OnOffType.from(heater.powerStatus()));
138                     }
139                 }
140             } else {
141                 logger.debug("Received command {} on channel {}, but this channel is not handled or supported by {}",
142                         channelUID.getId(), command.toString(), this.getThing().getUID());
143             }
144         } else {
145             updateStatus(ThingStatus.OFFLINE);
146         }
147     }
148
149     private void updateIndependentHeaterProperties(@Nullable final Command temperatureCommand,
150             @Nullable final Command masterOnOffCommand, @Nullable final Command fanCommand) {
151         getAccountHandler().ifPresent(handler -> {
152             handler.updateIndependentHeaterProperties(config.macAddress, config.heaterId, temperatureCommand,
153                     masterOnOffCommand, fanCommand);
154         });
155     }
156
157     @Override
158     public void initialize() {
159         config = getConfigAs(MillheatHeaterConfiguration.class);
160         logger.debug("Initializing Millheat heater using config {}", config);
161         if (config.heaterId == null && config.macAddress == null) {
162             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
163         } else {
164             final Optional<Heater> heater = getMillheatModel().findHeaterByMacOrId(config.macAddress, config.heaterId);
165             if (heater.isPresent()) {
166                 addOptionalChannels(heater.get());
167                 updateStatus(ThingStatus.ONLINE);
168             } else {
169                 updateStatus(ThingStatus.OFFLINE);
170             }
171         }
172     }
173
174     private void addOptionalChannels(final Heater heater) {
175         final List<Channel> newChannels = new ArrayList<>();
176         newChannels.addAll(getThing().getChannels());
177         if (heater.canChangeTemp() && heater.getRoom() == null) {
178             // Add power switch channel
179             newChannels
180                     .add(ChannelBuilder
181                             .create(new ChannelUID(getThing().getUID(), MillheatBindingConstants.CHANNEL_MASTER_SWITCH),
182                                     "Switch")
183                             .withType(MillheatBindingConstants.CHANNEL_TYPE_MASTER_SWITCH_UID).build());
184             // Add independent heater target temperature
185             newChannels.add(ChannelBuilder
186                     .create(new ChannelUID(getThing().getUID(), MillheatBindingConstants.CHANNEL_TARGET_TEMPERATURE),
187                             "Number:Temperature")
188                     .withType(MillheatBindingConstants.CHANNEL_TYPE_TARGET_TEMPERATURE_HEATER_UID).build());
189         }
190
191         updateThing(editThing().withChannels(newChannels).build());
192     }
193 }