]> git.basschouten.com Git - openhab-addons.git/blob
37fbd05402f3e2e599fc4b86b7f1d759ab37d6b4
[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.sensibo.internal.handler;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.sensibo.internal.model.SensiboModel;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * @author Arne Seime - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class SensiboBaseThingHandler extends BaseThingHandler {
34     private final Logger logger = LoggerFactory.getLogger(SensiboBaseThingHandler.class);
35
36     public SensiboBaseThingHandler(final Thing thing) {
37         super(thing);
38     }
39
40     public void updateState(final SensiboModel model) {
41         for (final Channel channel : getThing().getChannels()) {
42             handleCommand(channel.getUID(), RefreshType.REFRESH, model);
43         }
44     }
45
46     public SensiboModel getSensiboModel() {
47         final Optional<SensiboAccountHandler> accountHandler = getAccountHandler();
48         if (accountHandler.isPresent()) {
49             return accountHandler.get().getModel();
50         } else {
51             logger.debug(
52                     "Thing {} cannot exist without a bridge and account handler - returning empty model. No heaters or rooms will be found",
53                     getThing().getUID());
54             return new SensiboModel(0);
55         }
56     }
57
58     protected Optional<SensiboAccountHandler> getAccountHandler() {
59         final Bridge bridge = getBridge();
60         if (bridge != null) {
61             final SensiboAccountHandler accountHandler = (SensiboAccountHandler) bridge.getHandler();
62             if (accountHandler != null) {
63                 return Optional.of(accountHandler);
64             }
65         }
66         return Optional.empty();
67     }
68
69     protected abstract void handleCommand(ChannelUID uid, Command command, SensiboModel model);
70 }