]> git.basschouten.com Git - openhab-addons.git/blob
50e09ab5fe4d9e2d51bb0de3b93e0d5d6fc66968
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tapocontrol.internal.device;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.tapocontrol.internal.structures.TapoDeviceInfo;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.unit.Units;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * TAPO Smart-Plug-Device.
31  *
32  * @author Christian Wild - Initial contribution
33  */
34 @NonNullByDefault
35 public class TapoSmartPlug extends TapoDevice {
36     private final Logger logger = LoggerFactory.getLogger(TapoSmartPlug.class);
37
38     /**
39      * Constructor
40      *
41      * @param thing Thing object representing device
42      */
43     public TapoSmartPlug(Thing thing) {
44         super(thing);
45     }
46
47     /**
48      * handle command sent to device
49      *
50      * @param channelUID channelUID command is sent to
51      * @param command command to be sent
52      */
53     @Override
54     public void handleCommand(ChannelUID channelUID, Command command) {
55         boolean refreshInfo = false;
56         String id = channelUID.getIdWithoutGroup();
57
58         /* perform actions */
59         if (command instanceof RefreshType) {
60             refreshInfo = true;
61         } else if (command instanceof OnOffType) {
62             Boolean targetState = command == OnOffType.ON ? Boolean.TRUE : Boolean.FALSE;
63             if (CHANNEL_OUTPUT.equals(id)) { // Command is sent to the device output
64                 connector.sendDeviceCommand(DEVICE_PROPERTY_ON, targetState);
65                 refreshInfo = true;
66             } else if (id.startsWith(CHANNEL_OUTPUT)) { // Command is sent to a child's device output
67                 Integer index = Integer.valueOf(id.replace(CHANNEL_OUTPUT, ""));
68                 connector.sendChildCommand(index, DEVICE_PROPERTY_ON, targetState);
69                 refreshInfo = true;
70             }
71         } else {
72             logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command, channelUID.getId());
73         }
74
75         /* refreshInfo */
76         if (refreshInfo) {
77             queryDeviceInfo(true);
78         }
79     }
80
81     /**
82      * UPDATE PROPERTIES
83      *
84      * @param TapoDeviceInfo
85      */
86     @Override
87     protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
88         super.devicePropertiesChanged(deviceInfo);
89         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
90         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
91                 getDecimalType(deviceInfo.getSignalLevel()));
92         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
93                 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
94         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));
95     }
96 }