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.netatmo.internal.handler.capability;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.VENDOR;
16 import static org.openhab.core.thing.Thing.*;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.List;
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.NAHomeStatus.HomeStatus;
32 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
33 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
34 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
35 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
36 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.binding.ThingHandlerService;
39 import org.openhab.core.types.Command;
42 * The {@link Capability} is the base class for all inherited capabilities
44 * @author Gaƫl L'hopital - Initial contribution
48 public class Capability {
50 protected final Thing thing;
51 protected final CommonInterface handler;
52 protected final ModuleType moduleType;
54 protected boolean firstLaunch;
55 protected Map<String, String> properties = Map.of();
56 protected @Nullable String statusReason;
58 Capability(CommonInterface handler) {
59 this.handler = handler;
60 this.thing = handler.getThing();
61 this.moduleType = ModuleType.from(thing.getThingTypeUID());
64 public final @Nullable String setNewData(NAObject newData) {
66 if (newData instanceof HomeData) {
67 updateHomeData((HomeData) newData);
69 if (newData instanceof HomeStatus) {
70 updateHomeStatus((HomeStatus) newData);
72 if (newData instanceof HomeStatusModule) {
73 updateHomeStatusModule((HomeStatusModule) newData);
75 if (newData instanceof Event) {
76 updateEvent((Event) newData);
78 if (newData instanceof WebhookEvent) {
79 updateWebhookEvent((WebhookEvent) newData);
81 if (newData instanceof HomeEvent) {
82 updateHomeEvent((HomeEvent) newData);
84 if (newData instanceof NAThing) {
85 updateNAThing((NAThing) newData);
87 if (newData instanceof NAMain) {
88 updateNAMain((NAMain) newData);
90 if (newData instanceof Device) {
91 updateNADevice((Device) newData);
93 afterNewData(newData);
97 protected void beforeNewData() {
98 properties = new HashMap<>(thing.getProperties());
99 firstLaunch = properties.isEmpty();
100 if (firstLaunch && !moduleType.isLogical()) {
101 String name = moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName;
102 properties.put(PROPERTY_MODEL_ID, name);
103 properties.put(PROPERTY_VENDOR, VENDOR);
108 protected void afterNewData(@Nullable NAObject newData) {
109 if (!properties.equals(thing.getProperties())) {
110 thing.setProperties(properties);
114 protected void updateNAThing(NAThing newData) {
115 String firmware = newData.getFirmware();
116 if (firmware != null && !firmware.isBlank()) {
117 properties.put(PROPERTY_FIRMWARE_VERSION, firmware);
119 if (!newData.isReachable()) {
120 statusReason = "@text/device-not-connected";
124 protected void updateNAMain(NAMain newData) {
125 // do nothing by default, can be overridden by subclasses
128 protected void updateHomeEvent(HomeEvent newData) {
129 // do nothing by default, can be overridden by subclasses
132 protected void updateHomeStatus(HomeStatus newData) {
133 // do nothing by default, can be overridden by subclasses
136 protected void updateHomeData(HomeData newData) {
137 // do nothing by default, can be overridden by subclasses
140 protected void updateEvent(Event newData) {
141 // do nothing by default, can be overridden by subclasses
144 protected void updateWebhookEvent(WebhookEvent newData) {
145 // do nothing by default, can be overridden by subclasses
148 protected void updateNADevice(Device newData) {
149 // do nothing by default, can be overridden by subclasses
152 public void initialize() {
153 // do nothing by default, can be overridden by subclasses
156 public void expireData() {
157 if (!handler.getCapabilities().containsKey(RefreshCapability.class)) {
158 CommonInterface bridgeHandler = handler.getBridgeHandler();
159 if (bridgeHandler != null) {
160 bridgeHandler.expireData();
165 public void dispose() {
166 // do nothing by default, can be overridden by subclasses
169 public void updateHomeStatusModule(HomeStatusModule newData) {
170 // do nothing by default, can be overridden by subclasses
173 public void handleCommand(String channelName, Command command) {
174 // do nothing by default, can be overridden by subclasses
177 public Collection<Class<? extends ThingHandlerService>> getServices() {
181 public List<NAObject> updateReadings() {