Search in sources :

Example 1 with CChannel

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel in project smarthome by eclipse.

the class HomeAssistantThingHandler method accept.

/**
 * Callback of {@link DelayedBatchProcessing}.
 * Add all newly discovered components to the Thing and start the components.
 */
@SuppressWarnings("null")
@Override
public void accept(List<AbstractComponent> discoveredComponentsList) {
    MqttBrokerConnection connection = this.connection;
    if (connection == null) {
        return;
    }
    List<Channel> channels = new ArrayList<>();
    synchronized (haComponents) {
        // sync whenever discoverComponents is started
        for (AbstractComponent discovered : discoveredComponentsList) {
            AbstractComponent known = haComponents.get(discovered.uid().getId());
            // Is component already known?
            if (known != null) {
                if (discovered.getConfigHash() != known.getConfigHash()) {
                    // Don't wait for the future to complete. We are also not interested in failures.
                    // The component will be replaced in a moment.
                    known.stop();
                } else {
                    continue;
                }
            }
            // Add channel and group types to the types registry
            channelTypeProvider.setChannelGroupType(discovered.groupTypeUID(), discovered.type());
            discovered.addChannelTypes(channelTypeProvider);
            // Add component to the component map
            haComponents.put(discovered.uid().getId(), discovered);
            // Start component / Subscribe to channel topics
            discovered.start(connection, scheduler, 0).exceptionally(e -> {
                logger.warn("Failed to start component {}", discovered.uid(), e);
                return null;
            });
        }
        // Add channels to Thing
        for (AbstractComponent e : haComponents.values()) {
            for (CChannel entry : e.channelTypes().values()) {
                channels.add(entry.channel);
            }
        }
    }
    updateThing(editThing().withChannels(channels).build());
    updateStatus(ThingStatus.ONLINE);
}
Also used : AbstractComponent(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.AbstractComponent) CChannel(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Channel(org.eclipse.smarthome.core.thing.Channel) CChannel(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel) ArrayList(java.util.ArrayList)

Example 2 with CChannel

use of org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel in project smarthome by eclipse.

the class HomeAssistantThingHandler method getChannelState.

@SuppressWarnings({ "null", "unused" })
@Override
@Nullable
public ChannelState getChannelState(ChannelUID channelUID) {
    String groupID = channelUID.getGroupId();
    if (groupID == null) {
        return null;
    }
    AbstractComponent component;
    synchronized (haComponents) {
        // sync whenever discoverComponents is started
        component = haComponents.get(groupID);
    }
    if (component == null) {
        return null;
    }
    CChannel componentChannel = component.channel(channelUID.getIdWithoutGroup());
    if (componentChannel == null) {
        return null;
    }
    return componentChannel.channelState;
}
Also used : AbstractComponent(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.AbstractComponent) CChannel(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

AbstractComponent (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.AbstractComponent)2 CChannel (org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.CChannel)2 ArrayList (java.util.ArrayList)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Channel (org.eclipse.smarthome.core.thing.Channel)1 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)1