Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mqtt.homeassistant] handle multiple availability topics #15977

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public abstract class AbstractMQTTThingHandler extends BaseThingHandler

private AtomicBoolean messageReceived = new AtomicBoolean(false);
private Map<String, @Nullable ChannelState> availabilityStates = new ConcurrentHashMap<>();
private AvailabilityMode availabilityMode = AvailabilityMode.ALL;

public AbstractMQTTThingHandler(Thing thing, int subscribeTimeout) {
super(thing);
Expand Down Expand Up @@ -261,15 +262,15 @@ public void dispose() {
@Override
public void updateChannelState(ChannelUID channelUID, State value) {
if (messageReceived.compareAndSet(false, true)) {
calculateThingStatus();
calculateThingStatus(true);
}
super.updateState(channelUID, value);
}

@Override
public void triggerChannel(ChannelUID channelUID, String event) {
if (messageReceived.compareAndSet(false, true)) {
calculateThingStatus();
calculateThingStatus(true);
}
super.triggerChannel(channelUID, event);
}
Expand All @@ -292,6 +293,11 @@ public void setConnection(MqttBrokerConnection connection) {
this.connection = connection;
}

@Override
public void setAvailabilityMode(AvailabilityMode mode) {
this.availabilityMode = mode;
}

@Override
public void addAvailabilityTopic(String availability_topic, String payload_available,
String payload_not_available) {
Expand All @@ -310,7 +316,8 @@ public void addAvailabilityTopic(String availability_topic, String payload_avail
channelUID, value, new ChannelStateUpdateListener() {
@Override
public void updateChannelState(ChannelUID channelUID, State value) {
calculateThingStatus();
boolean online = value.equals(OnOffType.ON);
calculateThingStatus(online);
}

@Override
Expand Down Expand Up @@ -352,18 +359,32 @@ public void clearAllAvailabilityTopics() {
@Override
public void resetMessageReceived() {
if (messageReceived.compareAndSet(true, false)) {
calculateThingStatus();
calculateThingStatus(false);
}
}

protected void calculateThingStatus() {
protected void calculateThingStatus(boolean lastValue) {
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
final Optional<Boolean> availabilityTopicsSeen;

if (availabilityStates.isEmpty()) {
availabilityTopicsSeen = Optional.empty();
} else {
availabilityTopicsSeen = Optional.of(availabilityStates.values().stream().allMatch(
c -> c != null && OnOffType.ON.equals(c.getCache().getChannelState().as(OnOffType.class))));
switch (availabilityMode) {
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
case ALL:
availabilityTopicsSeen = Optional.of(availabilityStates.values().stream().allMatch(
c -> c != null && OnOffType.ON.equals(c.getCache().getChannelState().as(OnOffType.class))));
break;
case ANY:
availabilityTopicsSeen = Optional.of(availabilityStates.values().stream().anyMatch(
c -> c != null && OnOffType.ON.equals(c.getCache().getChannelState().as(OnOffType.class))));
break;
case LATEST:
availabilityTopicsSeen = Optional.of(lastValue);
break;
default:
availabilityTopicsSeen = Optional.empty();
break;
}
}
updateThingStatus(messageReceived.get(), availabilityTopicsSeen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,39 @@
* Interface to keep track of the availability of device using an availability topic or messages received
*
* @author Jochen Klein - Initial contribution
* @author Cody Cutrer - Support all/any/latest
*/
@NonNullByDefault
public interface AvailabilityTracker {
/**
* controls the conditions needed to set the entity to available
*/
public enum AvailabilityMode {
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
/**
* payload_available must be received on all configured availability topics before the entity is marked as
* online
*/
ALL,

/**
* payload_available must be received on at least one configured availability topic before the entity is marked
* as
* online
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
*/
ANY,

/**
* the last payload_available or payload_not_available received on any configured availability topic controls
* the
* availability
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
*/
LATEST
}

/**
* Sets how multiple availability topics are treated
*/
void setAvailabilityMode(AvailabilityMode mode);

/**
* Adds an availability topic to determine the availability of a device.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public GenericMQTTThingHandler(Thing thing, MqttChannelStateDescriptionProvider
clearAllAvailabilityTopics();
initializeAvailabilityTopicsFromConfig();
return channelStateByChannelUID.values().stream().map(c -> c.start(connection, scheduler, 0))
.collect(FutureCollector.allOf()).thenRun(this::calculateThingStatus);
.collect(FutureCollector.allOf()).thenRun(() -> calculateThingStatus(false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.mqtt.generic.AvailabilityTracker;
import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
Expand All @@ -32,6 +33,8 @@
import org.openhab.binding.mqtt.homeassistant.internal.HaID;
import org.openhab.binding.mqtt.homeassistant.internal.component.ComponentFactory.ComponentConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Availability;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AvailabilityMode;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.Device;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.thing.ChannelGroupUID;
Expand Down Expand Up @@ -99,15 +102,42 @@ public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfig

this.configSeen = false;

String availabilityTopic = this.channelConfiguration.getAvailabilityTopic();
if (availabilityTopic != null) {
String availabilityTemplate = this.channelConfiguration.getAvailabilityTemplate();
if (availabilityTemplate != null) {
availabilityTemplate = JINJA_PREFIX + availabilityTemplate;
final List<Availability> availabilities = channelConfiguration.getAvailability();
if (availabilities != null) {
AvailabilityMode mode = channelConfiguration.getAvailabilityMode();
switch (mode) {
ccutrer marked this conversation as resolved.
Show resolved Hide resolved
case ALL:
componentConfiguration.getTracker().setAvailabilityMode(AvailabilityTracker.AvailabilityMode.ALL);
break;
case ANY:
componentConfiguration.getTracker().setAvailabilityMode(AvailabilityTracker.AvailabilityMode.ANY);
break;
case LATEST:
componentConfiguration.getTracker()
.setAvailabilityMode(AvailabilityTracker.AvailabilityMode.LATEST);
break;
}
for (Availability availability : availabilities) {
String availabilityTemplate = availability.getValueTemplate();
if (availabilityTemplate != null) {
availabilityTemplate = JINJA_PREFIX + availabilityTemplate;
}
componentConfiguration.getTracker().addAvailabilityTopic(availability.getTopic(),
availability.getPayloadAvailable(), availability.getPayloadNotAvailable(), availabilityTemplate,
componentConfiguration.getTransformationServiceProvider());
}
} else {
String availabilityTopic = this.channelConfiguration.getAvailabilityTopic();
if (availabilityTopic != null) {
String availabilityTemplate = this.channelConfiguration.getAvailabilityTemplate();
if (availabilityTemplate != null) {
availabilityTemplate = JINJA_PREFIX + availabilityTemplate;
}
componentConfiguration.getTracker().addAvailabilityTopic(availabilityTopic,
this.channelConfiguration.getPayloadAvailable(),
this.channelConfiguration.getPayloadNotAvailable(), availabilityTemplate,
componentConfiguration.getTransformationServiceProvider());
}
componentConfiguration.getTracker().addAvailabilityTopic(availabilityTopic,
this.channelConfiguration.getPayloadAvailable(), this.channelConfiguration.getPayloadNotAvailable(),
availabilityTemplate, componentConfiguration.getTransformationServiceProvider());
}
}

Expand Down