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