]> git.basschouten.com Git - openhab-addons.git/blob
84e5ae2c0a5d2b5ed8301089b07ca55b8c3dd85f
[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.netatmo.internal.handler.capability;
14
15 import java.util.List;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
20 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
21 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
22 import org.openhab.binding.netatmo.internal.handler.channelhelper.EventChannelHelper;
23 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
24 import org.openhab.binding.netatmo.internal.servlet.WebhookServlet;
25
26 /**
27  * {@link HomeSecurityThingCapability} is the ancestor of capabilities hosted by a security home
28  * e.g. person and camera capabilities
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class HomeSecurityThingCapability extends Capability {
35     protected final NetatmoDescriptionProvider descriptionProvider;
36     protected final EventChannelHelper eventHelper;
37
38     private Optional<WebhookServlet> webhook = Optional.empty();
39     protected Optional<SecurityCapability> securityCapability = Optional.empty();
40     protected Optional<HomeCapability> homeCapability = Optional.empty();
41
42     public HomeSecurityThingCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
43             List<ChannelHelper> channelHelpers) {
44         super(handler);
45         this.descriptionProvider = descriptionProvider;
46         this.eventHelper = (EventChannelHelper) channelHelpers.stream().filter(c -> c instanceof EventChannelHelper)
47                 .findFirst().orElseThrow(() -> new IllegalArgumentException(
48                         "HomeSecurityThingCapability must find an EventChannelHelper, please file a bug report."));
49         eventHelper.setModuleType(moduleType);
50     }
51
52     @Override
53     public void initialize() {
54         super.initialize();
55         securityCapability = handler.getHomeCapability(SecurityCapability.class);
56         homeCapability = handler.getHomeCapability(HomeCapability.class);
57         ApiBridgeHandler accountHandler = handler.getAccountHandler();
58         if (accountHandler != null) {
59             webhook = accountHandler.getWebHookServlet();
60             webhook.ifPresent(servlet -> servlet.registerDataListener(handler.getId(), this));
61         }
62     }
63
64     @Override
65     public void dispose() {
66         webhook.ifPresent(servlet -> servlet.unregisterDataListener(handler.getId()));
67         super.dispose();
68     }
69 }