Skip to content

Commit

Permalink
[mqtt-homeassistant] Refactoring: fixed under_score/CamelCase usages …
Browse files Browse the repository at this point in the history
…and nullable annotations (openhab#11120)

Signed-off-by: Anton Kharuzhy <[email protected]>
  • Loading branch information
antroids authored and frederictobiasc committed Oct 26, 2021
1 parent fe4d1c8 commit 4869554
Show file tree
Hide file tree
Showing 34 changed files with 692 additions and 576 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public static class Builder {
private final String label;
private final ChannelStateUpdateListener channelStateUpdateListener;

private @Nullable String state_topic;
private @Nullable String command_topic;
private @Nullable String stateTopic;
private @Nullable String commandTopic;
private boolean retain;
private boolean trigger;
private @Nullable Integer qos;
Expand All @@ -144,14 +144,14 @@ public Builder(AbstractComponent<?> component, String channelID, Value valueStat
this.channelStateUpdateListener = channelStateUpdateListener;
}

public Builder stateTopic(@Nullable String state_topic) {
this.state_topic = state_topic;
public Builder stateTopic(@Nullable String stateTopic) {
this.stateTopic = stateTopic;
return this;
}

public Builder stateTopic(@Nullable String state_topic, @Nullable String... templates) {
this.state_topic = state_topic;
if (state_topic != null && !state_topic.isBlank()) {
public Builder stateTopic(@Nullable String stateTopic, @Nullable String... templates) {
this.stateTopic = stateTopic;
if (stateTopic != null && !stateTopic.isBlank()) {
for (String template : templates) {
if (template != null && !template.isBlank()) {
this.templateIn = template;
Expand All @@ -164,27 +164,26 @@ public Builder stateTopic(@Nullable String state_topic, @Nullable String... temp

/**
* @deprecated use commandTopic(String, boolean, int)
* @param command_topic topic
* @param commandTopic topic
* @param retain retain
* @return this
*/
@Deprecated
public Builder commandTopic(@Nullable String command_topic, boolean retain) {
this.command_topic = command_topic;
public Builder commandTopic(@Nullable String commandTopic, boolean retain) {
this.commandTopic = commandTopic;
this.retain = retain;
return this;
}

public Builder commandTopic(@Nullable String command_topic, boolean retain, int qos) {
return commandTopic(command_topic, retain, qos, null);
public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos) {
return commandTopic(commandTopic, retain, qos, null);
}

public Builder commandTopic(@Nullable String command_topic, boolean retain, int qos,
@Nullable String template) {
this.command_topic = command_topic;
public Builder commandTopic(@Nullable String commandTopic, boolean retain, int qos, @Nullable String template) {
this.commandTopic = commandTopic;
this.retain = retain;
this.qos = qos;
if (command_topic != null && !command_topic.isBlank()) {
if (commandTopic != null && !commandTopic.isBlank()) {
this.templateOut = template;
}
return this;
Expand Down Expand Up @@ -215,16 +214,16 @@ public ComponentChannel build(boolean addToComponent) {
channelTypeUID = new ChannelTypeUID(MqttBindingConstants.BINDING_ID,
channelUID.getGroupId() + "_" + channelID);
channelState = new HomeAssistantChannelState(
ChannelConfigBuilder.create().withRetain(retain).withQos(qos).withStateTopic(state_topic)
.withCommandTopic(command_topic).makeTrigger(trigger).build(),
ChannelConfigBuilder.create().withRetain(retain).withQos(qos).withStateTopic(stateTopic)
.withCommandTopic(commandTopic).makeTrigger(trigger).build(),
channelUID, valueState, channelStateUpdateListener, commandFilter);

String localStateTopic = state_topic;
String localStateTopic = stateTopic;
if (localStateTopic == null || localStateTopic.isBlank() || this.trigger) {
type = ChannelTypeBuilder.trigger(channelTypeUID, label)
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL)).build();
} else {
StateDescriptionFragment description = valueState.createStateDescription(command_topic == null).build();
StateDescriptionFragment description = valueState.createStateDescription(commandTopic == null).build();
type = ChannelTypeBuilder.state(channelTypeUID, label, channelState.getItemType())
.withConfigDescriptionURI(URI.create(MqttBindingConstants.CONFIG_HA_CHANNEL))
.withStateDescriptionFragment(description).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,7 @@ public boolean equals(@Nullable Object obj) {
if (!nodeID.equals(other.nodeID)) {
return false;
}
if (!objectID.equals(other.objectID)) {
return false;
}
return true;
return objectID.equals(other.objectID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public AbstractComponent(ComponentFactory.ComponentConfiguration componentConfig

this.configSeen = false;

String availability_topic = this.channelConfiguration.getAvailabilityTopic();
if (availability_topic != null) {
componentConfiguration.getTracker().addAvailabilityTopic(availability_topic,
String availabilityTopic = this.channelConfiguration.getAvailabilityTopic();
if (availabilityTopic != null) {
componentConfiguration.getTracker().addAvailabilityTopic(availabilityTopic,
this.channelConfiguration.getPayloadAvailable(),
this.channelConfiguration.getPayloadNotAvailable());
}
Expand Down Expand Up @@ -180,7 +180,7 @@ public Map<String, ComponentChannel> getChannelMap() {
/**
* Return a components channel. A HomeAssistant MQTT component consists of multiple functions
* and those are mapped to one or more channels. The channel IDs are constants within the
* derived Component, like the {@link Switch#switchChannelID}.
* derived Component, like the {@link Switch#SWITCH_CHANNEL_ID}.
*
* @param channelID The channel ID
* @return A components channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.openhab.binding.mqtt.generic.values.TextValue;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;

import com.google.gson.annotations.SerializedName;

/**
* A MQTT alarm control panel, following the https://www.home-assistant.io/components/alarm_control_panel.mqtt/
* specification.
Expand All @@ -28,10 +30,10 @@
*/
@NonNullByDefault
public class AlarmControlPanel extends AbstractComponent<AlarmControlPanel.ChannelConfiguration> {
public static final String stateChannelID = "alarm"; // Randomly chosen channel "ID"
public static final String switchDisarmChannelID = "disarm"; // Randomly chosen channel "ID"
public static final String switchArmHomeChannelID = "armhome"; // Randomly chosen channel "ID"
public static final String switchArmAwayChannelID = "armaway"; // Randomly chosen channel "ID"
public static final String STATE_CHANNEL_ID = "alarm"; // Randomly chosen channel "ID"
public static final String SWITCH_DISARM_CHANNEL_ID = "disarm"; // Randomly chosen channel "ID"
public static final String SWITCH_ARM_HOME_CHANNEL_ID = "armhome"; // Randomly chosen channel "ID"
public static final String SWITCH_ARM_AWAY_CHANNEL_ID = "armaway"; // Randomly chosen channel "ID"

/**
* Configuration class for MQTT component
Expand All @@ -43,45 +45,57 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {

protected @Nullable String code;

protected String state_topic = "";
protected String state_disarmed = "disarmed";
protected String state_armed_home = "armed_home";
protected String state_armed_away = "armed_away";
protected String state_pending = "pending";
protected String state_triggered = "triggered";
@SerializedName("state_topic")
protected String stateTopic = "";
@SerializedName("state_disarmed")
protected String stateDisarmed = "disarmed";
@SerializedName("state_armed_home")
protected String stateArmedHome = "armed_home";
@SerializedName("state_armed_away")
protected String stateArmedAway = "armed_away";
@SerializedName("state_pending")
protected String statePending = "pending";
@SerializedName("state_triggered")
protected String stateTriggered = "triggered";

protected @Nullable String command_topic;
protected String payload_disarm = "DISARM";
protected String payload_arm_home = "ARM_HOME";
protected String payload_arm_away = "ARM_AWAY";
@SerializedName("command_topic")
protected @Nullable String commandTopic;
@SerializedName("payload_disarm")
protected String payloadDisarm = "DISARM";
@SerializedName("payload_arm_home")
protected String payloadArmHome = "ARM_HOME";
@SerializedName("payload_arm_away")
protected String payloadArmAway = "ARM_AWAY";
}

public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

final String[] state_enum = { channelConfiguration.state_disarmed, channelConfiguration.state_armed_home,
channelConfiguration.state_armed_away, channelConfiguration.state_pending,
channelConfiguration.state_triggered };
buildChannel(stateChannelID, new TextValue(state_enum), channelConfiguration.getName(),
final String[] stateEnum = { channelConfiguration.stateDisarmed, channelConfiguration.stateArmedHome,
channelConfiguration.stateArmedAway, channelConfiguration.statePending,
channelConfiguration.stateTriggered };
buildChannel(STATE_CHANNEL_ID, new TextValue(stateEnum), channelConfiguration.getName(),
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.state_topic, channelConfiguration.getValueTemplate())//
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
.build();

String command_topic = channelConfiguration.command_topic;
if (command_topic != null) {
buildChannel(switchDisarmChannelID, new TextValue(new String[] { channelConfiguration.payload_disarm }),
String commandTopic = channelConfiguration.commandTopic;
if (commandTopic != null) {
buildChannel(SWITCH_DISARM_CHANNEL_ID, new TextValue(new String[] { channelConfiguration.payloadDisarm }),
channelConfiguration.getName(), componentConfiguration.getUpdateListener())
.commandTopic(command_topic, channelConfiguration.isRetain(), channelConfiguration.getQos())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos())
.build();

buildChannel(switchArmHomeChannelID, new TextValue(new String[] { channelConfiguration.payload_arm_home }),
channelConfiguration.getName(), componentConfiguration.getUpdateListener())
.commandTopic(command_topic, channelConfiguration.isRetain(), channelConfiguration.getQos())
buildChannel(SWITCH_ARM_HOME_CHANNEL_ID,
new TextValue(new String[] { channelConfiguration.payloadArmHome }), channelConfiguration.getName(),
componentConfiguration.getUpdateListener())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos())
.build();

buildChannel(switchArmAwayChannelID, new TextValue(new String[] { channelConfiguration.payload_arm_away }),
channelConfiguration.getName(), componentConfiguration.getUpdateListener())
.commandTopic(command_topic, channelConfiguration.isRetain(), channelConfiguration.getQos())
buildChannel(SWITCH_ARM_AWAY_CHANNEL_ID,
new TextValue(new String[] { channelConfiguration.payloadArmAway }), channelConfiguration.getName(),
componentConfiguration.getUpdateListener())
.commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
import org.openhab.binding.mqtt.homeassistant.internal.listener.OffDelayUpdateStateListener;

import com.google.gson.annotations.SerializedName;

/**
* A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
*
* @author David Graeff - Initial contribution
*/
@NonNullByDefault
public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"

/**
* Configuration class for MQTT component
Expand All @@ -40,39 +42,49 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
super("MQTT Binary Sensor");
}

protected @Nullable String device_class;
protected boolean force_update = false;
protected @Nullable Integer expire_after;
protected @Nullable Integer off_delay;
@SerializedName("device_class")
protected @Nullable String deviceClass;
@SerializedName("force_update")
protected boolean forceUpdate = false;
@SerializedName("expire_after")
protected @Nullable Integer expireAfter;
@SerializedName("off_delay")
protected @Nullable Integer offDelay;

protected String state_topic = "";
protected String payload_on = "ON";
protected String payload_off = "OFF";
@SerializedName("state_topic")
protected String stateTopic = "";
@SerializedName("payload_on")
protected String payloadOn = "ON";
@SerializedName("payload_off")
protected String payloadOff = "OFF";

protected @Nullable String json_attributes_topic;
protected @Nullable String json_attributes_template;
protected @Nullable List<String> json_attributes;
@SerializedName("json_attributes_topic")
protected @Nullable String jsonAttributesTopic;
@SerializedName("json_attributes_template")
protected @Nullable String jsonAttributesTemplate;
@SerializedName("json_attributes")
protected @Nullable List<String> jsonAttributes;
}

public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
super(componentConfiguration, ChannelConfiguration.class);

OnOffValue value = new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off);
OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);

buildChannel(sensorChannelID, value, "value", getListener(componentConfiguration, value))
.stateTopic(channelConfiguration.state_topic, channelConfiguration.getValueTemplate()).build();
buildChannel(SENSOR_CHANNEL_ID, value, "value", getListener(componentConfiguration, value))
.stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()).build();
}

private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
Value value) {
ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();

if (channelConfiguration.expire_after != null) {
updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
if (channelConfiguration.expireAfter != null) {
updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
componentConfiguration.getTracker(), componentConfiguration.getScheduler());
}
if (channelConfiguration.off_delay != null) {
updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.off_delay, value,
if (channelConfiguration.offDelay != null) {
updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
componentConfiguration.getScheduler());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
@NonNullByDefault
public class Camera extends AbstractComponent<Camera.ChannelConfiguration> {
public static final String cameraChannelID = "camera"; // Randomly chosen channel "ID"
public static final String CAMERA_CHANNEL_ID = "camera"; // Randomly chosen channel "ID"

/**
* Configuration class for MQTT component
Expand All @@ -43,7 +43,7 @@ public Camera(ComponentFactory.ComponentConfiguration componentConfiguration) {

ImageValue value = new ImageValue();

buildChannel(cameraChannelID, value, channelConfiguration.getName(), componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.topic).build();
buildChannel(CAMERA_CHANNEL_ID, value, channelConfiguration.getName(),
componentConfiguration.getUpdateListener()).stateTopic(channelConfiguration.topic).build();
}
}
Loading

0 comments on commit 4869554

Please sign in to comment.