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.nest.internal.wwn.handler;
15 import static org.openhab.binding.nest.internal.wwn.WWNBindingConstants.*;
16 import static org.openhab.core.types.RefreshType.REFRESH;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nest.internal.wwn.config.WWNStructureConfiguration;
21 import org.openhab.binding.nest.internal.wwn.dto.WWNStructure;
22 import org.openhab.binding.nest.internal.wwn.dto.WWNStructure.HomeAwayState;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Deals with the structures on the WWN API, turning them into a thing in openHAB.
36 * @author David Bennett - Initial contribution
37 * @author Wouter Born - Handle channel refresh command
40 public class WWNStructureHandler extends WWNBaseHandler<WWNStructure> {
41 private final Logger logger = LoggerFactory.getLogger(WWNStructureHandler.class);
43 private @Nullable String structureId;
45 public WWNStructureHandler(Thing thing) {
46 super(thing, WWNStructure.class);
50 protected State getChannelState(ChannelUID channelUID, WWNStructure structure) {
51 switch (channelUID.getId()) {
53 return getAsStringTypeOrNull(structure.getAway());
54 case CHANNEL_CO_ALARM_STATE:
55 return getAsStringTypeOrNull(structure.getCoAlarmState());
56 case CHANNEL_COUNTRY_CODE:
57 return getAsStringTypeOrNull(structure.getCountryCode());
58 case CHANNEL_ETA_BEGIN:
59 return getAsDateTimeTypeOrNull(structure.getEtaBegin());
60 case CHANNEL_PEAK_PERIOD_END_TIME:
61 return getAsDateTimeTypeOrNull(structure.getPeakPeriodEndTime());
62 case CHANNEL_PEAK_PERIOD_START_TIME:
63 return getAsDateTimeTypeOrNull(structure.getPeakPeriodStartTime());
64 case CHANNEL_POSTAL_CODE:
65 return getAsStringTypeOrNull(structure.getPostalCode());
66 case CHANNEL_RUSH_HOUR_REWARDS_ENROLLMENT:
67 return getAsOnOffTypeOrNull(structure.isRhrEnrollment());
68 case CHANNEL_SECURITY_STATE:
69 return getAsStringTypeOrNull(structure.getWwnSecurityState());
70 case CHANNEL_SMOKE_ALARM_STATE:
71 return getAsStringTypeOrNull(structure.getSmokeAlarmState());
72 case CHANNEL_TIME_ZONE:
73 return getAsStringTypeOrNull(structure.getTimeZone());
75 logger.error("Unsupported channelId '{}'", channelUID.getId());
76 return UnDefType.UNDEF;
81 public String getId() {
82 return getStructureId();
85 private String getStructureId() {
86 String localStructureId = structureId;
87 if (localStructureId == null) {
88 localStructureId = getConfigAs(WWNStructureConfiguration.class).structureId;
89 structureId = localStructureId;
91 return localStructureId;
95 * Handles updating the details on this structure by sending the request all the way
98 * @param channelUID the channel to update
99 * @param command the command to apply
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 if (REFRESH.equals(command)) {
104 WWNStructure lastUpdate = getLastUpdate();
105 if (lastUpdate != null) {
106 updateState(channelUID, getChannelState(channelUID, lastUpdate));
108 } else if (CHANNEL_AWAY.equals(channelUID.getId())) {
109 // Change the home/away state.
110 if (command instanceof StringType) {
111 StringType cmd = (StringType) command;
112 // Set the mode to be the cmd value.
113 addUpdateRequest(NEST_STRUCTURE_UPDATE_PATH, "away", HomeAwayState.valueOf(cmd.toString()));
119 protected void update(@Nullable WWNStructure oldStructure, WWNStructure structure) {
120 logger.debug("Updating {}", getThing().getUID());
122 updateLinkedChannels(oldStructure, structure);
124 if (ThingStatus.ONLINE != thing.getStatus()) {
125 updateStatus(ThingStatus.ONLINE);