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.time.ZoneId;
18 import java.util.Optional;
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;
42 * The {@link MillheatHomeHandler} is responsible for handling home commands, for now vacation mode properties
44 * @author Arne Seime - Initial contribution
47 public class MillheatHomeHandler extends MillheatBaseThingHandler {
48 private final Logger logger = LoggerFactory.getLogger(MillheatHomeHandler.class);
49 private @NonNullByDefault({}) MillheatHomeConfiguration config;
51 public MillheatHomeHandler(final Thing thing) {
56 public void handleCommand(final ChannelUID channelUID, final Command command) {
57 handleCommand(channelUID, command, getMillheatModel());
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));
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);
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);
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())));
93 updateState(channelUID, UnDefType.UNDEF);
95 } else if (command instanceof DateTimeType) {
96 updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_START, command);
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())));
104 updateState(channelUID, UnDefType.UNDEF);
106 } else if (command instanceof DateTimeType) {
107 updateVacationModeProperty(home, SetHolidayParameterRequest.PROP_END, command);
110 logger.debug("Received command {} on channel {}, but this channel is not handled or supported by {}",
111 channelUID.getId(), command.toString(), this.getThing().getUID());
114 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE);
118 private void updateVacationModeProperty(Home home, String property, Command command) {
119 getAccountHandler().ifPresent(handler -> {
120 handler.updateVacationProperty(home, property, command);
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);
132 updateStatus(ThingStatus.OFFLINE);