2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.vat.internal.profile;
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.i18n.LocaleProvider;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.thing.profiles.ProfileCallback;
24 import org.openhab.core.thing.profiles.ProfileContext;
25 import org.openhab.core.thing.profiles.ProfileTypeUID;
26 import org.openhab.core.thing.profiles.StateProfile;
27 import org.openhab.core.transform.TransformationException;
28 import org.openhab.core.transform.TransformationHelper;
29 import org.openhab.core.transform.TransformationService;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.Type;
33 import org.openhab.core.types.UnDefType;
34 import org.openhab.transform.vat.internal.config.VATConfig;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Profile to offer the {@link VATTransformationProfile} on an ItemChannelLink.
41 * @author Jacob Laursen - Initial contribution
44 public class VATTransformationProfile implements StateProfile {
46 private final Logger logger = LoggerFactory.getLogger(VATTransformationProfile.class);
48 private final ProfileCallback callback;
49 private final TransformationService service;
50 private final LocaleProvider localeProvider;
52 private VATConfig configuration;
54 public VATTransformationProfile(final ProfileCallback callback, final TransformationService service,
55 final ProfileContext context, LocaleProvider localeProvider) {
56 this.callback = callback;
57 this.service = service;
58 this.localeProvider = localeProvider;
59 this.configuration = context.getConfiguration().as(VATConfig.class);
63 public ProfileTypeUID getProfileTypeUID() {
64 return PROFILE_TYPE_UID;
68 public void onCommandFromItem(Command command) {
69 callback.handleCommand(command);
73 public void onStateUpdateFromItem(State state) {
77 public void onCommandFromHandler(Command command) {
78 callback.sendCommand((Command) transformState(command));
82 public void onStateUpdateFromHandler(State state) {
83 callback.sendUpdate((State) transformState(state));
86 private Type transformState(Type state) {
87 String result = state.toFullString();
88 String percentage = getVATPercentage();
90 result = TransformationHelper.transform(service, percentage, "%s", result);
91 } catch (TransformationException e) {
92 logger.warn("Could not apply '{}' transformation on state '{}' with value '{}'.", PROFILE_TYPE_UID.getId(),
95 Type resultType = state;
97 if (state instanceof DecimalType) {
98 resultType = DecimalType.valueOf(result);
99 } else if (state instanceof QuantityType) {
100 resultType = QuantityType.valueOf(result);
101 } else if (state instanceof UnDefType) {
102 resultType = UnDefType.valueOf(result);
104 logger.debug("Transformed '{}' into '{}'", state, resultType);
109 private String getVATPercentage() {
110 if (!configuration.percentage.isBlank()) {
111 return getOverriddenVAT();
114 String country = localeProvider.getLocale().getCountry();
115 String rate = RATES.get(country);
117 logger.warn("No VAT rate for country {}", country);
123 private String getOverriddenVAT() {
124 String percentage = configuration.percentage.trim();
125 if (percentage.endsWith("%")) {
126 percentage = percentage.substring(0, percentage.length() - 1).trim();
129 return new BigDecimal(percentage).toString();
130 } catch (NumberFormatException e) {
131 logger.warn("{} is not a valid percentage", percentage);