Define grid tariff filters in YAML (#17690)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-11-10 19:37:54 +01:00 committed by Ciprian Pascu
parent 268eabdcec
commit d8beff5a18
11 changed files with 613 additions and 195 deletions

View File

@ -1,186 +0,0 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api;
import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*;
import java.time.LocalDate;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Factory for creating a {@link DatahubTariffFilter} for a specific Grid Company GLN.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DatahubTariffFilterFactory {
private static final String GLN_AAL_ELNET = "5790001095451";
private static final String GLN_CERIUS = "5790000705184";
private static final String GLN_DINEL = "5790000610099";
private static final String GLN_ELEKTRUS = "5790000836239";
private static final String GLN_ELINORD = "5790001095277";
private static final String GLN_ELNET_MIDT = "5790001100520";
private static final String GLN_ELNET_KONGERSLEV = "5790002502699";
private static final String GLN_FLOW_ELNET = "5790000392551";
private static final String GLN_HAMMEL_ELFORSYNING_NET = "5790001090166";
private static final String GLN_HURUP_ELVAERK_NET = "5790000610839";
private static final String GLN_IKAST_EL_NET = "5790000682102";
private static final String GLN_KONSTANT = "5790000704842";
private static final String GLN_L_NET = "5790001090111";
private static final String GLN_MIDTFYNS_ELFORSYNING = "5790001089023";
private static final String GLN_N1 = "5790001089030";
private static final String GLN_NETSELSKABET_ELVAERK = "5790000681075";
private static final String GLN_NKE_ELNET = "5790001088231";
private static final String GLN_NORD_ENERGI_NET = "5790000610877";
private static final String GLN_NORDVESTJYSK_ELFORSYNING_NOE_NET = "5790000395620";
private static final String GLN_RADIUS = "5790000705689";
private static final String GLN_RAH_NET = "5790000681327";
private static final String GLN_RAVDEX = "5790000836727";
private static final String GLN_SUNDS_NET = "5790001095444";
private static final String GLN_TARM_ELVAERK_NET = "5790000706419";
private static final String GLN_TREFOR_EL_NET = "5790000392261";
private static final String GLN_TREFOR_EL_NET_OEST = "5790000706686";
private static final String GLN_VEKSEL = "5790001088217";
private static final String GLN_VORES_ELNET = "5790000610976";
private static final String GLN_ZEANET = "5790001089375";
private static final String NOTE_NET_TARIFF = "Nettarif";
private static final String NOTE_NET_TARIFF_C = NOTE_NET_TARIFF + " C";
private static final String NOTE_NET_TARIFF_C_HOUR = NOTE_NET_TARIFF_C + " time";
private static final String NOTE_NET_TARIFF_C_FLEX = NOTE_NET_TARIFF_C + " Flex";
private static final String NOTE_NET_TARIFF_C_FLEX_HOUR = NOTE_NET_TARIFF_C_FLEX + " - time";
private static final String NOTE_SYSTEM_TARIFF = "Systemtarif";
private static final String NOTE_ELECTRICITY_TAX = "Elafgift";
private static final String NOTE_REDUCED_ELECTRICITY_TAX = "Reduceret elafgift";
private static final String NOTE_TRANSMISSION_NET_TARIFF = "Transmissions nettarif";
public static final LocalDate IKAST_EL_NET_CUTOFF_DATE = LocalDate.of(2022, 10, 1);
public static final LocalDate KONSTANT_CUTOFF_DATE = LocalDate.of(2023, 2, 1);
public static final LocalDate N1_CUTOFF_DATE = LocalDate.of(2023, 1, 1);
public static final LocalDate RADIUS_CUTOFF_DATE = LocalDate.of(2024, 4, 1);
public static DatahubTariffFilter getGridTariffByGLN(String globalLocationNumber) {
switch (globalLocationNumber) {
case GLN_AAL_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("AAL-NT-05"), ChargeTypeCode.of("AAL-NTR05")),
Set.of(NOTE_NET_TARIFF_C_HOUR), DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_CERIUS:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("30TR_C_ET")), Set.of(NOTE_NET_TARIFF_C_HOUR));
case GLN_DINEL:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TCL>100_02"), ChargeTypeCode.of("TCL<100_52")),
Set.of(NOTE_NET_TARIFF_C_HOUR));
case GLN_ELEKTRUS:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("6000091")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ELINORD:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43300")),
Set.of("Transportbetaling, eget net C"));
case GLN_ELNET_MIDT:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("T3001")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ELNET_KONGERSLEV:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("K_22100")), Set.of(NOTE_NET_TARIFF_C));
case GLN_FLOW_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("FE2 NT-01")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_HAMMEL_ELFORSYNING_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("50001")), Set.of("Overliggende net"),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_HURUP_ELVAERK_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("HEV-NT-01")), Set.of(NOTE_NET_TARIFF));
case GLN_IKAST_EL_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("IEV-NT-01")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(IKAST_EL_NET_CUTOFF_DATE));
case GLN_KONSTANT:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("151-NT01T"), ChargeTypeCode.of("151-NRA04T")),
Set.of(), DateQueryParameter.of(KONSTANT_CUTOFF_DATE));
case GLN_L_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("4010")), Set.of(NOTE_NET_TARIFF_C_HOUR));
case GLN_MIDTFYNS_ELFORSYNING:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT15000")), Set.of(NOTE_NET_TARIFF_C_FLEX),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_N1:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("CD"), ChargeTypeCode.of("CD R")), Set.of(),
DateQueryParameter.of(N1_CUTOFF_DATE));
case GLN_NETSELSKABET_ELVAERK:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("0NCFF")), Set.of(NOTE_NET_TARIFF_C + " Flex"),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NKE_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("94TR_C_ET")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NORD_ENERGI_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TAC")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NORDVESTJYSK_ELFORSYNING_NOE_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("30030")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_RADIUS:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("DT_C_01")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(RADIUS_CUTOFF_DATE));
case GLN_RAH_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("RAH-C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_RAVDEX:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_SUNDS_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("SEF-NT-05"), ChargeTypeCode.of("SEF-NT-05R")),
Set.of(NOTE_NET_TARIFF_C_FLEX_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_TARM_ELVAERK_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TEV-NT-01"), ChargeTypeCode.of("TEV-NT-01R")),
Set.of(NOTE_NET_TARIFF_C));
case GLN_TREFOR_EL_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_TREFOR_EL_NET_OEST:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("46")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_VEKSEL:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-10")),
Set.of(NOTE_NET_TARIFF_C_HOUR + " NT-10"));
case GLN_VORES_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT1009")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ZEANET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43110")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
default:
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR));
}
}
public static DatahubTariffFilter getSystemTariff() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_SYSTEM_TARIFF),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getTransmissionGridTariff() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_TRANSMISSION_NET_TARIFF),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getElectricityTax() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_ELECTRICITY_TAX),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getReducedElectricityTax() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_REDUCED_ELECTRICITY_TAX),
DateQueryParameter.of(LocalDate.of(2021, 2, 1)));
}
}

View File

@ -37,4 +37,13 @@ public enum DateQueryParameterType {
public String toString() {
return name;
}
public static DateQueryParameterType of(String name) {
for (DateQueryParameterType type : values()) {
if (type.name.equals(name)) {
return type;
}
}
throw new IllegalArgumentException("Unknown date query parameter: " + name);
}
}

View File

@ -0,0 +1,113 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.filter;
import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.energidataservice.internal.api.DatahubTariffFilter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
import org.openhab.binding.energidataservice.internal.api.filter.dto.DatahubFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
/**
* Factory for creating a {@link DatahubTariffFilter} for a specific Grid Company GLN.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DatahubTariffFilterFactory {
private static final String RESOURCE_NAME = "/filters/grid_tariffs.yaml";
private static final String NOTE_NET_TARIFF_C = "Nettarif C";
private static final String NOTE_SYSTEM_TARIFF = "Systemtarif";
private static final String NOTE_ELECTRICITY_TAX = "Elafgift";
private static final String NOTE_REDUCED_ELECTRICITY_TAX = "Reduceret elafgift";
private static final String NOTE_TRANSMISSION_NET_TARIFF = "Transmissions nettarif";
private final Logger logger = LoggerFactory.getLogger(DatahubTariffFilterFactory.class);
private final Map<String, DatahubFilter> filterMap = getMap();
private Map<String, DatahubFilter> getMap() {
HashMap<String, DatahubFilter> filterMap = new HashMap<>();
Collection<DatahubFilter> filters = parseResource();
for (DatahubFilter filter : filters) {
filterMap.put(filter.gln(), filter);
}
return filterMap;
}
private Collection<DatahubFilter> parseResource() {
try (InputStream inputStream = DatahubTariffFilterFactory.class.getResourceAsStream(RESOURCE_NAME)) {
if (inputStream == null) {
throw new IllegalStateException("Grid tariffs resource not found");
}
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.registerModule(new JavaTimeModule());
return mapper.readValue(inputStream,
mapper.getTypeFactory().constructCollectionType(List.class, DatahubFilter.class));
} catch (IOException e) {
throw new IllegalStateException("Grid tariffs resource could not be read and parsed", e);
}
}
public DatahubTariffFilter getGridTariffByGLN(String globalLocationNumber) {
DatahubFilter datahubFilter = filterMap.get(globalLocationNumber);
if (datahubFilter != null) {
logger.trace("Found filter in YAML resource: {}", datahubFilter);
return new DatahubTariffFilter(datahubFilter.chargeTypeCodes(), datahubFilter.notes(),
datahubFilter.start());
}
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR));
}
public static DatahubTariffFilter getSystemTariff() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_SYSTEM_TARIFF),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getTransmissionGridTariff() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_TRANSMISSION_NET_TARIFF),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getElectricityTax() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_ELECTRICITY_TAX),
DateQueryParameter.of(ENERGINET_CUTOFF_DATE));
}
public static DatahubTariffFilter getReducedElectricityTax() {
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_REDUCED_ELECTRICITY_TAX),
DateQueryParameter.of(LocalDate.of(2021, 2, 1)));
}
}

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.filter.dto;
import java.util.Objects;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
import org.openhab.binding.energidataservice.internal.api.filter.serialization.ChargeTypeCodeDeserializer;
import org.openhab.binding.energidataservice.internal.api.filter.serialization.DateQueryParameterDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
/**
* Record for deserializing YAML grid tariff definitions.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public record DatahubFilter(String gln,
@JsonDeserialize(contentUsing = ChargeTypeCodeDeserializer.class) Set<ChargeTypeCode> chargeTypeCodes,
Set<String> notes, @JsonDeserialize(using = DateQueryParameterDeserializer.class) DateQueryParameter start) {
@Override
public DateQueryParameter start() {
return Objects.isNull(start) ? DateQueryParameter.EMPTY : start;
}
@Override
public Set<String> notes() {
return Objects.isNull(notes) ? Set.of() : notes;
}
@Override
public String toString() {
return "DatahubFilter{gln='" + gln + "', chargeTypeCodes=" + chargeTypeCodes + "', notes=" + notes + "', start="
+ start + '}';
}
}

View File

@ -0,0 +1,52 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.filter.serialization;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
/**
* The {@link ChargeTypeCodeDeserializer} converts a charge type code string
* into a {@link ChargeTypeCode} object.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class ChargeTypeCodeDeserializer extends StdDeserializer<ChargeTypeCode> {
private static final long serialVersionUID = 1L;
public ChargeTypeCodeDeserializer() {
this(null);
}
public ChargeTypeCodeDeserializer(@Nullable Class<?> vc) {
super(vc);
}
@Override
public ChargeTypeCode deserialize(@Nullable JsonParser p, @Nullable DeserializationContext ctxt)
throws IOException, JacksonException {
if (p == null) {
throw new IllegalArgumentException("JsonParser is null");
}
return ChargeTypeCode.of(p.getText());
}
}

View File

@ -0,0 +1,62 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.filter.serialization;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
/**
* The {@link DateQueryParameterDeserializer} converts a string representation
* of either a {@link LocalDate} or {@link DateQueryParameterType} into a
* {@link DateQueryParameter}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DateQueryParameterDeserializer extends StdDeserializer<DateQueryParameter> {
private static final long serialVersionUID = 1L;
public DateQueryParameterDeserializer() {
this(null);
}
public DateQueryParameterDeserializer(@Nullable Class<?> vc) {
super(vc);
}
@Override
public DateQueryParameter deserialize(@Nullable JsonParser p, @Nullable DeserializationContext ctxt)
throws IOException, JacksonException {
if (p == null) {
throw new IllegalArgumentException("JsonParser is null");
}
String value = p.getText();
try {
LocalDate date = LocalDate.parse(value);
return DateQueryParameter.of(date);
} catch (DateTimeParseException e) {
return DateQueryParameter.of(DateQueryParameterType.of(value));
}
}
}

View File

@ -19,6 +19,7 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.energidataservice.internal.api.filter.DatahubTariffFilterFactory;
import org.openhab.binding.energidataservice.internal.handler.EnergiDataServiceHandler;
import org.openhab.binding.energidataservice.internal.provider.Co2EmissionProvider;
import org.openhab.binding.energidataservice.internal.provider.ElectricityPriceProvider;
@ -50,6 +51,7 @@ public class EnergiDataServiceHandlerFactory extends BaseThingHandlerFactory {
private final TimeZoneProvider timeZoneProvider;
private final ElectricityPriceProvider electricityPriceProvider;
private final Co2EmissionProvider co2EmissionProvider;
private final DatahubTariffFilterFactory datahubTariffFilterFactory;
@Activate
public EnergiDataServiceHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
@ -61,6 +63,7 @@ public class EnergiDataServiceHandlerFactory extends BaseThingHandlerFactory {
this.timeZoneProvider = timeZoneProvider;
this.electricityPriceProvider = electricityPriceProvider;
this.co2EmissionProvider = co2EmissionProvider;
this.datahubTariffFilterFactory = new DatahubTariffFilterFactory();
}
@Override
@ -74,7 +77,7 @@ public class EnergiDataServiceHandlerFactory extends BaseThingHandlerFactory {
if (THING_TYPE_SERVICE.equals(thingTypeUID)) {
return new EnergiDataServiceHandler(thing, httpClient, timeZoneProvider, electricityPriceProvider,
co2EmissionProvider);
co2EmissionProvider, datahubTariffFilterFactory);
}
return null;

View File

@ -44,12 +44,12 @@ import org.openhab.binding.energidataservice.internal.action.EnergiDataServiceAc
import org.openhab.binding.energidataservice.internal.api.ChargeType;
import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
import org.openhab.binding.energidataservice.internal.api.DatahubTariffFilter;
import org.openhab.binding.energidataservice.internal.api.DatahubTariffFilterFactory;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
import org.openhab.binding.energidataservice.internal.api.GlobalLocationNumber;
import org.openhab.binding.energidataservice.internal.api.dto.DatahubPricelistRecord;
import org.openhab.binding.energidataservice.internal.api.dto.ElspotpriceRecord;
import org.openhab.binding.energidataservice.internal.api.filter.DatahubTariffFilterFactory;
import org.openhab.binding.energidataservice.internal.config.DatahubPriceConfiguration;
import org.openhab.binding.energidataservice.internal.config.EnergiDataServiceConfiguration;
import org.openhab.binding.energidataservice.internal.exception.DataServiceException;
@ -101,18 +101,21 @@ public class EnergiDataServiceHandler extends BaseThingHandler
private final ApiController apiController;
private final ElectricityPriceProvider electricityPriceProvider;
private final Co2EmissionProvider co2EmissionProvider;
private final DatahubTariffFilterFactory datahubTariffFilterFactory;
private final Set<Subscription> activeSubscriptions = new HashSet<>();
private EnergiDataServiceConfiguration config;
public EnergiDataServiceHandler(final Thing thing, final HttpClient httpClient,
final TimeZoneProvider timeZoneProvider, final ElectricityPriceProvider electricityPriceProvider,
final Co2EmissionProvider co2EmissionProvider) {
final Co2EmissionProvider co2EmissionProvider,
final DatahubTariffFilterFactory datahubTariffFilterFactory) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
this.apiController = new ApiController(httpClient, timeZoneProvider);
this.electricityPriceProvider = electricityPriceProvider;
this.co2EmissionProvider = co2EmissionProvider;
this.datahubTariffFilterFactory = datahubTariffFilterFactory;
// Default configuration
this.config = new EnergiDataServiceConfiguration();
@ -376,21 +379,21 @@ public class EnergiDataServiceHandler extends BaseThingHandler
private DatahubTariffFilter getGridTariffFilter() {
Channel channel = getThing().getChannel(CHANNEL_GRID_TARIFF);
if (channel == null) {
return DatahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
return datahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
}
DatahubPriceConfiguration datahubPriceConfiguration = channel.getConfiguration()
.as(DatahubPriceConfiguration.class);
if (!datahubPriceConfiguration.hasAnyFilterOverrides()) {
return DatahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
return datahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
}
DateQueryParameter start = datahubPriceConfiguration.getStart();
if (start == null) {
logger.warn("Invalid channel configuration parameter 'start' or 'offset': {} (offset: {})",
datahubPriceConfiguration.start, datahubPriceConfiguration.offset);
return DatahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
return datahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN);
}
Set<ChargeTypeCode> chargeTypeCodes = datahubPriceConfiguration.getChargeTypeCodes();
@ -401,7 +404,7 @@ public class EnergiDataServiceHandler extends BaseThingHandler
filter = new DatahubTariffFilter(chargeTypeCodes, notes, start);
} else {
// Only override start date in pre-configured filter.
filter = new DatahubTariffFilter(DatahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN),
filter = new DatahubTariffFilter(datahubTariffFilterFactory.getGridTariffByGLN(config.gridCompanyGLN),
start);
}

View File

@ -0,0 +1,197 @@
# All Elnet
- gln: "5790001095451"
chargeTypeCodes:
- "AAL-NT-05"
- "AAL-NTR05"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Cerius
- gln: "5790000705184"
chargeTypeCodes:
- "30TR_C_ET"
notes:
- "Nettarif C time"
# Dinel
- gln: "5790000610099"
chargeTypeCodes:
- "TCL>100_02"
- "TCL<100_52"
notes:
- "Nettarif C time"
# Elektrus
- gln: "5790000836239"
chargeTypeCodes:
- "6000091"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Elinord
- gln: "5790001095277"
chargeTypeCodes:
- "43300"
notes:
- "Transportbetaling, eget net C"
# Elnet Midt
- gln: "5790001100520"
chargeTypeCodes:
- "T3001"
notes:
- "Nettarif C"
start: "StartOfDay"
# El-net Kongerslev
- gln: "5790002502699"
chargeTypeCodes:
- "K_22100"
notes:
- "Nettarif C"
# FLOW Elnet
- gln: "5790000392551"
chargeTypeCodes:
- "FE2 NT-01"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Hammel Elforsyning Net
- gln: "5790001090166"
chargeTypeCodes:
- "50001"
notes:
- "Overliggende net"
start: "StartOfDay"
# Hurup Elværk Net
- gln: "5790000610839"
chargeTypeCodes:
- "HEV-NT-01"
notes:
- "Nettarif"
# Ikast El Net
- gln: "5790000682102"
chargeTypeCodes:
- "IEV-NT-01"
notes:
- "Nettarif C time"
start: "2022-10-01"
# Konstant
- gln: "5790000704842"
chargeTypeCodes:
- "151-NT01T"
- "151-NRA04T"
start: "2023-02-01"
# L-Net
- gln: "5790001090111"
chargeTypeCodes:
- "4010"
notes:
- "Nettarif C time"
# Midtfyns Elforsyning
- gln: "5790001089023"
chargeTypeCodes:
- "TNT15000"
notes:
- "Nettarif C Flex"
start: "StartOfDay"
# N1
- gln: "5790001089030"
chargeTypeCodes:
- "CD"
- "CD R"
start: "2023-01-01"
# Netselskabet Elværk
- gln: "5790000681075"
chargeTypeCodes:
- "0NCFF"
notes:
- "Nettarif C Flex"
start: "StartOfDay"
# NKE-Elnet
- gln: "5790001088231"
chargeTypeCodes:
- "94TR_C_ET"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Nord Energi Net
- gln: "5790000610877"
chargeTypeCodes:
- "TAC"
notes:
- "Nettarif C"
start: "StartOfDay"
# Nordvestjysk Elforsyning (NOE Net)
- gln: "5790000395620"
chargeTypeCodes:
- "30030"
notes:
- "Nettarif C"
start: "StartOfDay"
# Radius
- gln: "5790000705689"
chargeTypeCodes:
- "DT_C_01"
notes:
- "Nettarif C"
start: "2024-04-01"
# RAH
- gln: "5790000681327"
chargeTypeCodes:
- "RAH-C"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Ravdex
- gln: "5790000836727"
chargeTypeCodes:
- "NT-C"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Sunds Net
- gln: "5790001095444"
chargeTypeCodes:
- "SEF-NT-05"
- "SEF-NT-05R"
notes:
- "Nettarif C Flex - time"
start: "StartOfDay"
# Tarm Elværk Net
- gln: "5790000706419"
chargeTypeCodes:
- "TEV-NT-01"
- "TEV-NT-01R"
notes:
- "Nettarif C"
# TREFOR El-net
- gln: "5790000392261"
chargeTypeCodes:
- "C"
notes:
- "Nettarif C time"
start: "StartOfDay"
# TREFOR El-net Øst
- gln: "5790000706686"
chargeTypeCodes:
- "46"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Veksel
- gln: "5790001088217"
chargeTypeCodes:
- "NT-10"
notes:
- "Nettarif C time NT-10"
# Vores Elnet
- gln: "5790000610976"
chargeTypeCodes:
- "TNT1009"
notes:
- "Nettarif C time"
start: "StartOfDay"
# Zeanet
- gln: "5790001089375"
chargeTypeCodes:
- "43110"
notes:
- "Nettarif C time"
start: "StartOfDay"

View File

@ -0,0 +1,115 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.energidataservice.internal.api.filter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.energidataservice.internal.api.DatahubTariffFilter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
/**
* Tests for {@link DatahubTariffFilterFactory}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
public class DatahubTariffFilterFactoryTest {
private DatahubTariffFilterFactory datahubTariffFilterFactory = new DatahubTariffFilterFactory();
@Test
void getSystemTariff() {
DatahubTariffFilter actual = DatahubTariffFilterFactory.getSystemTariff();
Collection<String> chargeTypeCodes = actual.getChargeTypeCodesAsStrings();
Collection<String> notes = actual.getNotes();
assertThat(chargeTypeCodes, is(empty()));
assertThat(notes.size(), is(equalTo(1)));
assertThat(notes.stream().findFirst().get(), is(equalTo("Systemtarif")));
assertThat(actual.getStart(), is(equalTo(DateQueryParameter.of(LocalDate.of(2023, 1, 1)))));
}
@Test
void getTransmissionGridTariff() {
DatahubTariffFilter actual = DatahubTariffFilterFactory.getTransmissionGridTariff();
Collection<String> chargeTypeCodes = actual.getChargeTypeCodesAsStrings();
Collection<String> notes = actual.getNotes();
assertThat(chargeTypeCodes, is(empty()));
assertThat(notes.size(), is(equalTo(1)));
assertThat(notes.stream().findFirst().get(), is(equalTo("Transmissions nettarif")));
assertThat(actual.getStart(), is(equalTo(DateQueryParameter.of(LocalDate.of(2023, 1, 1)))));
}
@Test
void getElectricityTax() {
DatahubTariffFilter actual = DatahubTariffFilterFactory.getElectricityTax();
Collection<String> chargeTypeCodes = actual.getChargeTypeCodesAsStrings();
Collection<String> notes = actual.getNotes();
assertThat(chargeTypeCodes, is(empty()));
assertThat(notes.size(), is(equalTo(1)));
assertThat(notes.stream().findFirst().get(), is(equalTo("Elafgift")));
assertThat(actual.getStart(), is(equalTo(DateQueryParameter.of(LocalDate.of(2023, 1, 1)))));
}
@Test
void getReducedElectricityTax() {
DatahubTariffFilter actual = DatahubTariffFilterFactory.getReducedElectricityTax();
Collection<String> chargeTypeCodes = actual.getChargeTypeCodesAsStrings();
Collection<String> notes = actual.getNotes();
assertThat(chargeTypeCodes, is(empty()));
assertThat(notes.size(), is(equalTo(1)));
assertThat(notes.stream().findFirst().get(), is(equalTo("Reduceret elafgift")));
assertThat(actual.getStart(), is(equalTo(DateQueryParameter.of(LocalDate.of(2021, 2, 1)))));
}
@ParameterizedTest
@MethodSource("provideTestCasesForGetGridTariffByGLN")
void getGridTariffByGLN(String gln, DateQueryParameter expectedStart, List<String> expectedChargeTypeCodes,
List<String> expectedNotes) {
DatahubTariffFilter actual = datahubTariffFilterFactory.getGridTariffByGLN(gln);
Collection<String> chargeTypeCodes = actual.getChargeTypeCodesAsStrings();
Collection<String> notes = actual.getNotes();
assertThat(chargeTypeCodes, containsInAnyOrder(expectedChargeTypeCodes.toArray()));
assertThat(notes, containsInAnyOrder(expectedNotes.toArray()));
assertThat(actual.getStart(), is(equalTo(expectedStart)));
}
private static Stream<Arguments> provideTestCasesForGetGridTariffByGLN() {
return Stream.of( //
Arguments.of("5790001095451", DateQueryParameter.of(DateQueryParameterType.START_OF_DAY),
List.of("AAL-NT-05", "AAL-NTR05"), List.of("Nettarif C time")), //
Arguments.of("5790000705184", DateQueryParameter.EMPTY, List.of("30TR_C_ET"),
List.of("Nettarif C time")), //
Arguments.of("5790000682102", DateQueryParameter.of(LocalDate.of(2022, 10, 1)), List.of("IEV-NT-01"),
List.of("Nettarif C time")), //
Arguments.of("5790001089030", DateQueryParameter.of(LocalDate.of(2023, 1, 1)), List.of("CD", "CD R"),
List.of())); //
}
}

View File

@ -30,13 +30,13 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
/**
* Tests for {@link LocalDateDeserializer}.
* Tests for {@link LocalDateTimeDeserializer}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
public class LocalDateDeserializerTest {
public class LocalDateTimeDeserializerTest {
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).create();