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.transform.vat.internal.config.VATConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Profile to offer the {@link VATTransformationService} on an ItemChannelLink.
40 * @author Jacob Laursen - Initial contribution
43 public class VATTransformationProfile implements StateProfile {
45 private final Logger logger = LoggerFactory.getLogger(VATTransformationProfile.class);
47 private final ProfileCallback callback;
48 private final TransformationService service;
49 private final LocaleProvider localeProvider;
51 private VATConfig configuration;
53 public VATTransformationProfile(final ProfileCallback callback, final TransformationService service,
54 final ProfileContext context, LocaleProvider localeProvider) {
55 this.callback = callback;
56 this.service = service;
57 this.localeProvider = localeProvider;
58 this.configuration = context.getConfiguration().as(VATConfig.class);
62 public ProfileTypeUID getProfileTypeUID() {
63 return PROFILE_TYPE_UID;
67 public void onCommandFromItem(Command command) {
68 callback.handleCommand(command);
72 public void onStateUpdateFromItem(State state) {
76 public void onCommandFromHandler(Command command) {
77 callback.sendCommand((Command) transformState(command));
81 public void onStateUpdateFromHandler(State state) {
82 callback.sendUpdate((State) transformState(state));
85 private Type transformState(Type state) {
86 String result = state.toFullString();
88 result = TransformationHelper.transform(service, getVATPercentage(), "%s", result);
89 } catch (TransformationException e) {
90 logger.warn("Could not apply '{}' transformation on state '{}' with value '{}'.", PROFILE_TYPE_UID.getId(),
91 state, getVATPercentage());
93 Type resultType = state;
95 if (state instanceof DecimalType) {
96 resultType = DecimalType.valueOf(result);
97 } else if (state instanceof QuantityType) {
98 resultType = QuantityType.valueOf(result);
100 logger.debug("Transformed '{}' into '{}'", state, resultType);
105 private String getVATPercentage() {
106 if (!configuration.percentage.isBlank()) {
107 return getOverriddenVAT();
110 String country = localeProvider.getLocale().getCountry();
111 String rate = RATES.get(country);
113 logger.warn("No VAT rate for country {}", country);
119 private String getOverriddenVAT() {
120 String percentage = configuration.percentage.trim();
121 if (percentage.endsWith("%")) {
122 percentage = percentage.substring(0, percentage.length() - 1).trim();
125 return new BigDecimal(percentage).toString();
126 } catch (NumberFormatException e) {
127 logger.warn("{} is not a valid percentage", percentage);