]> git.basschouten.com Git - openhab-addons.git/blob
4170dc4add339b4f31de6d16d6e44625cb2f53e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.warmup.internal.handler;
14
15 import static org.openhab.binding.warmup.internal.WarmupBindingConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.warmup.internal.api.MyWarmupApiException;
22 import org.openhab.binding.warmup.internal.model.query.LocationDTO;
23 import org.openhab.binding.warmup.internal.model.query.QueryResponseDTO;
24 import org.openhab.binding.warmup.internal.model.query.RoomDTO;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author James Melville - Initial contribution
38  */
39 @NonNullByDefault
40 public class RoomHandler extends WarmupThingHandler implements WarmupRefreshListener {
41
42     private final Logger logger = LoggerFactory.getLogger(RoomHandler.class);
43     private @Nullable RoomConfigurationDTO config;
44
45     public RoomHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void initialize() {
51         super.initialize();
52         config = getConfigAs(RoomConfigurationDTO.class);
53         if (config.getSerialNumber().length() == 0) {
54             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Serial Number not configured");
55         } else {
56             super.refreshFromServer();
57         }
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         super.handleCommand(channelUID, command);
63         if (CHANNEL_TARGET_TEMPERATURE.equals(channelUID.getId())
64                 && command instanceof QuantityType<?> quantityCommand) {
65             setOverride(quantityCommand);
66         }
67         if (CHANNEL_FROST_PROTECTION_MODE.equals(channelUID.getId()) && command instanceof OnOffType onOffCommand) {
68             toggleFrostProtectionMode(onOffCommand);
69         }
70     }
71
72     /**
73      * Process device list and populate room properties, status and state
74      *
75      * @param domain Data model representing all devices
76      */
77     @Override
78     public void refresh(@Nullable QueryResponseDTO domain) {
79         if (domain == null) {
80             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "No data from bridge");
81         } else if (config != null) {
82             final String serialNumber = config.getSerialNumber();
83             for (LocationDTO location : domain.getData().getUser().getLocations()) {
84                 for (RoomDTO room : location.getRooms()) {
85                     if (room.getThermostat4ies() != null && !room.getThermostat4ies().isEmpty()
86                             && room.getThermostat4ies().get(0).getDeviceSN().equals(serialNumber)) {
87                         if (room.getThermostat4ies().get(0).getLastPoll() > 10) {
88                             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
89                                     "Thermostat has not polled for 10 minutes");
90                         } else {
91                             updateStatus(ThingStatus.ONLINE);
92
93                             updateProperty(PROPERTY_ROOM_ID, room.getId());
94                             updateProperty(PROPERTY_ROOM_NAME, room.getName());
95                             updateProperty(PROPERTY_LOCATION_ID, location.getId());
96                             updateProperty(PROPERTY_LOCATION_NAME, location.getName());
97
98                             updateState(CHANNEL_CURRENT_TEMPERATURE, parseTemperature(room.getCurrentTemperature()));
99                             updateState(CHANNEL_TARGET_TEMPERATURE, parseTemperature(room.getTargetTemperature()));
100                             updateState(CHANNEL_OVERRIDE_DURATION, parseDuration(room.getOverrideDuration()));
101                             updateState(CHANNEL_RUN_MODE, parseString(room.getRunMode()));
102                             updateState(CHANNEL_FROST_PROTECTION_MODE,
103                                     OnOffType.from(room.getRunMode().equals(FROST_PROTECTION_MODE)));
104                         }
105                         return;
106                     }
107                 }
108             }
109             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Room not found");
110         } else {
111             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Room not configured");
112         }
113     }
114
115     private void setOverride(final QuantityType<?> command) {
116         String roomId = getThing().getProperties().get(PROPERTY_ROOM_ID);
117         String locationId = getThing().getProperties().get(PROPERTY_LOCATION_ID);
118
119         QuantityType<?> temp = command.toUnit(SIUnits.CELSIUS);
120
121         if (temp != null) {
122             final int value = temp.multiply(BigDecimal.TEN).intValue();
123
124             try {
125                 final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
126                 if (bridgeHandler != null && config != null) {
127                     final int overrideDuration = config.getOverrideDuration();
128                     if (overrideDuration > 0 && locationId != null && roomId != null) {
129                         bridgeHandler.getApi().setOverride(locationId, roomId, value, overrideDuration);
130                         refreshFromServer();
131                     }
132                 }
133             } catch (MyWarmupApiException e) {
134                 logger.debug("Set Override failed: {}", e.getMessage());
135             }
136         }
137     }
138
139     private void toggleFrostProtectionMode(OnOffType command) {
140         String roomId = getThing().getProperties().get(PROPERTY_ROOM_ID);
141         String locationId = getThing().getProperties().get(PROPERTY_LOCATION_ID);
142         try {
143             final MyWarmupAccountHandler bridgeHandler = getBridgeHandler();
144             if (bridgeHandler != null && locationId != null && roomId != null) {
145                 bridgeHandler.getApi().toggleFrostProtectionMode(locationId, roomId, command);
146                 refreshFromServer();
147             }
148         } catch (MyWarmupApiException e) {
149             logger.debug("Toggle Frost Protection failed: {}", e.getMessage());
150         }
151     }
152 }