]> git.basschouten.com Git - openhab-addons.git/blob
5d07539cd8d41a8dc468e414d2d766d1394a08e8
[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.evohome.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.evohome.internal.EvohomeBindingConstants;
18 import org.openhab.binding.evohome.internal.api.models.v2.dto.response.GatewayStatus;
19 import org.openhab.binding.evohome.internal.api.models.v2.dto.response.TemperatureControlSystemStatus;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27
28 /**
29  * Handler for a temperature control system. Gets and sets global system mode.
30  *
31  * @author Jasper van Zuijlen - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class EvohomeTemperatureControlSystemHandler extends BaseEvohomeHandler {
36     private @Nullable GatewayStatus gatewayStatus;
37     private @Nullable TemperatureControlSystemStatus tcsStatus;
38
39     public EvohomeTemperatureControlSystemHandler(Thing thing) {
40         super(thing);
41     }
42
43     @Override
44     public void initialize() {
45         super.initialize();
46     }
47
48     public void update(@Nullable GatewayStatus gatewayStatus, @Nullable TemperatureControlSystemStatus tcsStatus) {
49         this.gatewayStatus = gatewayStatus;
50         this.tcsStatus = tcsStatus;
51
52         if (tcsStatus == null || gatewayStatus == null) {
53             updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
54                     "Status not found, check the display id");
55         } else if (!handleActiveFaults(gatewayStatus)) {
56             updateEvohomeThingStatus(ThingStatus.ONLINE);
57             updateState(EvohomeBindingConstants.DISPLAY_SYSTEM_MODE_CHANNEL,
58                     new StringType(tcsStatus.getMode().getMode()));
59         }
60     }
61
62     @Override
63     public void handleCommand(ChannelUID channelUID, Command command) {
64         if (command == RefreshType.REFRESH) {
65             update(gatewayStatus, tcsStatus);
66         } else if (channelUID.getId().equals(EvohomeBindingConstants.DISPLAY_SYSTEM_MODE_CHANNEL)) {
67             EvohomeAccountBridgeHandler bridge = getEvohomeBridge();
68             if (bridge != null) {
69                 bridge.setTcsMode(getEvohomeThingConfig().id, command.toString());
70             }
71         }
72     }
73
74     private boolean handleActiveFaults(GatewayStatus gatewayStatus) {
75         if (gatewayStatus.hasActiveFaults()) {
76             updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
77                     gatewayStatus.getActiveFault(0).getFaultType());
78             return true;
79         }
80         return false;
81     }
82 }