]> git.basschouten.com Git - openhab-addons.git/blob
48f3828788faf443893b62fc89a951db0acc9364
[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.nest.internal.wwn.handler;
14
15 import static org.openhab.binding.nest.internal.wwn.WWNBindingConstants.*;
16 import static org.openhab.core.types.RefreshType.REFRESH;
17
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;
32
33 /**
34  * Deals with the structures on the WWN API, turning them into a thing in openHAB.
35  *
36  * @author David Bennett - Initial contribution
37  * @author Wouter Born - Handle channel refresh command
38  */
39 @NonNullByDefault
40 public class WWNStructureHandler extends WWNBaseHandler<WWNStructure> {
41     private final Logger logger = LoggerFactory.getLogger(WWNStructureHandler.class);
42
43     private @Nullable String structureId;
44
45     public WWNStructureHandler(Thing thing) {
46         super(thing, WWNStructure.class);
47     }
48
49     @Override
50     protected State getChannelState(ChannelUID channelUID, WWNStructure structure) {
51         switch (channelUID.getId()) {
52             case CHANNEL_AWAY:
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());
74             default:
75                 logger.error("Unsupported channelId '{}'", channelUID.getId());
76                 return UnDefType.UNDEF;
77         }
78     }
79
80     @Override
81     public String getId() {
82         return getStructureId();
83     }
84
85     private String getStructureId() {
86         String localStructureId = structureId;
87         if (localStructureId == null) {
88             localStructureId = getConfigAs(WWNStructureConfiguration.class).structureId;
89             structureId = localStructureId;
90         }
91         return localStructureId;
92     }
93
94     /**
95      * Handles updating the details on this structure by sending the request all the way
96      * to Nest.
97      *
98      * @param channelUID the channel to update
99      * @param command the command to apply
100      */
101     @Override
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));
107             }
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()));
114             }
115         }
116     }
117
118     @Override
119     protected void update(@Nullable WWNStructure oldStructure, WWNStructure structure) {
120         logger.debug("Updating {}", getThing().getUID());
121
122         updateLinkedChannels(oldStructure, structure);
123
124         if (ThingStatus.ONLINE != thing.getStatus()) {
125             updateStatus(ThingStatus.ONLINE);
126         }
127     }
128 }