]> git.basschouten.com Git - openhab-addons.git/blob
38eebf6f67caffca38d279effcf5129df30a1272
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.nobohub.internal;
14
15 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_ACTIVE_WEEK_PROFILE;
16 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_ACTIVE_WEEK_PROFILE_NAME;
17 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_CALCULATED_WEEK_PROFILE_STATUS;
18 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_COMFORT_TEMPERATURE;
19 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_CURRENT_TEMPERATURE;
20 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.CHANNEL_ZONE_ECO_TEMPERATURE;
21 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_HOSTNAME;
22 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.PROPERTY_ZONE_ID;
23
24 import java.time.LocalDateTime;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.measure.quantity.Temperature;
30
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.nobohub.internal.model.NoboDataException;
34 import org.openhab.binding.nobohub.internal.model.WeekProfile;
35 import org.openhab.binding.nobohub.internal.model.WeekProfileStatus;
36 import org.openhab.binding.nobohub.internal.model.Zone;
37 import org.openhab.core.library.types.DecimalType;
38 import org.openhab.core.library.types.QuantityType;
39 import org.openhab.core.library.types.StringType;
40 import org.openhab.core.library.unit.SIUnits;
41 import org.openhab.core.thing.Bridge;
42 import org.openhab.core.thing.ChannelUID;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingStatus;
45 import org.openhab.core.thing.ThingStatusDetail;
46 import org.openhab.core.thing.binding.BaseThingHandler;
47 import org.openhab.core.types.Command;
48 import org.openhab.core.types.RefreshType;
49 import org.openhab.core.types.StateOption;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * Shows information about a named Zone in the Nobø Hub.
55  *
56  * @author Jørgen Austvik - Initial contribution
57  */
58 @NonNullByDefault
59 public class ZoneHandler extends BaseThingHandler {
60
61     private final Logger logger = LoggerFactory.getLogger(ZoneHandler.class);
62
63     private final WeekProfileStateDescriptionOptionsProvider weekProfileStateDescriptionOptionsProvider;
64
65     private final NoboHubTranslationProvider messages;
66
67     protected int id;
68
69     public ZoneHandler(Thing thing, NoboHubTranslationProvider messages,
70             WeekProfileStateDescriptionOptionsProvider weekProfileStateDescriptionOptionsProvider) {
71         super(thing);
72         this.messages = messages;
73         this.weekProfileStateDescriptionOptionsProvider = weekProfileStateDescriptionOptionsProvider;
74     }
75
76     public void onUpdate(Zone zone) {
77         logger.debug("Updating zone: {}", zone.getName());
78         updateStatus(ThingStatus.ONLINE);
79
80         QuantityType<Temperature> comfortTemperature = new QuantityType<>(zone.getComfortTemperature(),
81                 SIUnits.CELSIUS);
82         updateState(CHANNEL_ZONE_COMFORT_TEMPERATURE, comfortTemperature);
83         QuantityType<Temperature> ecoTemperature = new QuantityType<>(zone.getEcoTemperature(), SIUnits.CELSIUS);
84         updateState(CHANNEL_ZONE_ECO_TEMPERATURE, ecoTemperature);
85
86         Double temp = zone.getTemperature();
87         if (temp != null && !Double.isNaN(temp)) {
88             QuantityType<Temperature> currentTemperature = new QuantityType<>(temp, SIUnits.CELSIUS);
89             updateState(CHANNEL_ZONE_CURRENT_TEMPERATURE, currentTemperature);
90         }
91
92         int activeWeekProfileId = zone.getActiveWeekProfileId();
93         Bridge noboHub = getBridge();
94         if (null != noboHub) {
95             logger.debug("Updating zone: {} at hub bridge: {}", zone.getName(),
96                     noboHub.getStatusInfo().getStatus().name());
97             NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
98             if (hubHandler != null) {
99                 WeekProfile weekProfile = hubHandler.getWeekProfile(activeWeekProfileId);
100                 if (null != weekProfile) {
101                     updateState(CHANNEL_ZONE_ACTIVE_WEEK_PROFILE_NAME, StringType.valueOf(weekProfile.getName()));
102                     updateState(CHANNEL_ZONE_ACTIVE_WEEK_PROFILE,
103                             DecimalType.valueOf(String.valueOf(weekProfile.getId())));
104                     try {
105                         WeekProfileStatus weekProfileStatus = weekProfile.getStatusAt(LocalDateTime.now());
106                         updateState(CHANNEL_ZONE_CALCULATED_WEEK_PROFILE_STATUS,
107                                 StringType.valueOf(weekProfileStatus.name()));
108                     } catch (NoboDataException nde) {
109                         logger.debug("Failed getting current week profile status", nde);
110                     }
111                 }
112
113                 List<StateOption> options = new ArrayList<>();
114                 logger.debug("Updating week profile state description options for zone {}.", zone.getName());
115                 for (WeekProfile wp : hubHandler.getWeekProfiles()) {
116                     options.add(new StateOption(String.valueOf(wp.getId()), wp.getName()));
117                 }
118                 logger.debug("State options count: {}. First: {}", options.size(),
119                         (!options.isEmpty()) ? options.get(0) : 0);
120                 weekProfileStateDescriptionOptionsProvider.setStateOptions(
121                         new ChannelUID(getThing().getUID(), CHANNEL_ZONE_ACTIVE_WEEK_PROFILE), options);
122             }
123         }
124
125         Map<String, String> properties = editProperties();
126         properties.put(PROPERTY_HOSTNAME, zone.getName());
127         properties.put(PROPERTY_ZONE_ID, Integer.toString(zone.getId()));
128         updateProperties(properties);
129     }
130
131     @Override
132     public void initialize() {
133         this.id = getConfigAs(ZoneConfiguration.class).id;
134         updateStatus(ThingStatus.ONLINE);
135     }
136
137     @Override
138     public void handleCommand(ChannelUID channelUID, Command command) {
139         if (command instanceof RefreshType) {
140             logger.debug("Refreshing channel {}", channelUID);
141
142             Zone zone = getZone();
143             if (null == zone) {
144                 logger.debug("Could not find Zone with id {} for channel {}", id, channelUID);
145                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
146                         messages.getText("message.zone.notfound", id, channelUID));
147             } else {
148                 onUpdate(zone);
149                 Bridge noboHub = getBridge();
150                 if (null != noboHub) {
151                     NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
152                     if (null != hubHandler) {
153                         WeekProfile weekProfile = hubHandler.getWeekProfile(zone.getActiveWeekProfileId());
154                         if (null != weekProfile) {
155                             String weekProfileName = weekProfile.getName();
156                             StringType weekProfileValue = StringType.valueOf(weekProfileName);
157                             updateState(CHANNEL_ZONE_ACTIVE_WEEK_PROFILE_NAME, weekProfileValue);
158                         }
159                     }
160                 }
161             }
162
163             return;
164         }
165
166         if (CHANNEL_ZONE_COMFORT_TEMPERATURE.equals(channelUID.getId())) {
167             Zone zone = getZone();
168             if (zone != null) {
169                 if (command instanceof DecimalType) {
170                     DecimalType comfortTemp = (DecimalType) command;
171                     logger.debug("Set comfort temp for zone {} to {}", zone.getName(), comfortTemp.doubleValue());
172                     zone.setComfortTemperature(comfortTemp.intValue());
173                     sendCommand(zone.generateCommandString("U00"));
174                 }
175             }
176
177             return;
178         }
179
180         if (CHANNEL_ZONE_ECO_TEMPERATURE.equals(channelUID.getId())) {
181             Zone zone = getZone();
182             if (zone != null) {
183                 if (command instanceof DecimalType) {
184                     DecimalType ecoTemp = (DecimalType) command;
185                     logger.debug("Set eco temp for zone {} to {}", zone.getName(), ecoTemp.doubleValue());
186                     zone.setEcoTemperature(ecoTemp.intValue());
187                     sendCommand(zone.generateCommandString("U00"));
188                 }
189             }
190             return;
191         }
192
193         if (CHANNEL_ZONE_ACTIVE_WEEK_PROFILE.equals(channelUID.getId())) {
194             Zone zone = getZone();
195             if (zone != null) {
196                 if (command instanceof DecimalType) {
197                     DecimalType weekProfileId = (DecimalType) command;
198                     logger.debug("Set week profile for zone {} to {}", zone.getName(), weekProfileId);
199                     zone.setWeekProfile(weekProfileId.intValue());
200                     sendCommand(zone.generateCommandString("U00"));
201                 }
202             }
203
204             return;
205         }
206
207         logger.debug("Unhandled zone command {}: {}", channelUID.getId(), command);
208     }
209
210     public @Nullable Integer getZoneId() {
211         return id;
212     }
213
214     private void sendCommand(String command) {
215         Bridge noboHub = getBridge();
216         if (null != noboHub) {
217             NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
218             if (null != hubHandler) {
219                 hubHandler.sendCommand(command);
220             }
221         }
222     }
223
224     private @Nullable Zone getZone() {
225         Bridge noboHub = getBridge();
226         if (null != noboHub) {
227             NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
228             if (null != hubHandler) {
229                 return hubHandler.getZone(id);
230             }
231         }
232
233         return null;
234     }
235 }