2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nobohub.internal;
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;
24 import java.time.LocalDateTime;
25 import java.util.ArrayList;
26 import java.util.List;
29 import javax.measure.quantity.Temperature;
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;
54 * Shows information about a named Zone in the Nobø Hub.
56 * @author Jørgen Austvik - Initial contribution
59 public class ZoneHandler extends BaseThingHandler {
61 private final Logger logger = LoggerFactory.getLogger(ZoneHandler.class);
63 private final WeekProfileStateDescriptionOptionsProvider weekProfileStateDescriptionOptionsProvider;
65 private final NoboHubTranslationProvider messages;
69 public ZoneHandler(Thing thing, NoboHubTranslationProvider messages,
70 WeekProfileStateDescriptionOptionsProvider weekProfileStateDescriptionOptionsProvider) {
72 this.messages = messages;
73 this.weekProfileStateDescriptionOptionsProvider = weekProfileStateDescriptionOptionsProvider;
76 public void onUpdate(Zone zone) {
77 logger.debug("Updating zone: {}", zone.getName());
78 updateStatus(ThingStatus.ONLINE);
80 QuantityType<Temperature> comfortTemperature = new QuantityType<>(zone.getComfortTemperature(),
82 updateState(CHANNEL_ZONE_COMFORT_TEMPERATURE, comfortTemperature);
83 QuantityType<Temperature> ecoTemperature = new QuantityType<>(zone.getEcoTemperature(), SIUnits.CELSIUS);
84 updateState(CHANNEL_ZONE_ECO_TEMPERATURE, ecoTemperature);
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);
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())));
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);
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()));
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);
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);
132 public void initialize() {
133 this.id = getConfigAs(ZoneConfiguration.class).id;
134 updateStatus(ThingStatus.ONLINE);
138 public void handleCommand(ChannelUID channelUID, Command command) {
139 if (command instanceof RefreshType) {
140 logger.debug("Refreshing channel {}", channelUID);
142 Zone zone = getZone();
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));
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);
166 if (CHANNEL_ZONE_COMFORT_TEMPERATURE.equals(channelUID.getId())) {
167 Zone zone = getZone();
169 if (command instanceof DecimalType comfortTemp) {
170 logger.debug("Set comfort temp for zone {} to {}", zone.getName(), comfortTemp.doubleValue());
171 zone.setComfortTemperature(comfortTemp.intValue());
172 sendCommand(zone.generateCommandString("U00"));
179 if (CHANNEL_ZONE_ECO_TEMPERATURE.equals(channelUID.getId())) {
180 Zone zone = getZone();
182 if (command instanceof DecimalType ecoTemp) {
183 logger.debug("Set eco temp for zone {} to {}", zone.getName(), ecoTemp.doubleValue());
184 zone.setEcoTemperature(ecoTemp.intValue());
185 sendCommand(zone.generateCommandString("U00"));
191 if (CHANNEL_ZONE_ACTIVE_WEEK_PROFILE.equals(channelUID.getId())) {
192 Zone zone = getZone();
194 if (command instanceof DecimalType weekProfileId) {
195 logger.debug("Set week profile for zone {} to {}", zone.getName(), weekProfileId);
196 zone.setWeekProfile(weekProfileId.intValue());
197 sendCommand(zone.generateCommandString("U00"));
204 logger.debug("Unhandled zone command {}: {}", channelUID.getId(), command);
207 public @Nullable Integer getZoneId() {
211 private void sendCommand(String command) {
212 Bridge noboHub = getBridge();
213 if (null != noboHub) {
214 NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
215 if (null != hubHandler) {
216 hubHandler.sendCommand(command);
221 private @Nullable Zone getZone() {
222 Bridge noboHub = getBridge();
223 if (null != noboHub) {
224 NoboHubBridgeHandler hubHandler = (NoboHubBridgeHandler) noboHub.getHandler();
225 if (null != hubHandler) {
226 return hubHandler.getZone(id);