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