]> git.basschouten.com Git - openhab-addons.git/blob
e6acff23df9f46a19661330e8f136070fd72035c
[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
57         /* perform actions */
58         if (command instanceof RefreshType) {
59             refreshInfo = true;
60         } else if (command == OnOffType.ON) {
61             connector.sendDeviceCommand(DEVICE_PROPERTY_ON, true);
62             refreshInfo = true;
63         } else if (command == OnOffType.OFF) {
64             connector.sendDeviceCommand(DEVICE_PROPERTY_ON, false);
65             refreshInfo = true;
66         } else {
67             logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
68                     channelUID.getId());
69         }
70
71         /* refreshInfo */
72         if (refreshInfo) {
73             queryDeviceInfo(true);
74         }
75     }
76
77     /**
78      * UPDATE PROPERTIES
79      * 
80      * @param TapoDeviceInfo
81      */
82     @Override
83     protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
84         super.devicePropertiesChanged(deviceInfo);
85         publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
86         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
87                 getDecimalType(deviceInfo.getSignalLevel()));
88         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
89                 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
90         publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));
91     }
92 }