]> git.basschouten.com Git - openhab-addons.git/blob
ec7eb2ea105cbf57d9f4908375065c83e4ac7bc8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.netatmo.internal.handler.capability;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
17
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
26 import org.openhab.binding.netatmo.internal.api.dto.Device;
27 import org.openhab.binding.netatmo.internal.api.dto.Event;
28 import org.openhab.binding.netatmo.internal.api.dto.HomeData;
29 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
31 import org.openhab.binding.netatmo.internal.api.dto.NAError;
32 import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
33 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
34 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
35 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
36 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
37 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.ThingHandlerService;
41 import org.openhab.core.types.Command;
42
43 /**
44  * The {@link Capability} is the base class for all inherited capabilities
45  *
46  * @author GaĆ«l L'hopital - Initial contribution
47  *
48  */
49 @NonNullByDefault
50 public class Capability {
51     protected final Thing thing;
52     protected final CommonInterface handler;
53     protected final ModuleType moduleType;
54     protected final ThingUID thingUID;
55
56     protected boolean firstLaunch;
57     protected Map<String, String> properties = Map.of();
58     protected @Nullable String statusReason;
59
60     Capability(CommonInterface handler) {
61         this.handler = handler;
62         this.thing = handler.getThing();
63         this.thingUID = thing.getUID();
64         this.moduleType = ModuleType.from(thing.getThingTypeUID());
65     }
66
67     public final @Nullable String setNewData(NAObject newData) {
68         beforeNewData();
69         if (newData instanceof NAError error) {
70             updateErrors(error);
71         } else {
72             if (newData instanceof HomeData homeData) {
73                 updateHomeData(homeData);
74             }
75             if (newData instanceof HomeStatus homeStatus) {
76                 updateHomeStatus(homeStatus);
77             }
78             if (newData instanceof HomeStatusModule homeStatusModule) {
79                 updateHomeStatusModule(homeStatusModule);
80             }
81
82             if (newData instanceof HomeEvent homeEvent) {
83                 updateHomeEvent(homeEvent);
84             } else if (newData instanceof WebhookEvent webhookEvent
85                     && webhookEvent.getEventType().validFor(moduleType)) {
86                 updateWebhookEvent(webhookEvent);
87             } else if (newData instanceof Event event) {
88                 updateEvent(event);
89             }
90
91             if (newData instanceof NAThing naThing) {
92                 updateNAThing(naThing);
93             }
94             if (newData instanceof NAMain naMain) {
95                 updateNAMain(naMain);
96             }
97             if (newData instanceof Device device) {
98                 updateNADevice(device);
99             }
100         }
101         afterNewData(newData);
102         return statusReason;
103     }
104
105     protected void beforeNewData() {
106         properties = new HashMap<>(thing.getProperties());
107         firstLaunch = properties.isEmpty();
108         if (firstLaunch) {
109             properties.put(PROPERTY_THING_TYPE_VERSION, moduleType.thingTypeVersion);
110             if (!moduleType.isLogical()) {
111                 String name = moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName;
112                 properties.put(PROPERTY_MODEL_ID, name);
113                 properties.put(PROPERTY_VENDOR, VENDOR);
114             }
115         }
116         statusReason = null;
117     }
118
119     protected void afterNewData(@Nullable NAObject newData) {
120         if (!properties.equals(thing.getProperties())) {
121             thing.setProperties(properties);
122         }
123     }
124
125     protected void updateNAThing(NAThing newData) {
126         String firmware = newData.getFirmware();
127         if (firmware != null && !firmware.isBlank()) {
128             properties.put(PROPERTY_FIRMWARE_VERSION, firmware);
129         }
130         if (!newData.isReachable()) {
131             statusReason = "@text/device-not-connected";
132         }
133     }
134
135     protected void updateNAMain(NAMain newData) {
136         // do nothing by default, can be overridden by subclasses
137     }
138
139     protected void updateHomeEvent(HomeEvent newData) {
140         // do nothing by default, can be overridden by subclasses
141     }
142
143     protected void updateHomeStatus(HomeStatus newData) {
144         // do nothing by default, can be overridden by subclasses
145     }
146
147     protected void updateHomeData(HomeData newData) {
148         // do nothing by default, can be overridden by subclasses
149     }
150
151     protected void updateEvent(Event newData) {
152         // do nothing by default, can be overridden by subclasses
153     }
154
155     protected void updateWebhookEvent(WebhookEvent newData) {
156         // do nothing by default, can be overridden by subclasses
157     }
158
159     protected void updateNADevice(Device newData) {
160         // do nothing by default, can be overridden by subclasses
161     }
162
163     protected void updateErrors(NAError error) {
164         // do nothing by default, can be overridden by subclasses
165     }
166
167     public void initialize() {
168         // do nothing by default, can be overridden by subclasses
169     }
170
171     public void expireData() {
172         if (!handler.getCapabilities().containsKey(RefreshCapability.class)) {
173             CommonInterface bridgeHandler = handler.getBridgeHandler();
174             if (bridgeHandler != null) {
175                 bridgeHandler.expireData();
176             }
177         }
178     }
179
180     public void dispose() {
181         // do nothing by default, can be overridden by subclasses
182     }
183
184     public void updateHomeStatusModule(HomeStatusModule newData) {
185         // do nothing by default, can be overridden by subclasses
186     }
187
188     public void handleCommand(String channelName, Command command) {
189         // do nothing by default, can be overridden by subclasses
190     }
191
192     public Collection<Class<? extends ThingHandlerService>> getServices() {
193         return List.of();
194     }
195
196     public List<NAObject> updateReadings() {
197         return List.of();
198     }
199 }