Skip to content

Commit

Permalink
[homekit] add support for min/max values for temperature (openhab#7782)
Browse files Browse the repository at this point in the history
* add support for min/max values for temperature
* run spotless

Signed-off-by: Eugen Freiter <freiter@gmx.de>
Signed-off-by: Daan Meijer <daan@studioseptember.nl>
  • Loading branch information
yfre authored and DaanMeijer committed Sep 1, 2020
1 parent ba7afd3 commit d195da4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 12 deletions.
13 changes: 8 additions & 5 deletions bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ A full list of supported accessory types can be found in the table *below*.
| | | TamperedStatus | Switch, Contact | accessory tampered status. A status of "ON"/"OPEN" indicates that the accessory has been tampered with. Value should return to "OFF"/"CLOSED" when the accessory has been reset to a non-tampered state. |
| | | BatteryLowStatus | Switch, Contact | accessory battery status. A value of "ON"/"OPEN" indicate that the battery level of the accessory is low. Value should return to "OFF"/"CLOSED" when the battery charges to a level thats above the low threshold. |
| TemperatureSensor | | | | Temperature sensor |
| | CurrentTemperature | | Number | current temperature |
| | CurrentTemperature | | Number | current temperature. supported configuration: minValue, maxValue, step. |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | accessory current working status. A value of "ON"/"OPEN" indicate that the accessory is active and is functioning without any errors. |
| | | FaultStatus | Switch, Contact | accessory fault status. "ON"/"OPEN" value indicates that the accessory has experienced a fault that may be interfering with its intended functionality. A value of "OFF"/"CLOSED" indicates that there is no fault. |
Expand Down Expand Up @@ -229,8 +229,8 @@ A full list of supported accessory types can be found in the table *below*.
| | | SwingMode | Number,SwitchItem | swing mode. values: 0/OFF=SWING DISABLED, 1/ON=SWING ENABLED |
| | | LockControl | Number,SwitchItem | status of physical control lock. values: 0/OFF=CONTROL LOCK DISABLED, 1/ON=CONTROL LOCK ENABLED |
| Thermostat | | | | A thermostat requires all mandatory characteristics defined below |
| | CurrentTemperature | | Number | current temperature |
| | TargetTemperature | | Number | target temperature |
| | CurrentTemperature | | Number | current temperature. supported configuration: minValue, maxValue, step |
| | TargetTemperature | | Number | target temperature. supported configuration: minValue, maxValue, step |
| | CurrentHeatingCoolingMode | | String | Current heating cooling mode (OFF, AUTO, HEAT, COOL). for mapping see homekit settings above. |
| | TargetHeatingCoolingMode | | String | Target heating cooling mode (OFF, AUTO, HEAT, COOL). for mapping see homekit settings above. |
| Lock | | | | A Lock Mechanism |
Expand Down Expand Up @@ -295,6 +295,7 @@ Switch legacy_motionsensor_single "Legacy Motion Sensor"
Switch legacy_occupancy_single "Legacy Occupanncy Sensor" [ "OccupancySensor" ]
Switch legacy_smoke_single "Legacy Smoke Sensor" [ "SmokeSensor" ]
Number legacy_humidity_single "Legacy Humidity Sensor" [ "CurrentHumidity" ]
Number legacy_temperature_sensor "Temperature Sensor" ["TemperatureSensor"]
Switch legacy_lock "Legacy Lock single" [ "Lock" ]
Expand Down Expand Up @@ -332,14 +333,16 @@ Switch leaksensor_single "Leak Sensor single"
Switch lock "Lock single" {homekit="Lock"}
Switch valve_single "Valve single" {homekit="Valve" [homekitValveType="Shower"]}
Number temperature_sensor "Temperature Sensor [%.1f C]" {homekit="TemperatureSensor" [minValue=10.5, maxValue=27] }
Group gValve "Valve Group" {homekit="Valve" [homekitValveType="Irrigation"]}
Switch valve_active "Valve active" (gValve) {homekit="Valve.ActiveStatus, Valve.InUseStatus"}
Number valve_duration "Valve duration" (gValve) {homekit="Valve.Duration"}
Number valve_remaining_duration "Valve remaining duration" (gValve) {homekit="Valve.RemainingDuration"}
Group gThermostat "Thermostat" {homekit="Thermostat"}
Number thermostat_current_temp "Thermostat Current Temp [%.1f C]" (gThermostat) {homekit="Thermostat.CurrentTemperature"}
Number thermostat_target_temp "Thermostat Target Temp[%.1f C]" (gThermostat) {homekit="Thermostat.TargetTemperature"}
Number thermostat_current_temp "Thermostat Current Temp [%.1f C]" (gThermostat) {homekit="Thermostat.CurrentTemperature" [minValue=0, maxValue=40]}
Number thermostat_target_temp "Thermostat Target Temp[%.1f C]" (gThermostat) {homekit="Thermostat.TargetTemperature" [minValue=10.5, maxValue=27]}
String thermostat_current_mode "Thermostat Current Mode" (gThermostat) {homekit="Thermostat.CurrentHeatingCoolingMode"}
String thermostat_target_mode "Thermostat Target Mode" (gThermostat) {homekit="Thermostat.TargetHeatingCoolingMode"}
Expand Down
6 changes: 3 additions & 3 deletions bundles/org.openhab.io.homekit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

<dependencies>
<dependency>
<groupId>io.github.hap-java</groupId>
<artifactId>hap</artifactId>
<version>2.0.0.OH</version>
<groupId>com.github.j-n-k</groupId>
<artifactId>hap-java</artifactId>
<version>2.0.0.OH2</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
public class HomekitTaggedItem {
private final Logger logger = LoggerFactory.getLogger(HomekitTaggedItem.class);

/** configuration keywords at items level **/
public final static String MIN_VALUE = "minValue";
public final static String MAX_VALUE = "maxValue";
public final static String STEP = "step";

private static final Map<Integer, String> CREATED_ACCESSORY_IDS = new ConcurrentHashMap<>();
/**
* The type of HomekitDevice we've decided this was. If the item is question is the member of a group which is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ protected void unsubscribe(HomekitCharacteristicType characteristicType) {
return (T) state.as(type);
}
}
logger.warn("State for characteristic {} at accessory {} cannot be retrieved.", characteristic,
accessory.getId());
logger.debug("State for characteristic {} at accessory {} cannot be retrieved.", characteristic,
accessory.getName());
return null;
}

Expand All @@ -170,8 +170,8 @@ protected void unsubscribe(HomekitCharacteristicType characteristicType) {
}

@SuppressWarnings("unchecked")
protected <T> T getAccessoryConfiguration(String key, @NonNull T defaultValue) {
final @Nullable Map<String, Object> configuration = accessory.getConfiguration();
private <T> T getItemConfiguration(@NonNull HomekitTaggedItem item, @NonNull String key, @NonNull T defaultValue) {
final @Nullable Map<String, Object> configuration = item.getConfiguration();
if (configuration != null) {
Object value = configuration.get(key);
if (value != null && value.getClass().equals(defaultValue.getClass())) {
Expand All @@ -181,6 +181,38 @@ protected <T> T getAccessoryConfiguration(String key, @NonNull T defaultValue) {
return defaultValue;
}

/**
* return configuration attached to the root accessory, e.g. groupItem.
* Note: result will be casted to the type of the default value.
* The type for number is BigDecimal.
*
* @param key configuration key
* @param defaultValue default value
* @param <T> expected type
* @return configuration value
*/
protected <T> T getAccessoryConfiguration(@NonNull String key, @NonNull T defaultValue) {
return getItemConfiguration(accessory, key, defaultValue);
}

/**
* return configuration of the characteristic item, e.g. currentTemperature.
* Note: result will be casted to the type of the default value.
* The type for number is BigDecimal.
*
* @param characteristicType characteristic type
* @param key configuration key
* @param defaultValue default value
* @param <T> expected type
* @return configuration value
*/
protected <T> T getAccessoryConfiguration(@NonNull HomekitCharacteristicType characteristicType,
@NonNull String key, @NonNull T defaultValue) {
final Optional<HomekitTaggedItem> characteristic = getCharacteristic(characteristicType);
return characteristic.isPresent() ? getItemConfiguration(characteristic.get(), key, defaultValue)
: defaultValue;
}

protected void addCharacteristic(HomekitTaggedItem characteristic) {
characteristics.add(characteristic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.io.homekit.internal.accessories;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand All @@ -24,6 +25,7 @@

import io.github.hapjava.accessories.TemperatureSensorAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
import io.github.hapjava.services.impl.TemperatureSensorService;

/**
Expand Down Expand Up @@ -51,6 +53,24 @@ public void subscribeCurrentTemperature(HomekitCharacteristicChangeCallback call
subscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE, callback);
}

@Override
public double getMinCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
}

@Override
public double getMaxCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
}

@Override
public double getMinStepCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.STEP,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_STEP)).doubleValue();
}

@Override
public void unsubscribeCurrentTemperature() {
unsubscribe(HomekitCharacteristicType.CURRENT_TEMPERATURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.thermostat.CurrentHeatingCoolingStateEnum;
import io.github.hapjava.characteristics.impl.thermostat.TargetHeatingCoolingStateEnum;
import io.github.hapjava.characteristics.impl.thermostat.TargetTemperatureCharacteristic;
import io.github.hapjava.characteristics.impl.thermostat.TemperatureDisplayUnitEnum;
import io.github.hapjava.services.impl.ThermostatService;

Expand Down Expand Up @@ -93,6 +94,24 @@ public CompletableFuture<Double> getCurrentTemperature() {
return CompletableFuture.completedFuture(state != null ? convertToCelsius(state.doubleValue()) : 0.0);
}

@Override
public double getMinCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
}

@Override
public double getMaxCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
}

@Override
public double getMinStepCurrentTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.CURRENT_TEMPERATURE, HomekitTaggedItem.STEP,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_STEP)).doubleValue();
}

@Override
public CompletableFuture<TargetHeatingCoolingStateEnum> getTargetState() {
final HomekitSettings settings = getSettings();
Expand Down Expand Up @@ -187,6 +206,24 @@ public void setTargetTemperature(Double value) {
}
}

@Override
public double getMinTargetTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.TARGET_TEMPERATURE, HomekitTaggedItem.MIN_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MIN_VALUE)).doubleValue();
}

@Override
public double getMaxTargetTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.TARGET_TEMPERATURE, HomekitTaggedItem.MAX_VALUE,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_MAX_VALUE)).doubleValue();
}

@Override
public double getMinStepTargetTemperature() {
return getAccessoryConfiguration(HomekitCharacteristicType.TARGET_TEMPERATURE, HomekitTaggedItem.STEP,
BigDecimal.valueOf(TargetTemperatureCharacteristic.DEFAULT_STEP)).doubleValue();
}

@Override
public void subscribeCurrentState(HomekitCharacteristicChangeCallback callback) {
subscribe(HomekitCharacteristicType.CURRENT_HEATING_COOLING_STATE, callback);
Expand Down

0 comments on commit d195da4

Please sign in to comment.