Skip to content

Commit

Permalink
Fix issue #58 parsing where no unit specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Sep 13, 2023
1 parent 0d90061 commit 33081c6
Show file tree
Hide file tree
Showing 28 changed files with 54,774 additions and 5 deletions.
4 changes: 4 additions & 0 deletions com.qubular.openhab-binding-vicare-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ If you have multiple heating devices configured then you may need to increase th
Changelog
---------

### 4.0.4

* Fixed #58 Fix parsing in cases where units are omitted.

### 4.0.3

* Fix for #57 Rework prefetching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public Feature deserialize(JsonElement jsonElement, Type type, JsonDeserializati
JsonObject startObject = properties.getAsJsonObject("start");
JsonObject endObject = properties.getAsJsonObject("end");
if (temperature != null) {
boolean activeStatus = activeObject.get("value").getAsBoolean();
Boolean activeStatus = ofNullable(activeObject).map(jo -> jo.get("value")).map(JsonElement::getAsBoolean).orElse(null);
DimensionalValue temperatureValue = dimensionalValueFromUnitValue(temperature);
List<CommandDescriptor> commandDescriptors = generateCommands(commands);
return new NumericSensorFeature(featureName, "temperature", commandDescriptors, temperatureValue, NA, activeStatus
Expand All @@ -445,6 +445,9 @@ public Feature deserialize(JsonElement jsonElement, Type type, JsonDeserializati
}
} else if (featureName.endsWith(".production") ||
featureName.contains(".consumption.")) {
Optional<String> defaultUnit = Optional.ofNullable(properties.getAsJsonObject("unit"))
.map(jo -> jo.get("value"))
.map(JsonElement::getAsString);
Map<String, Value> arrayProperties = properties.entrySet().stream()
.filter(e -> TYPE_ARRAY.equals(e.getValue().getAsJsonObject().get("type").getAsString()))
.collect(toMap(Map.Entry::getKey, e -> {
Expand All @@ -454,7 +457,10 @@ public Feature deserialize(JsonElement jsonElement, Type type, JsonDeserializati
for (int i = 0; i < jsonArray.size(); ++i) {
values[i] = jsonArray.get(i).getAsDouble();
}
Unit unit = new Unit(property.get("unit").getAsString());
Unit unit = Optional.ofNullable(property.get("unit")).map(JsonElement::getAsString)
.or(() -> defaultUnit)
.map(Unit::new)
.orElse(Unit.EMPTY);
return new ArrayValue(unit, values);
}));

Expand Down Expand Up @@ -485,7 +491,7 @@ private static Map<String, Value> propertyMap(JsonObject properties) {
return BooleanValue.valueOf(
propObject.get("value").getAsBoolean());
case TYPE_NUMBER:
return new DimensionalValue(new Unit(propObject.get("unit").getAsString()), propObject.get("value").getAsDouble());
return dimensionalValueFromUnitValue(propObject);
default:
throw new IllegalStateException(
"Unsupported property type " + propObject.get(
Expand Down Expand Up @@ -551,8 +557,11 @@ private static LocalDate dateFromYYYYMMDD(JsonObject prop) {
}

private static DimensionalValue dimensionalValueFromUnitValue(JsonObject prop) {
String unit = prop.get("unit").getAsString();
Unit unit = Optional.ofNullable(prop.get("unit"))
.map(JsonElement::getAsString)
.map(Unit::new)
.orElse(Unit.EMPTY);
double numberValue = prop.get("value").getAsDouble();
return new DimensionalValue(new Unit(unit), numberValue);
return new DimensionalValue(unit, numberValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.opentest4j.AssertionFailedError;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
Expand Down Expand Up @@ -1435,6 +1437,26 @@ public void supports_heating_solar_power_production() throws ServletException, A
assertEquals(0.0, solarPower.get().getConsumption(ConsumptionFeature.Stat.PREVIOUS_YEAR).get().getValue(), 0.001);
}

@Test
public void supports_heating_solar_power_production_withSeparateUnit() throws ServletException, AuthenticationException, NamespaceException, IOException {
List<Feature> features = getFeatures("response/Solar.json");

Optional<ConsumptionFeature> solarPower = features.stream()
.filter(f -> f.getName().equals("heating.solar.power.production"))
.map(ConsumptionFeature.class::cast)
.findFirst();

assertEquals(19.773, solarPower.get().getConsumption(ConsumptionFeature.Stat.CURRENT_DAY).get().getValue(), 0.001);
assertEquals(Unit.KILOWATT_HOUR, solarPower.get().getConsumption(ConsumptionFeature.Stat.CURRENT_DAY).get().getUnit());
assertEquals(20.642, solarPower.get().getConsumption(ConsumptionFeature.Stat.PREVIOUS_DAY).get().getValue(), 0.001);
assertEquals(19.773, solarPower.get().getConsumption(ConsumptionFeature.Stat.CURRENT_WEEK).get().getValue(), 0.001);
assertEquals(20.642, solarPower.get().getConsumption(ConsumptionFeature.Stat.PREVIOUS_WEEK).get().getValue(), 0.001);
assertEquals(19.773, solarPower.get().getConsumption(ConsumptionFeature.Stat.CURRENT_MONTH).get().getValue(), 0.001);
assertEquals(20.642, solarPower.get().getConsumption(ConsumptionFeature.Stat.PREVIOUS_MONTH).get().getValue(), 0.001);
assertEquals(19.773, solarPower.get().getConsumption(ConsumptionFeature.Stat.CURRENT_YEAR).get().getValue(), 0.001);
assertEquals(20.642, solarPower.get().getConsumption(ConsumptionFeature.Stat.PREVIOUS_YEAR).get().getValue(), 0.001);
}

@Test
@DisabledIf("realConnection")
public void supports_heating_solar_sensors_temperature_collector() throws ServletException, AuthenticationException, NamespaceException, IOException {
Expand Down Expand Up @@ -1689,4 +1711,30 @@ public void supports_ventilation_operating_programs_holiday() throws ServletExce
assertEquals(LocalDateValue.EMPTY, holiday.get().getProperties().get("start"));
assertEquals(LocalDateValue.EMPTY, holiday.get().getProperties().get("end"));
}

@ValueSource(strings = {"response/Solar.json",
"response/Vitocal200.json",
"response/Vitocal200S.json",
"response/Vitocal200S_E8NEV.json",
"response/Vitocal222S.json",
"response/Vitocal250A.json",
"response/Vitocal300G.json",
"response/Vitocaldens222F.json",
"response/VitochargeVX3.json",
"response/Vitodens100W.json",
"response/Vitodens111W.json",
"response/Vitodens200W.json",
"response/Vitodens200W_2.json",
"response/Vitodens222W.json",
"response/Vitodens300W.json",
"response/Vitodens333F.json",
"response/VitolaUniferral.json",
"response/VitovalorPT2.json",
"response/zigbee_zk03839.json",
"response/zigbee_zk03840.json"})
@ParameterizedTest
public void canParseAllJsonResponses(String filename) throws ServletException, AuthenticationException, NamespaceException, IOException {
logger.info("canParseAllJsonResponses({})", filename);
getFeatures(filename);
}
}
Loading

0 comments on commit 33081c6

Please sign in to comment.