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.sleepiq.internal.handler;
15 import static org.openhab.binding.sleepiq.internal.SleepIQBindingConstants.*;
17 import java.util.Collections;
20 import org.openhab.binding.sleepiq.api.SleepIQ;
21 import org.openhab.binding.sleepiq.api.model.Bed;
22 import org.openhab.binding.sleepiq.api.model.BedSideStatus;
23 import org.openhab.binding.sleepiq.api.model.BedStatus;
24 import org.openhab.binding.sleepiq.internal.config.SleepIQBedConfiguration;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingStatusInfo;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandler;
36 import org.openhab.core.thing.binding.BridgeHandler;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link SleepIQDualBedHandler} is responsible for handling channel state updates from the cloud service.
46 * @author Gregory Moyer - Initial contribution
48 public class SleepIQDualBedHandler extends BaseThingHandler implements BedStatusListener {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections.singleton(THING_TYPE_DUAL_BED);
51 private final Logger logger = LoggerFactory.getLogger(SleepIQDualBedHandler.class);
53 private volatile String bedId;
55 public SleepIQDualBedHandler(final Thing thing) {
60 public void initialize() {
61 logger.debug("Verifying SleepIQ cloud/bridge configuration");
63 Bridge bridge = getBridge();
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66 "No cloud service bridge has been configured");
70 ThingHandler handler = bridge.getHandler();
71 if (!(handler instanceof SleepIQCloudHandler)) {
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Incorrect bridge thing found");
76 logger.debug("Reading SleepIQ bed binding configuration");
77 bedId = getConfigAs(SleepIQBedConfiguration.class).bedId;
79 logger.debug("Registering SleepIQ bed status listener");
80 SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) handler;
81 cloudHandler.registerBedStatusListener(this);
83 if (ThingStatus.ONLINE != bridge.getStatus()) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
87 updateStatus(ThingStatus.ONLINE);
92 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
93 super.bridgeStatusChanged(bridgeStatusInfo);
95 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
100 private void updateProperties() {
101 logger.debug("Updating SleepIQ bed properties for bed {}", bedId);
103 SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) getBridge().getHandler();
104 Bed bed = cloudHandler.getBed(bedId);
106 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bed found with ID " + bedId);
110 updateProperties(cloudHandler.updateProperties(bed, editProperties()));
114 public void dispose() {
115 logger.debug("Disposing bed handler for bed {}", bedId);
117 Bridge bridge = getBridge();
118 if (bridge != null) {
119 BridgeHandler bridgeHandler = bridge.getHandler();
120 if (bridgeHandler instanceof SleepIQCloudHandler) {
121 ((SleepIQCloudHandler) bridgeHandler).unregisterBedStatusListener(this);
129 public void onBedStateChanged(final SleepIQ cloud, final BedStatus status) {
130 if (!status.getBedId().equals(bedId)) {
134 logger.debug("Updating left side status for bed {}", bedId);
135 BedSideStatus left = status.getLeftSide();
136 updateState(CHANNEL_LEFT_IN_BED, left.isInBed() ? OnOffType.ON : OnOffType.OFF);
137 updateState(CHANNEL_LEFT_SLEEP_NUMBER, new DecimalType(left.getSleepNumber()));
138 updateState(CHANNEL_LEFT_PRESSURE, new DecimalType(left.getPressure()));
139 updateState(CHANNEL_LEFT_LAST_LINK, new StringType(left.getLastLink().toString()));
140 updateState(CHANNEL_LEFT_ALERT_ID, new DecimalType(left.getAlertId()));
141 updateState(CHANNEL_LEFT_ALERT_DETAILED_MESSAGE, new StringType(left.getAlertDetailedMessage()));
143 logger.debug("Updating right side status for bed {}", bedId);
144 BedSideStatus right = status.getRightSide();
145 updateState(CHANNEL_RIGHT_IN_BED, right.isInBed() ? OnOffType.ON : OnOffType.OFF);
146 updateState(CHANNEL_RIGHT_SLEEP_NUMBER, new DecimalType(right.getSleepNumber()));
147 updateState(CHANNEL_RIGHT_PRESSURE, new DecimalType(right.getPressure()));
148 updateState(CHANNEL_RIGHT_LAST_LINK, new StringType(right.getLastLink().toString()));
149 updateState(CHANNEL_RIGHT_ALERT_ID, new DecimalType(right.getAlertId()));
150 updateState(CHANNEL_RIGHT_ALERT_DETAILED_MESSAGE, new StringType(right.getAlertDetailedMessage()));
154 public void handleCommand(final ChannelUID channelUID, final Command command) {
155 // all channels are read-only
157 if (command == RefreshType.REFRESH) {
158 SleepIQCloudHandler cloudHandler = (SleepIQCloudHandler) getBridge().getHandler();
159 cloudHandler.refreshBedStatus();