]> git.basschouten.com Git - openhab-addons.git/blob
1fb6d43456ccb0d61842481358649b82bb4102e3
[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.ZoneStatus;
17 import org.openhab.core.library.types.DecimalType;
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  * The {@link EvohomeHeatingZoneHandler} is responsible for handling commands, which are
28  * sent to one of the channels.
29  *
30  * @author Jasper van Zuijlen - Initial contribution
31  * @author Neil Renaud - Working implementation
32  * @author Jasper van Zuijlen - Refactor + Permanent Zone temperature setting
33  */
34 public class EvohomeHeatingZoneHandler extends BaseEvohomeHandler {
35
36     private static final int CANCEL_SET_POINT_OVERRIDE = 0;
37     private ThingStatus tcsStatus;
38     private ZoneStatus zoneStatus;
39
40     public EvohomeHeatingZoneHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void initialize() {
46         super.initialize();
47     }
48
49     public void update(ThingStatus tcsStatus, ZoneStatus zoneStatus) {
50         this.tcsStatus = tcsStatus;
51         this.zoneStatus = zoneStatus;
52
53         // Make the zone offline when the related display is offline
54         // If the related display is not a thing, ignore this
55         if (tcsStatus != null && tcsStatus.equals(ThingStatus.OFFLINE)) {
56             updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
57                     "Display Controller offline");
58         } else if (zoneStatus == null) {
59             updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
60                     "Status not found, check the zone id");
61         } else if (!handleActiveFaults(zoneStatus)) {
62             updateEvohomeThingStatus(ThingStatus.ONLINE);
63
64             updateState(EvohomeBindingConstants.ZONE_TEMPERATURE_CHANNEL,
65                     new DecimalType(zoneStatus.getTemperature().getTemperature()));
66             updateState(EvohomeBindingConstants.ZONE_SET_POINT_STATUS_CHANNEL,
67                     new StringType(zoneStatus.getHeatSetpoint().getSetpointMode()));
68             updateState(EvohomeBindingConstants.ZONE_SET_POINT_CHANNEL,
69                     new DecimalType(zoneStatus.getHeatSetpoint().getTargetTemperature()));
70         }
71     }
72
73     @Override
74     public void handleCommand(ChannelUID channelUID, Command command) {
75         if (command == RefreshType.REFRESH) {
76             update(tcsStatus, zoneStatus);
77         } else {
78             EvohomeAccountBridgeHandler bridge = getEvohomeBridge();
79             if (bridge != null) {
80                 String channelId = channelUID.getId();
81                 if (EvohomeBindingConstants.ZONE_SET_POINT_CHANNEL.equals(channelId)
82                         && command instanceof DecimalType) {
83                     double newTemp = ((DecimalType) command).doubleValue();
84                     if (newTemp == CANCEL_SET_POINT_OVERRIDE) {
85                         bridge.cancelSetPointOverride(getEvohomeThingConfig().id);
86                     } else if (newTemp < 5) {
87                         newTemp = 5;
88                     }
89                     if (newTemp >= 5 && newTemp <= 35) {
90                         bridge.setPermanentSetPoint(getEvohomeThingConfig().id, newTemp);
91                     }
92                 }
93             }
94         }
95     }
96
97     private boolean handleActiveFaults(ZoneStatus zoneStatus) {
98         if (zoneStatus.hasActiveFaults()) {
99             updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
100                     zoneStatus.getActiveFault(0).getFaultType());
101             return true;
102         }
103         return false;
104     }
105 }