]> git.basschouten.com Git - openhab-addons.git/blob
ae71d0cb25dd79b939ffbcde41d00151be6a3071
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.time.ZoneId;
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.millheat.internal.config.MillheatHomeConfiguration;
22 import org.openhab.binding.millheat.internal.dto.SetHolidayParameterRequest;
23 import org.openhab.binding.millheat.internal.model.Home;
24 import org.openhab.binding.millheat.internal.model.MillheatModel;
25 import org.openhab.binding.millheat.internal.model.ModeType;
26 import org.openhab.core.library.types.DateTimeType;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.unit.SIUnits;
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.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link MillheatHomeHandler} is responsible for handling home commands, for now vacation mode properties
43  *
44  * @author Arne Seime - Initial contribution
45  */
46 @NonNullByDefault
47 public class MillheatHomeHandler extends MillheatBaseThingHandler {
48     private final Logger logger = LoggerFactory.getLogger(MillheatHomeHandler.class);
49     private @NonNullByDefault({}) MillheatHomeConfiguration config;
50
51     public MillheatHomeHandler(final Thing thing) {
52         super(thing);
53     }
54
55     @Override
56     public void handleCommand(final ChannelUID channelUID, final Command command) {
57         handleCommand(channelUID, command, getMillheatModel());
58     }
59
60     @Override
61     protected void handleCommand(final ChannelUID channelUID, final Command command, final MillheatModel model) {
62         final Optional<Home> optionalHome = model.findHomeById(config.homeId);
63         if (optionalHome.isPresent()) {
64             updateStatus(ThingStatus.ONLINE);
65             final Home home = optionalHome.get();
66             if (CHANNEL_HOME_VACATION_TARGET_TEMPERATURE.equals(channelUID.getId())) {
67                 if (command instanceof RefreshType) {
68                     updateState(channelUID, new QuantityType<>(home.getHolidayTemp(), SIUnits.CELSIUS));
69                 } else if (command instanceof QuantityType<?>) {
70                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_TEMP, command);
71                 } else if (command instanceof DecimalType) {
72                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_TEMP,
73                             new QuantityType<>((DecimalType) command, SIUnits.CELSIUS));
74                 }
75             } else if (CHANNEL_HOME_VACATION_MODE.equals(channelUID.getId())) {
76                 if (command instanceof RefreshType) {
77                     updateState(channelUID, OnOffType.from(home.getMode().getMode() == ModeType.VACATION));
78                 } else if (command instanceof OnOffType) {
79                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_MODE, command);
80                 }
81             } else if (CHANNEL_HOME_VACATION_MODE_ADVANCED.equals(channelUID.getId())) {
82                 if (command instanceof RefreshType) {
83                     updateState(channelUID, OnOffType.from(home.isAdvancedVacationMode()));
84                 } else if (command instanceof OnOffType) {
85                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_MODE_ADVANCED, command);
86                 }
87             } else if (CHANNEL_HOME_VACATION_MODE_START.equals(channelUID.getId())) {
88                 if (command instanceof RefreshType) {
89                     if (home.getVacationModeStart() != null) {
90                         updateState(channelUID,
91                                 new DateTimeType(home.getVacationModeStart().atZone(ZoneId.systemDefault())));
92                     } else {
93                         updateState(channelUID, UnDefType.UNDEF);
94                     }
95                 } else if (command instanceof DateTimeType) {
96                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_START, command);
97                 }
98             } else if (CHANNEL_HOME_VACATION_MODE_END.equals(channelUID.getId())) {
99                 if (command instanceof RefreshType) {
100                     if (home.getVacationModeEnd() != null) {
101                         updateState(channelUID,
102                                 new DateTimeType(home.getVacationModeEnd().atZone(ZoneId.systemDefault())));
103                     } else {
104                         updateState(channelUID, UnDefType.UNDEF);
105                     }
106                 } else if (command instanceof DateTimeType) {
107                     updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_END, command);
108                 }
109             } else {
110                 logger.debug("Received command {} on channel {}, but this channel is not handled or supported by {}",
111                         channelUID.getId(), command.toString(), this.getThing().getUID());
112             }
113         } else {
114             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
115         }
116     }
117
118     private void updateVacationModeProperty(Home home, String property, Command command) {
119         getAccountHandler().ifPresent(handler -> {
120             handler.updateVacationProperty(home, property, command);
121         });
122     }
123
124     @Override
125     public void initialize() {
126         config = getConfigAs(MillheatHomeConfiguration.class);
127         logger.debug("Initializing Millheat home using config {}", config);
128         final Optional<Home> room = getMillheatModel().findHomeById(config.homeId);
129         if (room.isPresent()) {
130             updateStatus(ThingStatus.ONLINE);
131         } else {
132             updateStatus(ThingStatus.OFFLINE);
133         }
134     }
135 }