2 * Copyright (c) 2010-2022 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.sensibo.internal.handler;
15 import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Locale;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
28 import javax.measure.IncommensurableException;
29 import javax.measure.UnconvertibleException;
30 import javax.measure.Unit;
31 import javax.measure.UnitConverter;
32 import javax.measure.quantity.Temperature;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.lang3.builder.ToStringBuilder;
36 import org.apache.commons.lang3.builder.ToStringStyle;
37 import org.apache.commons.lang3.text.WordUtils;
38 import org.eclipse.jdt.annotation.NonNullByDefault;
39 import org.eclipse.jdt.annotation.Nullable;
40 import org.openhab.binding.sensibo.internal.CallbackChannelsTypeProvider;
41 import org.openhab.binding.sensibo.internal.SensiboBindingConstants;
42 import org.openhab.binding.sensibo.internal.config.SensiboSkyConfiguration;
43 import org.openhab.binding.sensibo.internal.dto.poddetails.TemperatureDTO;
44 import org.openhab.binding.sensibo.internal.model.SensiboModel;
45 import org.openhab.binding.sensibo.internal.model.SensiboSky;
46 import org.openhab.core.library.types.DecimalType;
47 import org.openhab.core.library.types.OnOffType;
48 import org.openhab.core.library.types.QuantityType;
49 import org.openhab.core.library.types.StringType;
50 import org.openhab.core.library.unit.SIUnits;
51 import org.openhab.core.library.unit.Units;
52 import org.openhab.core.thing.Channel;
53 import org.openhab.core.thing.ChannelUID;
54 import org.openhab.core.thing.Thing;
55 import org.openhab.core.thing.ThingStatus;
56 import org.openhab.core.thing.ThingStatusDetail;
57 import org.openhab.core.thing.binding.ThingHandlerService;
58 import org.openhab.core.thing.binding.builder.ChannelBuilder;
59 import org.openhab.core.thing.type.ChannelType;
60 import org.openhab.core.thing.type.ChannelTypeBuilder;
61 import org.openhab.core.thing.type.ChannelTypeProvider;
62 import org.openhab.core.thing.type.ChannelTypeUID;
63 import org.openhab.core.thing.type.StateChannelTypeBuilder;
64 import org.openhab.core.types.Command;
65 import org.openhab.core.types.RefreshType;
66 import org.openhab.core.types.StateDescriptionFragmentBuilder;
67 import org.openhab.core.types.StateOption;
68 import org.openhab.core.types.UnDefType;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
73 * The {@link SensiboSkyHandler} is responsible for handling commands, which are
74 * sent to one of the channels.
76 * @author Arne Seime - Initial contribution
79 public class SensiboSkyHandler extends SensiboBaseThingHandler implements ChannelTypeProvider {
80 public static final String SWING_PROPERTY = "swing";
81 public static final String MASTER_SWITCH_PROPERTY = "on";
82 public static final String FAN_LEVEL_PROPERTY = "fanLevel";
83 public static final String MODE_PROPERTY = "mode";
84 public static final String TARGET_TEMPERATURE_PROPERTY = "targetTemperature";
85 public static final String SWING_MODE_LABEL = "Swing Mode";
86 public static final String FAN_LEVEL_LABEL = "Fan Level";
87 public static final String MODE_LABEL = "Mode";
88 public static final String TARGET_TEMPERATURE_LABEL = "Target Temperature";
89 private static final String ITEM_TYPE_STRING = "String";
90 private static final String ITEM_TYPE_NUMBER_TEMPERATURE = "Number:Temperature";
91 private final Logger logger = LoggerFactory.getLogger(SensiboSkyHandler.class);
92 private final Map<ChannelTypeUID, ChannelType> generatedChannelTypes = new HashMap<>();
93 private Optional<SensiboSkyConfiguration> config = Optional.empty();
95 public SensiboSkyHandler(final Thing thing) {
99 private static String beautify(final String camelCaseWording) {
100 final StringBuilder b = new StringBuilder();
101 for (final String s : StringUtils.splitByCharacterTypeCamelCase(camelCaseWording)) {
105 final StringBuilder bs = new StringBuilder();
106 for (final String t : StringUtils.splitByWholeSeparator(b.toString(), " _")) {
111 return WordUtils.capitalizeFully(bs.toString()).trim();
114 private String getMacAddress() {
115 if (config.isPresent()) {
116 return config.get().macAddress;
118 throw new IllegalArgumentException("No configuration present");
122 public void handleCommand(final ChannelUID channelUID, final Command command) {
123 handleCommand(channelUID, command, getSensiboModel());
127 * Package private in order to be reachable from unit test
129 void updateAcState(SensiboSky sensiboSky, String property, Object value) {
130 StateChange stateChange = checkStateChangeValid(sensiboSky, property, value);
131 if (stateChange.valid) {
132 getAccountHandler().ifPresent(
133 handler -> handler.updateSensiboSkyAcState(getMacAddress(), property, stateChange.value, this));
135 logger.info("Update command not sent; invalid state change for SensiboSky AC state: {}",
136 stateChange.validationMessage);
140 private void updateTimer(@Nullable Integer secondsFromNowUntilSwitchOff) {
142 .ifPresent(handler -> handler.updateSensiboSkyTimer(getMacAddress(), secondsFromNowUntilSwitchOff));
146 protected void handleCommand(final ChannelUID channelUID, final Command command, final SensiboModel model) {
147 model.findSensiboSkyByMacAddress(getMacAddress()).ifPresent(sensiboSky -> {
148 if (sensiboSky.isAlive()) {
149 if (getThing().getStatus() != ThingStatus.ONLINE) {
150 addDynamicChannelsAndProperties(sensiboSky);
151 updateStatus(ThingStatus.ONLINE); // In case it has been offline
153 switch (channelUID.getId()) {
154 case CHANNEL_CURRENT_HUMIDITY:
155 handleCurrentHumidityCommand(channelUID, command, sensiboSky);
157 case CHANNEL_CURRENT_TEMPERATURE:
158 handleCurrentTemperatureCommand(channelUID, command, sensiboSky);
160 case CHANNEL_MASTER_SWITCH:
161 handleMasterSwitchCommand(channelUID, command, sensiboSky);
163 case CHANNEL_TARGET_TEMPERATURE:
164 handleTargetTemperatureCommand(channelUID, command, sensiboSky);
167 handleModeCommand(channelUID, command, sensiboSky);
169 case CHANNEL_SWING_MODE:
170 handleSwingCommand(channelUID, command, sensiboSky);
172 case CHANNEL_FAN_LEVEL:
173 handleFanLevelCommand(channelUID, command, sensiboSky);
176 handleTimerCommand(channelUID, command, sensiboSky);
179 logger.debug("Received command on unknown channel {}, ignoring", channelUID.getId());
182 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
183 "Unreachable by Sensibo servers");
188 private void handleTimerCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
189 if (command instanceof RefreshType) {
190 if (sensiboSky.getTimer().isPresent() && sensiboSky.getTimer().get().secondsRemaining > 0) {
191 updateState(channelUID, new DecimalType(sensiboSky.getTimer().get().secondsRemaining));
193 updateState(channelUID, UnDefType.UNDEF);
195 } else if (command instanceof DecimalType) {
196 final DecimalType newValue = (DecimalType) command;
197 updateTimer(newValue.intValue());
203 private void handleFanLevelCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
204 if (command instanceof RefreshType) {
205 if (sensiboSky.getAcState().isPresent() && sensiboSky.getAcState().get().getFanLevel() != null) {
206 updateState(channelUID, new StringType(sensiboSky.getAcState().get().getFanLevel()));
208 updateState(channelUID, UnDefType.UNDEF);
210 } else if (command instanceof StringType) {
211 final StringType newValue = (StringType) command;
212 updateAcState(sensiboSky, FAN_LEVEL_PROPERTY, newValue.toString());
216 private void handleSwingCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
217 if (command instanceof RefreshType && sensiboSky.getAcState().isPresent()) {
218 if (sensiboSky.getAcState().isPresent() && sensiboSky.getAcState().get().getSwing() != null) {
219 updateState(channelUID, new StringType(sensiboSky.getAcState().get().getSwing()));
221 updateState(channelUID, UnDefType.UNDEF);
223 } else if (command instanceof StringType) {
224 final StringType newValue = (StringType) command;
225 updateAcState(sensiboSky, SWING_PROPERTY, newValue.toString());
229 private void handleModeCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
230 if (command instanceof RefreshType) {
231 if (sensiboSky.getAcState().isPresent()) {
232 updateState(channelUID, new StringType(sensiboSky.getAcState().get().getMode()));
234 updateState(channelUID, UnDefType.UNDEF);
236 } else if (command instanceof StringType) {
237 final StringType newValue = (StringType) command;
238 updateAcState(sensiboSky, MODE_PROPERTY, newValue.toString());
239 addDynamicChannelsAndProperties(sensiboSky);
243 private void handleTargetTemperatureCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
244 if (command instanceof RefreshType) {
245 sensiboSky.getAcState().ifPresent(acState -> {
247 Integer targetTemperature = acState.getTargetTemperature();
248 if (targetTemperature != null) {
249 updateState(channelUID, new QuantityType<>(targetTemperature, sensiboSky.getTemperatureUnit()));
251 updateState(channelUID, UnDefType.UNDEF);
254 if (!sensiboSky.getAcState().isPresent()) {
255 updateState(channelUID, UnDefType.UNDEF);
257 } else if (command instanceof QuantityType<?>) {
258 QuantityType<?> newValue = (QuantityType<?>) command;
259 if (!Objects.equals(sensiboSky.getTemperatureUnit(), newValue.getUnit())) {
260 // If quantity is given in celsius when fahrenheit is used or opposite
262 UnitConverter temperatureConverter = newValue.getUnit()
263 .getConverterToAny(sensiboSky.getTemperatureUnit());
264 // No decimals supported
265 long convertedValue = (long) temperatureConverter.convert(newValue.longValue());
266 updateAcState(sensiboSky, TARGET_TEMPERATURE_PROPERTY, new DecimalType(convertedValue));
267 } catch (UnconvertibleException | IncommensurableException e) {
268 logger.info("Could not convert {} to {}: {}", newValue, sensiboSky.getTemperatureUnit(),
272 updateAcState(sensiboSky, TARGET_TEMPERATURE_PROPERTY, new DecimalType(newValue.intValue()));
274 } else if (command instanceof DecimalType) {
275 updateAcState(sensiboSky, TARGET_TEMPERATURE_PROPERTY, command);
279 private void handleMasterSwitchCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
280 if (command instanceof RefreshType) {
281 sensiboSky.getAcState().ifPresent(e -> updateState(channelUID, OnOffType.from(e.isOn())));
282 } else if (command instanceof OnOffType) {
283 updateAcState(sensiboSky, MASTER_SWITCH_PROPERTY, command == OnOffType.ON);
287 private void handleCurrentTemperatureCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
288 if (command instanceof RefreshType) {
289 updateState(channelUID, new QuantityType<>(sensiboSky.getTemperature(), SIUnits.CELSIUS));
293 private void handleCurrentHumidityCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
294 if (command instanceof RefreshType) {
295 updateState(channelUID, new QuantityType<>(sensiboSky.getHumidity(), Units.PERCENT));
300 public Collection<Class<? extends ThingHandlerService>> getServices() {
301 return Collections.singleton(CallbackChannelsTypeProvider.class);
305 public void initialize() {
306 config = Optional.ofNullable(getConfigAs(SensiboSkyConfiguration.class));
307 logger.debug("Initializing SensiboSky using config {}", config);
308 getSensiboModel().findSensiboSkyByMacAddress(getMacAddress()).ifPresent(pod -> {
311 addDynamicChannelsAndProperties(pod);
312 updateStatus(ThingStatus.ONLINE);
314 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
315 "Unreachable by Sensibo servers");
320 private boolean isDynamicChannel(final ChannelTypeUID uid) {
321 return SensiboBindingConstants.DYNAMIC_CHANNEL_TYPES.stream().anyMatch(e -> uid.getId().startsWith(e));
324 private void addDynamicChannelsAndProperties(final SensiboSky sensiboSky) {
325 logger.debug("Updating dynamic channels for {}", sensiboSky.getId());
326 final List<Channel> newChannels = new ArrayList<>();
327 for (final Channel channel : getThing().getChannels()) {
328 final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
329 if (channelTypeUID != null && !isDynamicChannel(channelTypeUID)) {
330 newChannels.add(channel);
334 newChannels.addAll(createDynamicChannels(sensiboSky));
335 Map<String, String> properties = sensiboSky.getThingProperties();
336 updateThing(editThing().withChannels(newChannels).withProperties(properties).build());
339 public List<Channel> createDynamicChannels(final SensiboSky sensiboSky) {
340 final List<Channel> newChannels = new ArrayList<>();
341 generatedChannelTypes.clear();
343 sensiboSky.getCurrentModeCapabilities().ifPresent(capabilities -> {
344 // Not all modes have swing and fan level
345 final ChannelTypeUID swingModeChannelType = addChannelType(SensiboBindingConstants.CHANNEL_TYPE_SWING_MODE,
346 SWING_MODE_LABEL, ITEM_TYPE_STRING, capabilities.swingModes, null, null);
349 .create(new ChannelUID(getThing().getUID(), SensiboBindingConstants.CHANNEL_SWING_MODE),
351 .withLabel(SWING_MODE_LABEL).withType(swingModeChannelType).build());
353 final ChannelTypeUID fanLevelChannelType = addChannelType(SensiboBindingConstants.CHANNEL_TYPE_FAN_LEVEL,
354 FAN_LEVEL_LABEL, ITEM_TYPE_STRING, capabilities.fanLevels, null, null);
355 newChannels.add(ChannelBuilder
356 .create(new ChannelUID(getThing().getUID(), SensiboBindingConstants.CHANNEL_FAN_LEVEL),
358 .withLabel(FAN_LEVEL_LABEL).withType(fanLevelChannelType).build());
361 final ChannelTypeUID modeChannelType = addChannelType(SensiboBindingConstants.CHANNEL_TYPE_MODE, MODE_LABEL,
362 ITEM_TYPE_STRING, sensiboSky.getRemoteCapabilities().keySet(), null, null);
363 newChannels.add(ChannelBuilder
364 .create(new ChannelUID(getThing().getUID(), SensiboBindingConstants.CHANNEL_MODE), ITEM_TYPE_STRING)
365 .withLabel(MODE_LABEL).withType(modeChannelType).build());
367 final ChannelTypeUID targetTemperatureChannelType = addChannelType(
368 SensiboBindingConstants.CHANNEL_TYPE_TARGET_TEMPERATURE, TARGET_TEMPERATURE_LABEL,
369 ITEM_TYPE_NUMBER_TEMPERATURE, sensiboSky.getTargetTemperatures(), "%d %unit%", "TargetTemperature");
370 newChannels.add(ChannelBuilder
371 .create(new ChannelUID(getThing().getUID(), SensiboBindingConstants.CHANNEL_TARGET_TEMPERATURE),
372 ITEM_TYPE_NUMBER_TEMPERATURE)
373 .withLabel(TARGET_TEMPERATURE_LABEL).withType(targetTemperatureChannelType).build());
378 private ChannelTypeUID addChannelType(final String channelTypePrefix, final String label, final String itemType,
379 final Collection<?> options, @Nullable final String pattern, @Nullable final String tag) {
380 final ChannelTypeUID channelTypeUID = new ChannelTypeUID(SensiboBindingConstants.BINDING_ID,
381 channelTypePrefix + getThing().getUID().getId());
382 final List<StateOption> stateOptions = options.stream()
383 .map(e -> new StateOption(e.toString(), e instanceof String ? beautify((String) e) : e.toString()))
384 .collect(Collectors.toList());
386 StateDescriptionFragmentBuilder stateDescription = StateDescriptionFragmentBuilder.create().withReadOnly(false)
387 .withOptions(stateOptions);
388 if (pattern != null) {
389 stateDescription = stateDescription.withPattern(pattern);
391 final StateChannelTypeBuilder builder = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
392 .withStateDescriptionFragment(stateDescription.build());
394 builder.withTag(tag);
396 final ChannelType channelType = builder.build();
398 generatedChannelTypes.put(channelTypeUID, channelType);
400 return channelTypeUID;
404 public Collection<ChannelType> getChannelTypes(@Nullable final Locale locale) {
405 return generatedChannelTypes.values();
409 public @Nullable ChannelType getChannelType(final ChannelTypeUID channelTypeUID, @Nullable final Locale locale) {
410 return generatedChannelTypes.get(channelTypeUID);
414 * Package private in order to be reachable from unit test
416 StateChange checkStateChangeValid(SensiboSky sensiboSky, String property, Object newPropertyValue) {
417 StateChange stateChange = new StateChange(newPropertyValue);
419 sensiboSky.getCurrentModeCapabilities().ifPresent(currentModeCapabilities -> {
421 case TARGET_TEMPERATURE_PROPERTY:
422 Unit<Temperature> temperatureUnit = sensiboSky.getTemperatureUnit();
423 TemperatureDTO validTemperatures = currentModeCapabilities.temperatures
424 .get(SIUnits.CELSIUS.equals(temperatureUnit) ? "C" : "F");
425 DecimalType rawValue = (DecimalType) newPropertyValue;
426 stateChange.updateValue(rawValue.intValue());
427 if (!validTemperatures.validValues.contains(rawValue.intValue())) {
428 stateChange.addError(String.format(
429 "Cannot change targetTemperature to '%d', valid targetTemperatures are one of %s",
430 rawValue.intValue(), ToStringBuilder.reflectionToString(
431 validTemperatures.validValues.toArray(), ToStringStyle.SIMPLE_STYLE)));
435 if (!sensiboSky.getRemoteCapabilities().containsKey(newPropertyValue)) {
436 stateChange.addError(
437 String.format("Cannot change mode to %s, valid modes are %s", newPropertyValue,
438 ToStringBuilder.reflectionToString(
439 sensiboSky.getRemoteCapabilities().keySet().toArray(),
440 ToStringStyle.SIMPLE_STYLE)));
443 case FAN_LEVEL_PROPERTY:
444 if (!currentModeCapabilities.fanLevels.contains(newPropertyValue)) {
445 stateChange.addError(String.format("Cannot change fanLevel to %s, valid fanLevels are %s",
446 newPropertyValue, ToStringBuilder.reflectionToString(
447 currentModeCapabilities.fanLevels.toArray(), ToStringStyle.SIMPLE_STYLE)));
450 case MASTER_SWITCH_PROPERTY:
454 if (!currentModeCapabilities.swingModes.contains(newPropertyValue)) {
455 stateChange.addError(String.format("Cannot change swing to %s, valid swings are %s",
456 newPropertyValue, ToStringBuilder.reflectionToString(
457 currentModeCapabilities.swingModes.toArray(), ToStringStyle.SIMPLE_STYLE)));
461 stateChange.addError(String.format("No such ac state property %s", property));
463 logger.debug("State change request {}", stateChange);
469 public class StateChange {
472 boolean valid = true;
474 String validationMessage;
476 public StateChange(Object value) {
480 public void updateValue(Object updatedValue) {
481 value = updatedValue;
484 public void addError(String validationMessage) {
486 this.validationMessage = validationMessage;
490 public String toString() {
491 return "StateChange [valid=" + valid + ", validationMessage=" + validationMessage + ", value=" + value
492 + ", value Class=" + value.getClass() + "]";