]> git.basschouten.com Git - openhab-addons.git/blob
72c844fada39bb48ebea3d5f4fcf733052546bfc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.homewizard.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.OnOffType;
17 import org.openhab.core.library.types.PercentType;
18 import org.openhab.core.library.types.QuantityType;
19 import org.openhab.core.library.unit.Units;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.types.Command;
23 import org.openhab.core.types.RefreshType;
24
25 /**
26  * The {@link HomeWizardEnergySocketHandler} implements functionality to handle a HomeWizard EnergySocket.
27  *
28  * @author DaniĆ«l van Os - Initial contribution
29  */
30 @NonNullByDefault
31 public class HomeWizardEnergySocketHandler extends HomeWizardStatefulDeviceHandler {
32
33     /**
34      * Constructor
35      *
36      * @param thing The thing to handle
37      */
38     public HomeWizardEnergySocketHandler(Thing thing) {
39         super(thing);
40     }
41
42     /**
43      * Converts a brightness value (0..255) to a percentage.
44      *
45      * @param brightness The brightness to convert.
46      * @return brightness percentage
47      */
48     private int brightnessToPercentage(int brightness) {
49         return (int) (100.0 * brightness / 255.0 + 0.5);
50     }
51
52     /**
53      * Converts a percentage to a brightness value (0..255)
54      *
55      * @param percentage The percentage to convert.
56      * @return brightness value
57      */
58     private int percentageToBrightness(String percentage) {
59         return (int) (Double.valueOf(percentage) * 255.0 / 100.0 + 0.5);
60     }
61
62     /**
63      * Handle incoming commands.
64      *
65      * Power on/off, Power lock/unlock and Ring brightness are supported.
66      */
67     @Override
68     public void handleCommand(ChannelUID channelUID, Command command) {
69         if (command instanceof RefreshType) {
70             // For now I prefer not updating immediately above firing a full update request for each channel
71             return;
72         }
73
74         StatePayload result = null;
75
76         /*
77          * The returned payloads below only contain the modified value, so each has it's own
78          * call to updateState instead of just calling handleStatePayload() with the returned
79          * payload.
80          */
81
82         switch (channelUID.getId()) {
83             case HomeWizardBindingConstants.CHANNEL_RING_BRIGHTNESS: {
84                 result = sendStateCommand(
85                         String.format("{\"brightness\": %d}", percentageToBrightness(command.toFullString())));
86                 if (result != null) {
87                     updateState(HomeWizardBindingConstants.CHANNEL_RING_BRIGHTNESS,
88                             new PercentType(brightnessToPercentage(result.getBrightness())));
89                 }
90                 break;
91             }
92             case HomeWizardBindingConstants.CHANNEL_POWER_SWITCH: {
93                 boolean onOff = command.equals(OnOffType.ON);
94                 result = sendStateCommand(String.format("{\"power_on\": %b}", onOff));
95                 if (result != null) {
96                     updateState(HomeWizardBindingConstants.CHANNEL_POWER_SWITCH, OnOffType.from(result.getPowerOn()));
97                 }
98                 break;
99             }
100             case HomeWizardBindingConstants.CHANNEL_POWER_LOCK: {
101                 boolean onOff = command.equals(OnOffType.ON);
102                 result = sendStateCommand(String.format("{\"switch_lock\": %b}", onOff));
103                 if (result != null) {
104                     updateState(HomeWizardBindingConstants.CHANNEL_POWER_LOCK, OnOffType.from(result.getSwitchLock()));
105                 }
106                 break;
107             }
108             default:
109                 logger.warn("Should handle {} {}", channelUID.getIdWithoutGroup(), command);
110                 break;
111         }
112     }
113
114     /**
115      * Device specific handling of the returned payload.
116      *
117      * @param payload The data parsed from the Json file
118      */
119     @Override
120     protected void handleDataPayload(DataPayload payload) {
121         updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_IMPORT_T1,
122                 new QuantityType<>(payload.getTotalEnergyImportT1Kwh(), Units.KILOWATT_HOUR));
123         updateState(HomeWizardBindingConstants.CHANNEL_ENERGY_EXPORT_T1,
124                 new QuantityType<>(payload.getTotalEnergyExportT1Kwh(), Units.KILOWATT_HOUR));
125         updateState(HomeWizardBindingConstants.CHANNEL_ACTIVE_POWER,
126                 new QuantityType<>(payload.getActivePowerW(), Units.WATT));
127     }
128
129     @Override
130     protected void handleStatePayload(StatePayload payload) {
131         updateState(HomeWizardBindingConstants.CHANNEL_POWER_SWITCH, OnOffType.from(payload.getPowerOn()));
132         updateState(HomeWizardBindingConstants.CHANNEL_POWER_LOCK, OnOffType.from(payload.getSwitchLock()));
133         updateState(HomeWizardBindingConstants.CHANNEL_RING_BRIGHTNESS,
134                 new PercentType(brightnessToPercentage(payload.getBrightness())));
135     }
136 }