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.binding.elroconnects.internal.devices;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link ElroConnectsDevicePowerSocket} is representing an ELRO Connects power socket device.
34 * @author Mark Herwege - Initial contribution
37 public class ElroConnectsDevicePowerSocket extends ElroConnectsDevice {
39 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDevicePowerSocket.class);
42 private static final String STAT_ON = "00";
43 private static final String STAT_OFF = "01";
46 protected static final String CMD_OFF = "0101FFFF";
47 protected static final String CMD_ON = "0100FFFF";
49 public ElroConnectsDevicePowerSocket(int deviceId, ElroConnectsBridgeHandler bridge) {
50 super(deviceId, bridge);
54 public void switchState(boolean state) {
56 bridge.deviceControl(deviceId, state ? CMD_ON : CMD_OFF);
57 } catch (IOException e) {
58 logger.debug("Failed to control device: {}", e.getMessage());
63 public void updateState() {
64 ElroConnectsDeviceHandler handler = getHandler();
65 if (handler == null) {
69 String deviceStatus = this.deviceStatus;
70 if (deviceStatus.length() < 6) {
71 logger.debug("Could not decode device status: {}", deviceStatus);
72 String msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
73 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
77 int signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
78 signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
79 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
81 String status = deviceStatus.substring(4, 6);
82 State state = STAT_ON.equals(status) ? OnOffType.ON
83 : (STAT_OFF.equals(status) ? OnOffType.OFF : UnDefType.UNDEF);
84 handler.updateState(POWER_STATE, state);
85 if (UnDefType.UNDEF.equals(state)) {
86 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
87 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
89 handler.updateStatus(ThingStatus.ONLINE);
94 public void testAlarm() {
99 public void muteAlarm() {