import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder;
+import org.openhab.core.thing.util.ThingHandlerHelper;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.UnDefType;
private final List<ResultChannelSet> resultChannels;
private final TimeZoneProvider tzProvider;
private @Nullable ScheduledFuture<?> updateFuture;
- private boolean initFinished;
public EventFilterHandler(Thing thing, TimeZoneProvider tzProvider) {
super(thing);
resultChannels = new CopyOnWriteArrayList<>();
- initFinished = false;
this.tzProvider = tzProvider;
}
@Override
public void initialize() {
- updateStatus(ThingStatus.UNKNOWN);
-
Bridge iCalendarBridge = getBridge();
if (iCalendarBridge == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
configuration = config;
updateChannelSet(config);
- initFinished = true;
if (iCalendarBridge.getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
return;
}
- updateStates();
+
+ updateStatus(ThingStatus.UNKNOWN);
}
@Override
* Updates all states and channels. Reschedules an update if no error occurs.
*/
private void updateStates() {
- if (!initFinished) {
+ if (!ThingHandlerHelper.isHandlerInitialized(this)) {
logger.debug("Ignoring call for updating states as this instance is not initialized yet.");
return;
}
// reload calendar file asynchronously
if (reloadCalendar()) {
updateStates();
+ updateChildren();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"The calendar seems to be configured correctly, but the local copy of calendar could not be loaded.");
}
}
+ @Override
+ public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
+ final AbstractPresentableCalendar calendar = runtimeCalendar;
+ if (calendar != null) {
+ updateChild(childHandler);
+ }
+ }
+
@Override
public void onCalendarUpdated() {
if (reloadCalendar()) {
updateStates();
- for (Thing childThing : getThing().getThings()) {
- ThingHandler handler = childThing.getHandler();
- if (handler instanceof CalendarUpdateListener) {
- try {
- logger.trace("Notifying {} about fresh calendar.", handler.getThing().getUID());
- ((CalendarUpdateListener) handler).onCalendarUpdated();
- } catch (Exception e) {
- logger.trace("The update of a child handler failed. Ignoring.", e);
- }
- }
- }
+ updateChildren();
} else {
logger.trace("Calendar was updated, but loading failed.");
}
updateStatesLastCalledTime = now;
}
}
+
+ /**
+ * Updates all children of this handler.
+ */
+ private void updateChildren() {
+ getThing().getThings().forEach(childThing -> updateChild(childThing.getHandler()));
+ }
+
+ /**
+ * Updates a specific child handler.
+ *
+ * @param childHandler the handler to be updated
+ */
+ private void updateChild(@Nullable ThingHandler childHandler) {
+ if (childHandler instanceof CalendarUpdateListener) {
+ logger.trace("Notifying {} about fresh calendar.", childHandler.getThing().getUID());
+ try {
+ ((CalendarUpdateListener) childHandler).onCalendarUpdated();
+ } catch (Exception e) {
+ logger.trace("The update of a child handler failed. Ignoring.", e);
+ }
+ }
+ }
}