|
39 | 39 | import org.openhab.binding.mqtt.homeassistant.internal.DiscoverComponents.ComponentDiscovered;
|
40 | 40 | import org.openhab.binding.mqtt.homeassistant.internal.HaID;
|
41 | 41 | import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
|
| 42 | +import org.openhab.binding.mqtt.homeassistant.internal.HomeAssistantChannelLinkageChecker; |
42 | 43 | import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
|
43 | 44 | import org.openhab.binding.mqtt.homeassistant.internal.component.ComponentFactory;
|
44 | 45 | import org.openhab.binding.mqtt.homeassistant.internal.component.DeviceTrigger;
|
|
83 | 84 | */
|
84 | 85 | @NonNullByDefault
|
85 | 86 | public class HomeAssistantThingHandler extends AbstractMQTTThingHandler
|
86 |
| - implements ComponentDiscovered, Consumer<List<Object>> { |
| 87 | + implements ComponentDiscovered, Consumer<List<Object>>, HomeAssistantChannelLinkageChecker { |
87 | 88 | public static final String AVAILABILITY_CHANNEL = "availability";
|
88 | 89 | private static final Comparator<AbstractComponent<?>> COMPONENT_COMPARATOR = Comparator
|
89 | 90 | .comparing((AbstractComponent<?> component) -> component.hasGroup())
|
@@ -134,7 +135,7 @@ public HomeAssistantThingHandler(Thing thing, MqttChannelTypeProvider channelTyp
|
134 | 135 | this.unitProvider = unitProvider;
|
135 | 136 | this.attributeReceiveTimeout = attributeReceiveTimeout;
|
136 | 137 | this.delayedProcessing = new DelayedBatchProcessing<>(attributeReceiveTimeout, this, scheduler);
|
137 |
| - this.discoverComponents = new DiscoverComponents(thing.getUID(), scheduler, this, this, gson, jinjava, |
| 138 | + this.discoverComponents = new DiscoverComponents(thing.getUID(), scheduler, this, this, this, gson, jinjava, |
138 | 139 | unitProvider);
|
139 | 140 | }
|
140 | 141 |
|
@@ -183,7 +184,7 @@ public void initialize() {
|
183 | 184 | String channelConfigurationJSON = (String) channelConfig.get("config");
|
184 | 185 | try {
|
185 | 186 | AbstractComponent<?> component = ComponentFactory.createComponent(thingUID, haID,
|
186 |
| - channelConfigurationJSON, this, this, scheduler, gson, jinjava, unitProvider); |
| 187 | + channelConfigurationJSON, this, this, this, scheduler, gson, jinjava, unitProvider); |
187 | 188 | if (typeID.equals(MqttBindingConstants.HOMEASSISTANT_MQTT_THING)) {
|
188 | 189 | typeID = calculateThingTypeUID(component);
|
189 | 190 | }
|
@@ -558,4 +559,36 @@ private List<Configuration> flattenChannelConfiguration(Configuration multiCompo
|
558 | 559 | Map<@Nullable String, AbstractComponent<?>> getComponents() {
|
559 | 560 | return haComponents;
|
560 | 561 | }
|
| 562 | + |
| 563 | + // For components to check if a channel is linked before starting them |
| 564 | + @Override |
| 565 | + public boolean isChannelLinked(ChannelUID channelUID) { |
| 566 | + return isLinked(channelUID); |
| 567 | + } |
| 568 | + |
| 569 | + // A channel is newly linked; make sure it is started |
| 570 | + @Override |
| 571 | + public void channelLinked(ChannelUID channelUID) { |
| 572 | + MqttBrokerConnection connection = this.connection; |
| 573 | + // We haven't started at all yet. |
| 574 | + if (connection == null) { |
| 575 | + return; |
| 576 | + } |
| 577 | + synchronized (haComponents) { |
| 578 | + haComponents.forEach((id, component) -> { |
| 579 | + if (component.getChannels().stream().anyMatch(channel -> channel.getUID().equals(channelUID))) { |
| 580 | + component.start(connection, scheduler, attributeReceiveTimeout).exceptionally(e -> { |
| 581 | + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage()); |
| 582 | + return null; |
| 583 | + }); |
| 584 | + } |
| 585 | + }); |
| 586 | + } |
| 587 | + super.channelLinked(channelUID); |
| 588 | + } |
| 589 | + |
| 590 | + // Don't bother unsubscribing on unlink; it's a relatively rare operation during normal usage, |
| 591 | + // and a decent amount of effort to make sure there aren't other links before stopping them, and |
| 592 | + // making sure not to stop other channels that are still linked. |
| 593 | + // A disable/re-enable of the thing will clear the subscriptions. |
561 | 594 | }
|
0 commit comments