Skip to content

Commit

Permalink
Rename price element to price component (openhab#15935)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored Nov 20, 2023
1 parent ff873ff commit b2b7602
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
14 changes: 7 additions & 7 deletions bundles/org.openhab.binding.energidataservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ To obtain the Global Location Number of your grid company:

For customers using electricity for heating, a reduced electricity tax rate may apply after consuming the first 4000 kWh within a year.
When you are entitled to reduced electricity tax, this option should be set.
This will ensure that thing action calculations use the reduced electricity tax rate when price elements are not explicitly provided.
This will ensure that thing action calculations use the reduced electricity tax rate when price components are not explicitly provided.
It will not impact channels, see [Electricity Tax](#electricity-tax) for further information.

## Channels
Expand Down Expand Up @@ -170,7 +170,7 @@ Historic prices older than 24 hours are removed from the JSON array each hour.
## Thing Actions

Thing actions can be used to perform calculations as well as import prices directly into rules without deserializing JSON from the [hourly-prices](#hourly-prices) channel.
This is more convenient, much faster, and provides automatic summation of the price elements of interest.
This is more convenient, much faster, and provides automatic summation of the price components of interest.

Actions use cached data for performing operations.
Since data is only fetched when an item is linked to a channel, there might not be any cached data available.
Expand Down Expand Up @@ -327,14 +327,14 @@ var price = actions.calculatePrice(now.toInstant(), now.plusHours(4).toInstant,

| Parameter | Type | Description |
|--------------------|-----------------------------|--------------------------------------------------------|
| priceElements | `String` | Comma-separated list of price elements to include |
| priceComponents | `String` | Comma-separated list of price components to include |

**Result:** `Map<Instant, BigDecimal>`

The parameter `priceElements` is a case-insensitive comma-separated list of price elements to include in the returned hourly prices.
These elements can be requested:
The parameter `priceComponents` is a case-insensitive comma-separated list of price components to include in the returned hourly prices.
These components can be requested:

| Price element | Description |
| Price component | Description |
|-----------------------|-------------------------|
| SpotPrice | Spot price |
| NetTariff | Net tariff |
Expand All @@ -343,7 +343,7 @@ These elements can be requested:
| ReducedElectricityTax | Reduced electricity tax |
| TransmissionNetTariff | Transmission net tariff |

Using `null` as parameter returns the total prices including all price elements.
Using `null` as parameter returns the total prices including all price components.
If **Reduced Electricity Tax** is set in Thing configuration, `ElectricityTax` will be excluded, otherwise `ReducedElectricityTax`.
This logic ensures consistent and comparable results not affected by artifical changes in the rate for electricity tax two times per year.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ public class EnergiDataServiceActions implements ThingActions {

private @Nullable EnergiDataServiceHandler handler;

private enum PriceElement {
private enum PriceComponent {
SPOT_PRICE("spotprice", null),
NET_TARIFF("nettariff", DatahubTariff.NET_TARIFF),
SYSTEM_TARIFF("systemtariff", DatahubTariff.SYSTEM_TARIFF),
ELECTRICITY_TAX("electricitytax", DatahubTariff.ELECTRICITY_TAX),
REDUCED_ELECTRICITY_TAX("reducedelectricitytax", DatahubTariff.REDUCED_ELECTRICITY_TAX),
TRANSMISSION_NET_TARIFF("transmissionnettariff", DatahubTariff.TRANSMISSION_NET_TARIFF);

private static final Map<String, PriceElement> NAME_MAP = Stream.of(values())
.collect(Collectors.toMap(PriceElement::toString, Function.identity()));
private static final Map<String, PriceComponent> NAME_MAP = Stream.of(values())
.collect(Collectors.toMap(PriceComponent::toString, Function.identity()));

private String name;
private @Nullable DatahubTariff datahubTariff;

private PriceElement(String name, @Nullable DatahubTariff datahubTariff) {
private PriceComponent(String name, @Nullable DatahubTariff datahubTariff) {
this.name = name;
this.datahubTariff = datahubTariff;
}
Expand All @@ -85,8 +85,8 @@ public String toString() {
return name;
}

public static PriceElement fromString(final String name) {
PriceElement myEnum = NAME_MAP.get(name.toLowerCase());
public static PriceComponent fromString(final String name) {
PriceComponent myEnum = NAME_MAP.get(name.toLowerCase());
if (null == myEnum) {
throw new IllegalArgumentException(String.format("'%s' has no corresponding value. Accepted values: %s",
name, Arrays.asList(values())));
Expand All @@ -109,30 +109,30 @@ public static PriceElement fromString(final String name) {

boolean isReducedElectricityTax = handler.isReducedElectricityTax();

return getPrices(Arrays.stream(PriceElement.values())
.filter(element -> element != (isReducedElectricityTax ? PriceElement.ELECTRICITY_TAX
: PriceElement.REDUCED_ELECTRICITY_TAX))
return getPrices(Arrays.stream(PriceComponent.values())
.filter(component -> component != (isReducedElectricityTax ? PriceComponent.ELECTRICITY_TAX
: PriceComponent.REDUCED_ELECTRICITY_TAX))
.collect(Collectors.toSet()));
}

@RuleAction(label = "@text/action.get-prices.label", description = "@text/action.get-prices.description")
public @ActionOutput(name = "prices", type = "java.util.Map<java.time.Instant, java.math.BigDecimal>") Map<Instant, BigDecimal> getPrices(
@ActionInput(name = "priceElements", label = "@text/action.get-prices.priceElements.label", description = "@text/action.get-prices.priceElements.description") @Nullable String priceElements) {
if (priceElements == null) {
logger.warn("Argument 'priceElements' is null");
@ActionInput(name = "priceComponents", label = "@text/action.get-prices.priceComponents.label", description = "@text/action.get-prices.priceComponents.description") @Nullable String priceComponents) {
if (priceComponents == null) {
logger.warn("Argument 'priceComponents' is null");
return Map.of();
}

Set<PriceElement> priceElementsSet;
Set<PriceComponent> priceComponentsSet;
try {
priceElementsSet = new HashSet<PriceElement>(
Arrays.stream(priceElements.split(",")).map(PriceElement::fromString).toList());
priceComponentsSet = new HashSet<PriceComponent>(
Arrays.stream(priceComponents.split(",")).map(PriceComponent::fromString).toList());
} catch (IllegalArgumentException e) {
logger.warn("{}", e.getMessage());
return Map.of();
}

return getPrices(priceElementsSet);
return getPrices(priceComponentsSet);
}

@RuleAction(label = "@text/action.calculate-price.label", description = "@text/action.calculate-price.description")
Expand Down Expand Up @@ -233,7 +233,7 @@ public static PriceElement fromString(final String name) {
}
}

private Map<Instant, BigDecimal> getPrices(Set<PriceElement> priceElements) {
private Map<Instant, BigDecimal> getPrices(Set<PriceComponent> priceComponents) {
EnergiDataServiceHandler handler = this.handler;
if (handler == null) {
logger.warn("EnergiDataServiceActions ThingHandler is null.");
Expand All @@ -242,8 +242,8 @@ private Map<Instant, BigDecimal> getPrices(Set<PriceElement> priceElements) {

Map<Instant, BigDecimal> prices;
boolean spotPricesRequired;
if (priceElements.contains(PriceElement.SPOT_PRICE)) {
if (priceElements.size() > 1 && !handler.getCurrency().equals(CURRENCY_DKK)) {
if (priceComponents.contains(PriceComponent.SPOT_PRICE)) {
if (priceComponents.size() > 1 && !handler.getCurrency().equals(CURRENCY_DKK)) {
logger.warn("Cannot calculate sum when spot price currency is {}", handler.getCurrency());
return Map.of();
}
Expand All @@ -254,13 +254,13 @@ private Map<Instant, BigDecimal> getPrices(Set<PriceElement> priceElements) {
prices = new HashMap<>();
}

for (PriceElement priceElement : PriceElement.values()) {
DatahubTariff datahubTariff = priceElement.getDatahubTariff();
for (PriceComponent priceComponent : PriceComponent.values()) {
DatahubTariff datahubTariff = priceComponent.getDatahubTariff();
if (datahubTariff == null) {
continue;
}

if (priceElements.contains(priceElement)) {
if (priceComponents.contains(priceComponent)) {
Map<Instant, BigDecimal> tariffMap = handler.getTariffs(datahubTariff);
mergeMaps(prices, tariffMap, !spotPricesRequired);
}
Expand All @@ -287,13 +287,13 @@ private void mergeMaps(Map<Instant, BigDecimal> destinationMap, Map<Instant, Big
* Static get prices method for DSL rule compatibility.
*
* @param actions
* @param priceElements Comma-separated list of price elements to include in prices.
* @param priceComponents Comma-separated list of price components to include in prices.
* @return Map of prices
*/
public static Map<Instant, BigDecimal> getPrices(@Nullable ThingActions actions, @Nullable String priceElements) {
public static Map<Instant, BigDecimal> getPrices(@Nullable ThingActions actions, @Nullable String priceComponents) {
if (actions instanceof EnergiDataServiceActions serviceActions) {
if (priceElements != null && !priceElements.isBlank()) {
return serviceActions.getPrices(priceElements);
if (priceComponents != null && !priceComponents.isBlank()) {
return serviceActions.getPrices(priceComponents);
} else {
return serviceActions.getPrices();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ action.calculate-price.label = calculate price
action.calculate-price.description = calculate price for power consumption in period excl. VAT
action.get-prices.label = get prices
action.get-prices.description = get hourly prices excl. VAT
action.get-prices.priceElements.label = price elements
action.get-prices.priceElements.description = comma-separated list of price elements to include in sums
action.get-prices.priceComponents.label = price components
action.get-prices.priceComponents.description = comma-separated list of price components to include in sums
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void getPricesTotalReducedElectricityTax() throws IOException {
}

@Test
void getPricesTotalAllElements() throws IOException {
void getPricesTotalAllComponents() throws IOException {
mockCommonDatasets(actions);

Map<Instant, BigDecimal> actual = actions
Expand All @@ -210,7 +210,7 @@ void getPricesTotalAllElements() throws IOException {
}

@Test
void getPricesInvalidPriceElement() throws IOException {
void getPricesInvalidPriceComponent() throws IOException {
mockCommonDatasets(actions);

Map<Instant, BigDecimal> actual = actions.getPrices("spotprice,nettarif");
Expand Down

0 comments on commit b2b7602

Please sign in to comment.