|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 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 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.binding.senseenergy.internal.actions; |
| 14 | + |
| 15 | +import static org.openhab.binding.senseenergy.internal.SenseEnergyBindingConstants.*; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeoutException; |
| 23 | + |
| 24 | +import javax.measure.quantity.Dimensionless; |
| 25 | +import javax.measure.quantity.Energy; |
| 26 | + |
| 27 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 28 | +import org.eclipse.jdt.annotation.Nullable; |
| 29 | +import org.openhab.binding.senseenergy.internal.api.SenseEnergyApi; |
| 30 | +import org.openhab.binding.senseenergy.internal.api.SenseEnergyApiException; |
| 31 | +import org.openhab.binding.senseenergy.internal.api.dto.SenseEnergyApiGetTrends; |
| 32 | +import org.openhab.binding.senseenergy.internal.handler.SenseEnergyMonitorHandler; |
| 33 | +import org.openhab.core.automation.annotation.ActionInput; |
| 34 | +import org.openhab.core.automation.annotation.ActionOutput; |
| 35 | +import org.openhab.core.automation.annotation.RuleAction; |
| 36 | +import org.openhab.core.library.types.QuantityType; |
| 37 | +import org.openhab.core.library.unit.Units; |
| 38 | +import org.openhab.core.thing.binding.ThingActions; |
| 39 | +import org.openhab.core.thing.binding.ThingActionsScope; |
| 40 | +import org.openhab.core.thing.binding.ThingHandler; |
| 41 | +import org.osgi.service.component.annotations.Component; |
| 42 | +import org.osgi.service.component.annotations.ServiceScope; |
| 43 | +import org.slf4j.Logger; |
| 44 | +import org.slf4j.LoggerFactory; |
| 45 | + |
| 46 | +/** |
| 47 | + * The { @link SenseEnergyMonitorActions } class implements the action(s) methods for the binding. |
| 48 | + * |
| 49 | + * @author Jeff James - Initial contribution |
| 50 | + */ |
| 51 | +@Component(scope = ServiceScope.PROTOTYPE, service = SenseEnergyMonitorActions.class) |
| 52 | +@ThingActionsScope(name = "senseenergy") |
| 53 | +@NonNullByDefault |
| 54 | +public class SenseEnergyMonitorActions implements ThingActions { |
| 55 | + private final Logger logger = LoggerFactory.getLogger(SenseEnergyMonitorActions.class); |
| 56 | + |
| 57 | + private @Nullable SenseEnergyMonitorHandler deviceHandler; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void setThingHandler(@Nullable ThingHandler handler) { |
| 61 | + if (handler instanceof SenseEnergyMonitorHandler deviceHandler) { |
| 62 | + this.deviceHandler = deviceHandler; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public @Nullable ThingHandler getThingHandler() { |
| 68 | + return deviceHandler; |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + * Query water usage |
| 73 | + */ |
| 74 | + @RuleAction(label = "Query Energy Trend", description = "@text/actions.description.query-energy-trend") |
| 75 | + public @ActionOutput(name = ACTION_OUTPUT_CONSUMPTION, type = "QuantityType<Energy>", description = "@text/actions.output.description.consumption") // |
| 76 | + @ActionOutput(name = ACTION_OUTPUT_PRODUCTION, type = "QuantityType<Energy>", description = "@text/actions.output.description.production") // |
| 77 | + @ActionOutput(name = ACTION_OUTPUT_FROM_GRID, type = "QuantityType<Energy>", description = "@text/actions.output.description.from-grid") // |
| 78 | + @ActionOutput(name = ACTION_OUTPUT_TO_GRID, type = "QuantityType<Energy>", description = "@text/actions.output.description.to-grid") // |
| 79 | + @ActionOutput(name = ACTION_OUTPUT_NET_PRODUCTION, type = "QuantityType<Energy>", description = "@text/actions.output.description.net-production") // |
| 80 | + @ActionOutput(name = ACTION_OUTPUT_SOLAR_POWERED, type = "QuantityType<Dimensionless>", description = "@text/actions.output.description.solar-powered") // |
| 81 | + Map<String, QuantityType<?>> queryEnergyTrend( // |
| 82 | + @ActionInput(name = ACTION_INPUT_SCALE, label = "Scale", required = true, description = "@text/actions.input.description.scale") @Nullable String scale, // |
| 83 | + @ActionInput(name = ACTION_INPUT_DATETIME, label = "Date/Time", required = true, description = "@text/actions.input.description.datetime") @Nullable Instant datetime) { |
| 84 | + logger.info("queryEnergyTrend called"); |
| 85 | + |
| 86 | + SenseEnergyMonitorHandler localDeviceHandler = deviceHandler; |
| 87 | + if (localDeviceHandler == null) { |
| 88 | + logger.warn("querying device usage, but device is undefined."); |
| 89 | + return Collections.emptyMap(); |
| 90 | + } |
| 91 | + |
| 92 | + Instant localDateTime = (datetime == null) ? Instant.now() : datetime; |
| 93 | + |
| 94 | + if (scale == null) { |
| 95 | + logger.warn("queryEnergyTrends called with null inputs"); |
| 96 | + return Collections.emptyMap(); |
| 97 | + } |
| 98 | + |
| 99 | + if (!SenseEnergyApi.TrendScale.contains(scale)) { |
| 100 | + logger.warn("Invalid scale type in call to queryEnergyTrend"); |
| 101 | + return Collections.emptyMap(); |
| 102 | + } |
| 103 | + SenseEnergyApi.TrendScale trendScale = SenseEnergyApi.TrendScale.valueOf(scale); |
| 104 | + |
| 105 | + SenseEnergyApiGetTrends trends; |
| 106 | + try { |
| 107 | + trends = localDeviceHandler.getApi().getTrendData(localDeviceHandler.getId(), trendScale, localDateTime); |
| 108 | + } catch (InterruptedException | TimeoutException | ExecutionException | SenseEnergyApiException e) { |
| 109 | + logger.warn("queryEnergyTrends function failed - {}", e.getMessage()); |
| 110 | + return Collections.emptyMap(); |
| 111 | + } |
| 112 | + |
| 113 | + if (trends == null) { |
| 114 | + return Collections.emptyMap(); |
| 115 | + } |
| 116 | + |
| 117 | + Map<String, QuantityType<?>> valuesMap = new HashMap<>(); |
| 118 | + |
| 119 | + valuesMap.put(ACTION_OUTPUT_CONSUMPTION, |
| 120 | + new QuantityType<Energy>(trends.consumption.totalPower, Units.KILOWATT_HOUR)); |
| 121 | + valuesMap.put(ACTION_OUTPUT_PRODUCTION, |
| 122 | + new QuantityType<Energy>(trends.production.totalPower, Units.KILOWATT_HOUR)); |
| 123 | + valuesMap.put(ACTION_OUTPUT_TO_GRID, new QuantityType<Energy>(trends.toGridEnergy, Units.KILOWATT_HOUR)); |
| 124 | + valuesMap.put(ACTION_OUTPUT_FROM_GRID, new QuantityType<Energy>(trends.fromGridEnergy, Units.KILOWATT_HOUR)); |
| 125 | + valuesMap.put(ACTION_OUTPUT_NET_PRODUCTION, |
| 126 | + new QuantityType<Energy>(trends.netProduction, Units.KILOWATT_HOUR)); |
| 127 | + valuesMap.put(ACTION_OUTPUT_SOLAR_POWERED, new QuantityType<Dimensionless>(trends.solarPowered, Units.PERCENT)); |
| 128 | + |
| 129 | + return valuesMap; |
| 130 | + } |
| 131 | + |
| 132 | + // Static method for Rules DSL backward compatibility |
| 133 | + public static @Nullable Map<String, QuantityType<?>> queryEnergyTrend(ThingActions actions, @Nullable String scale, |
| 134 | + @Nullable Instant datetime) { |
| 135 | + if (actions instanceof SenseEnergyMonitorActions localActions) { |
| 136 | + return localActions.queryEnergyTrend(scale, datetime); |
| 137 | + } else { |
| 138 | + throw new IllegalArgumentException("Instance is not a SenseEnergyMonitorActions class."); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments