[tado] Fix AC target temperature reading and writing bugs (#13272)

* [tado] fix npe when target temperature json element is missing
* [tado] apply temperature command/value restrictions

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2022-08-17 08:32:17 +01:00 committed by GitHub
parent 801895e2d2
commit 8baa500998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import java.math.RoundingMode;
import java.time.OffsetDateTime;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tado.internal.TadoBindingConstants.HvacMode;
import org.openhab.binding.tado.internal.TadoBindingConstants.OperationMode;
import org.openhab.binding.tado.internal.TadoBindingConstants.TemperatureUnit;
@ -232,13 +233,20 @@ public class TadoZoneStateAdapter {
return new DateTimeType(offsetDateTime.toZonedDateTime());
}
private static State toTemperatureState(TemperatureObject temperature, TemperatureUnit temperatureUnit) {
private static State toTemperatureState(@Nullable TemperatureObject temperature, TemperatureUnit temperatureUnit) {
if (temperature == null || (temperature.getCelsius() == null && temperature.getFahrenheit() == null)) {
return UnDefType.UNDEF;
}
return temperatureUnit == TemperatureUnit.FAHRENHEIT
? new QuantityType<>(temperature.getFahrenheit(), ImperialUnits.FAHRENHEIT)
: new QuantityType<>(temperature.getCelsius(), SIUnits.CELSIUS);
}
private static State toTemperatureState(TemperatureDataPoint temperature, TemperatureUnit temperatureUnit) {
private static State toTemperatureState(@Nullable TemperatureDataPoint temperature,
TemperatureUnit temperatureUnit) {
if (temperature == null || (temperature.getCelsius() == null && temperature.getFahrenheit() == null)) {
return UnDefType.UNDEF;
}
return temperatureUnit == TemperatureUnit.FAHRENHEIT
? new QuantityType<>(temperature.getFahrenheit(), ImperialUnits.FAHRENHEIT)
: new QuantityType<>(temperature.getCelsius(), SIUnits.CELSIUS);

View File

@ -76,11 +76,6 @@ public class AirConditioningZoneSettingsBuilder extends ZoneSettingsBuilder {
targetMode = getCurrentOrDefaultAcMode(zoneStateProvider);
}
Float temperature = this.temperature;
if (temperature != null) {
newSetting.setTemperature(temperature(temperature, temperatureUnit));
}
Boolean swing = this.swing;
if (swing != null) {
newSetting.setSwing(swing.booleanValue() ? Power.ON : Power.OFF);
@ -105,6 +100,24 @@ public class AirConditioningZoneSettingsBuilder extends ZoneSettingsBuilder {
AcModeCapabilities targetModeCapabilities = TadoApiTypeUtils.getModeCapabilities(targetMode,
genericCapabilities);
Float temperature = this.temperature;
if (temperature != null) {
IntRange range = null;
boolean valid = false;
TemperatureRange caps = targetModeCapabilities.getTemperatures();
if (caps != null) {
range = temperatureUnit == TemperatureUnit.CELSIUS ? caps.getCelsius() : caps.getFahrenheit();
valid = (range != null) && (range.getMin() <= temperature) && (temperature <= range.getMax());
}
if (valid) {
newSetting.setTemperature(temperature(temperature, temperatureUnit));
} else {
logger.warn(STATE_VALUE_NOT_SUPPORTED, "Target Temperature", temperature,
targetMode.getClass().getSimpleName(), targetMode,
range == null ? "none" : String.format("%d..%d", range.getMin(), range.getMax()));
}
}
FanLevel fanLevel = this.fanLevel;
if (fanLevel != null) {
ACFanLevel targetFanLevel = getFanLevel(fanLevel);