Skip to content

Commit c5b7209

Browse files
committed
various
- remove unnecessary handler initialized tests - resubscribe event if channel linked - refactoring Signed-off-by: Andrew Fiddian-Green <[email protected]>
1 parent 6eafb2e commit c5b7209

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

bundles/org.openhab.binding.homekit/src/main/java/org/openhab/binding/homekit/internal/handler/HomekitAccessoryHandler.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.openhab.core.thing.type.ChannelType;
7171
import org.openhab.core.thing.type.ChannelTypeRegistry;
7272
import org.openhab.core.thing.type.ChannelTypeUID;
73-
import org.openhab.core.thing.util.ThingHandlerHelper;
7473
import org.openhab.core.types.Command;
7574
import org.openhab.core.types.RefreshType;
7675
import org.openhab.core.types.State;
@@ -906,11 +905,6 @@ protected IpTransport getIpTransport() throws IllegalAccessException {
906905
return super.getIpTransport();
907906
}
908907

909-
@Override
910-
protected boolean bridgedThingsInitialized() {
911-
return ThingHandlerHelper.isHandlerInitialized(thing); // no bridged accessories; return own status
912-
}
913-
914908
@Override
915909
protected void onConnectedThingAccessoriesLoaded() {
916910
createProperties();
@@ -928,7 +922,11 @@ public void onEvent(String json) {
928922
*/
929923
@Override
930924
public void channelLinked(ChannelUID channelUID) {
925+
boolean eventedCharacteristicsChanged = false;
931926
try {
927+
if (!alreadyAtStartLevelComplete()) {
928+
return; // item-channel-links not yet fully initialized
929+
}
932930
final Channel channel = thing.getChannel(channelUID);
933931
if (channel == null) {
934932
return; // OH core ensures this does not happen
@@ -969,6 +967,7 @@ public void channelLinked(ChannelUID channelUID) {
969967
entry.aid = aid;
970968
entry.iid = iid;
971969
eventedCharacteristics.put(AID_IID_FORMAT.formatted(entry.aid, entry.iid), entry);
970+
eventedCharacteristicsChanged = true;
972971
}
973972
if (checkChannelLinkByIID) {
974973
return; // unique match found; return directly
@@ -977,6 +976,9 @@ public void channelLinked(ChannelUID channelUID) {
977976
}
978977
}
979978
} finally {
979+
if (eventedCharacteristicsChanged) { // if evented list changes (re-) enable eventing (using new list)
980+
scheduler.submit(() -> enableEvents(true));
981+
}
980982
super.channelLinked(channelUID);
981983
}
982984
}

bundles/org.openhab.binding.homekit/src/main/java/org/openhab/binding/homekit/internal/handler/HomekitBaseAccessoryHandler.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ private void fetchAccessories() {
243243
* 'onThingOnline' methods (and its eventual overloaded implementations).
244244
*/
245245
private void processBridgedThings() {
246-
if (!bridgedThingsInitialized()) {
247-
logger.warn("{} unexpected error: bridged Things not initialized.", thing.getUID());
248-
}
249246
onConnectedThingAccessoriesLoaded();
250247
onThingOnline();
251248
}
@@ -291,10 +288,9 @@ public void initialize() {
291288
if (alreadyAtStartLevelComplete()) {
292289
// schedule connection attempt immediately
293290
scheduleConnectionAttempt();
294-
} else {
291+
} else if (bundle.getBundleContext() instanceof BundleContext ctx) {
295292
// delay connection attempt until STARTLEVEL_COMPLETE is signalled via receive() method below
296-
BundleContext context = bundle.getBundleContext();
297-
eventSubscription = context.registerService(EventSubscriber.class.getName(), this, null);
293+
eventSubscription = ctx.registerService(EventSubscriber.class.getName(), this, null);
298294
}
299295
}
300296
updateStatus(ThingStatus.UNKNOWN);
@@ -306,14 +302,16 @@ public void initialize() {
306302
* Note: STARTLEVEL_COMPLETE means all Thing handlers are instantiated and their initialize() methods have
307303
* been called, and the registries for item, thing, and item-channel-links have all been loaded.
308304
*/
309-
private boolean alreadyAtStartLevelComplete() {
310-
BundleContext context = bundle.getBundleContext();
311-
ServiceReference<StartLevelService> reference = context.getServiceReference(StartLevelService.class);
312-
if (reference != null && context.getService(reference) instanceof StartLevelService service) {
313-
try {
314-
return service.getStartLevel() >= StartLevelService.STARTLEVEL_COMPLETE;
315-
} finally {
316-
context.ungetService(reference);
305+
protected boolean alreadyAtStartLevelComplete() {
306+
if (bundle.getBundleContext() instanceof BundleContext ctx) {
307+
if (ctx.getServiceReference(StartLevelService.class) instanceof ServiceReference<StartLevelService> ref) {
308+
if (ctx.getService(ref) instanceof StartLevelService svc) {
309+
try {
310+
return svc.getStartLevel() >= StartLevelService.STARTLEVEL_COMPLETE;
311+
} finally {
312+
ctx.ungetService(ref);
313+
}
314+
}
317315
}
318316
}
319317
return false;
@@ -416,12 +414,11 @@ private String normalizePairingCode(String input) throws IllegalArgumentExceptio
416414
protected void scheduleConnectionAttempt() {
417415
if (getBridge() instanceof Bridge bridge && bridge.getHandler() instanceof HomekitBridgeHandler bridgeHandler) {
418416
bridgeHandler.scheduleConnectionAttempt();
419-
} else {
420-
ScheduledFuture<?> task = connectionAttemptTask;
421-
if (task == null || task.isDone() || task.isCancelled()) {
422-
connectionAttemptTask = scheduler.schedule(this::attemptConnect, connectionAttemptDelay,
423-
TimeUnit.SECONDS);
424-
}
417+
return;
418+
}
419+
ScheduledFuture<?> task = connectionAttemptTask;
420+
if (task == null || task.isDone() || task.isCancelled()) {
421+
connectionAttemptTask = scheduler.schedule(this::attemptConnect, connectionAttemptDelay, TimeUnit.SECONDS);
425422
}
426423
}
427424

@@ -687,7 +684,11 @@ protected void createProperties() {
687684
*
688685
* @param enable true to enable events, false to disable
689686
*/
690-
private void enableEvents(boolean enable) {
687+
protected void enableEvents(boolean enable) {
688+
if (getBridge() instanceof Bridge bridge && bridge.getHandler() instanceof HomekitBridgeHandler bridgeHandler) {
689+
bridgeHandler.enableEvents(true);
690+
return;
691+
}
691692
try {
692693
enableEventsOrThrow(enable);
693694
} catch (InterruptedException e) {
@@ -772,12 +773,6 @@ private synchronized void refresh() {
772773
}
773774
}
774775

775-
/**
776-
* Checks if all bridged accessory things have the reached status UNKNOWN, OFFLINE, or ONLINE.
777-
* Subclasses MUST override this to perform the check.
778-
*/
779-
protected abstract boolean bridgedThingsInitialized();
780-
781776
/**
782777
* Called when the connected thing has finished loading the accessories.
783778
* Subclasses MUST override this to perform any extra processing required.
@@ -863,11 +858,11 @@ private void cancelRefreshTasks() {
863858
protected void requestManualRefresh() {
864859
if (getBridge() instanceof Bridge bridge && bridge.getHandler() instanceof HomekitBridgeHandler bridgeHandler) {
865860
bridgeHandler.requestManualRefresh();
866-
} else {
867-
Future<?> task = manualRefreshTask;
868-
if (task == null || task.isDone() || task.isCancelled()) {
869-
manualRefreshTask = scheduler.schedule(this::refresh, MANUAL_REFRESH_DELAY_SECONDS, TimeUnit.SECONDS);
870-
}
861+
return;
862+
}
863+
Future<?> task = manualRefreshTask;
864+
if (task == null || task.isDone() || task.isCancelled()) {
865+
manualRefreshTask = scheduler.schedule(this::refresh, MANUAL_REFRESH_DELAY_SECONDS, TimeUnit.SECONDS);
871866
}
872867
}
873868

bundles/org.openhab.binding.homekit/src/main/java/org/openhab/binding/homekit/internal/handler/HomekitBridgeHandler.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.openhab.core.thing.binding.ThingHandler;
3333
import org.openhab.core.thing.binding.ThingHandlerService;
3434
import org.openhab.core.thing.binding.builder.BridgeBuilder;
35-
import org.openhab.core.thing.util.ThingHandlerHelper;
3635
import org.openhab.core.types.Command;
3736
import org.osgi.framework.Bundle;
3837

@@ -92,12 +91,6 @@ public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
9291
// do nothing
9392
}
9493

95-
@Override
96-
protected boolean bridgedThingsInitialized() {
97-
return getThing().getThings().stream()
98-
.allMatch(bridgedAccessory -> ThingHandlerHelper.isHandlerInitialized(bridgedAccessory));
99-
}
100-
10194
@Override
10295
protected void onConnectedThingAccessoriesLoaded() {
10396
createProperties();

0 commit comments

Comments
 (0)