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.millheat.internal.handler;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.millheat.internal.model.MillheatModel;
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;
30 * Base class for heater and room handlers
32 * @author Arne Seime - Initial contribution
35 public abstract class MillheatBaseThingHandler extends BaseThingHandler {
36 private final Logger logger = LoggerFactory.getLogger(MillheatBaseThingHandler.class);
38 public MillheatBaseThingHandler(final Thing thing) {
42 public void updateState(final MillheatModel model) {
43 for (final Channel channel : getThing().getChannels()) {
44 handleCommand(channel.getUID(), RefreshType.REFRESH, model);
48 protected MillheatModel getMillheatModel() {
49 final Optional<MillheatAccountHandler> accountHandler = getAccountHandler();
50 if (accountHandler.isPresent()) {
51 return accountHandler.get().getModel();
54 "Thing {} cannot exist without a bridge and account handler - returning empty model. No heaters or rooms will be found",
56 return new MillheatModel(0);
60 protected Optional<MillheatAccountHandler> getAccountHandler() {
61 final Bridge bridge = getBridge();
63 MillheatAccountHandler handler = (MillheatAccountHandler) bridge.getHandler();
64 if (handler != null) {
65 return Optional.of(handler);
68 return Optional.empty();
71 protected abstract void handleCommand(ChannelUID uid, Command command, MillheatModel model);