Skip to content

Commit

Permalink
[mqtt.homeassistant] Fix MQTT Vacuum serialization names
Browse files Browse the repository at this point in the history
While integrating the review comments, I did not remember that some
variable names are written as they are because they result from some
deserialization.

This small PR fixes this.

Signed-off-by: Stefan Triller <[email protected]>
  • Loading branch information
t2000 committed Nov 9, 2021
1 parent 7bd71fd commit ed62a4a
Showing 1 changed file with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,24 @@ static class ChannelConfiguration extends AbstractChannelConfiguration {
super("MQTT Vacuum");
}

protected @Nullable String commandTopic;
protected String stateTopic = "";
protected @Nullable String sendCommandTopic; // for custom_command
// variable names are with "_" by intention to avoid code cluttering with serialization annotations
protected @Nullable String command_topic;
protected String state_topic = "";
protected @Nullable String send_command_topic; // for custom_command

// [start, pause, stop, return_home, battery, status, locate, clean_spot, fan_speed, send_command]
protected String[] supportedFeatures = new String[] {};
protected @Nullable String setFanSpeedTopic;
protected String[] fanSpeedList = new String[] {};
protected String[] supported_features = new String[] {};
protected @Nullable String set_fan_speed_topic;
protected String[] fan_speed_list = new String[] {};

protected @Nullable String jsonAttributesTopic;
protected @Nullable String jsonAttributesTemplate;
protected @Nullable String json_attributes_topic;
protected @Nullable String json_attributes_template;
}

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

List<String> features = Arrays.asList(channelConfiguration.supportedFeatures);
List<String> features = Arrays.asList(channelConfiguration.supported_features);

// features = [start, pause, stop, return_home, status, locate, clean_spot, fan_speed, send_command]
ArrayList<String> possibleCommands = new ArrayList<String>();
Expand All @@ -111,28 +112,28 @@ public Vacuum(ComponentFactory.ComponentConfiguration componentConfiguration) {

TextValue value = new TextValue(possibleCommands.toArray(new String[0]));
buildChannel(VACUUM_COMMAND_CHANNEL_ID, value, "Command", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.commandTopic).commandTopic(channelConfiguration.commandTopic, false, 1)
.build();
.stateTopic(channelConfiguration.command_topic)
.commandTopic(channelConfiguration.command_topic, false, 1).build();

List<String> vacuumStates = List.of("docked", "cleaning", "returning", "paused", "idle", "error");
TextValue valueState = new TextValue(vacuumStates.toArray(new String[0]));
buildChannel(VACUUM_STATE_CHANNEL_ID, valueState, "State", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, "{{value_json.state}}").build();
.stateTopic(channelConfiguration.state_topic, "{{value_json.state}}").build();

if (features.contains("battery")) {
// build battery level channel (0-100)
NumberValue batValue = new NumberValue(BigDecimal.ZERO, new BigDecimal(100), new BigDecimal(1), "%");
buildChannel(VACUUM_BATTERY_CHANNEL_ID, batValue, "Battery Level",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, "{{value_json.battery_level}}").build();
.stateTopic(channelConfiguration.state_topic, "{{value_json.battery_level}}").build();
}

if (features.contains("fan_speed")) {
// build fan speed channel with values from channelConfiguration.fan_speed_list
TextValue fanValue = new TextValue(channelConfiguration.fanSpeedList);
TextValue fanValue = new TextValue(channelConfiguration.fan_speed_list);
buildChannel(VACUUM_FAN_SPEED_CHANNEL_ID, fanValue, "Fan speed", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.stateTopic, "{{value_json.fan_speed}}")
.commandTopic(channelConfiguration.setFanSpeedTopic, false, 1).build();
.stateTopic(channelConfiguration.state_topic, "{{value_json.fan_speed}}")
.commandTopic(channelConfiguration.set_fan_speed_topic, false, 1).build();
}

// {"mainBrush":"220.6","sideBrush":"120.6","filter":"70.6","sensor":"0.0","currentCleanTime":"0.0","currentCleanArea":"0.0","cleanTime":"79.3","cleanArea":"4439.9","cleanCount":183,"last_run_stats":{"startTime":1613503117000,"endTime":1613503136000,"duration":0,"area":"0.0","errorCode":0,"errorDescription":"No
Expand All @@ -141,124 +142,126 @@ public Vacuum(ComponentFactory.ComponentConfiguration componentConfiguration) {
NumberValue currentCleanTimeValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_CURRENT_CLEAN_TIME_CHANNEL_ID, currentCleanTimeValue, "Current Cleaning Time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.currentCleanTime}}")
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.currentCleanTime}}")
.build();

NumberValue currentCleanAreaValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_CURRENT_CLEAN_AREA_CHANNEL_ID, currentCleanAreaValue, "Current Cleaning Area",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.currentCleanArea}}")
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.currentCleanArea}}")
.build();

NumberValue cleanTimeValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_CLEAN_TIME_CHANNEL_ID, cleanTimeValue, "Cleaning Time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.cleanTime}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.cleanTime}}").build();

NumberValue cleanAreaValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_CLEAN_AREA_CHANNEL_ID, cleanAreaValue, "Cleaned Area",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.cleanArea}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.cleanArea}}").build();

NumberValue cleaCountValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_CLEAN_COUNT_CHANNEL_ID, cleaCountValue, "Cleaning Counter",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.cleanCount}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.cleanCount}}")
.build();

DateTimeValue lastStartTime = new DateTimeValue();
buildChannel(VACUUM_LAST_RUN_START_CHANNEL_ID, lastStartTime, "Last run start time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.startTime}}")
.build();

DateTimeValue lastEndTime = new DateTimeValue();
buildChannel(VACUUM_LAST_RUN_END_CHANNEL_ID, lastEndTime, "Last run end time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.endTime}}")
.build();

NumberValue lastRunDurationValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_LAST_RUN_DURATION_CHANNEL_ID, lastRunDurationValue, "Last run duration",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.duration}}")
.build();

NumberValue lastRunAreaValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_LAST_RUN_AREA_CHANNEL_ID, lastRunAreaValue, "Last run area",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.last_run_stats.area}}")
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.area}}")
.build();

NumberValue lastRunErrorCodeValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_LAST_RUN_ERROR_CODE_CHANNEL_ID, lastRunErrorCodeValue, "Last run error code",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.errorCode}}")
.build();

TextValue lastRunErrorDescriptionValue = new TextValue();
buildChannel(VACUUM_LAST_RUN_ERROR_DESCRIPTION_CHANNEL_ID, lastRunErrorDescriptionValue,
"Last run error description", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.errorDescription}}")
.build();

// true/false doesnt map to ON/OFF => use TextValue instead of OnOffValue
TextValue lastRunFinishedFlagValue = new TextValue();
buildChannel(VACUUM_LAST_RUN_FINISHED_FLAG_CHANNEL_ID, lastRunFinishedFlagValue, "Last run finished flag",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic,
.stateTopic(channelConfiguration.json_attributes_topic,
"{{value_json.last_run_stats.finishedFlag}}")
.build();

// only for valetudo re => advanced channels
DateTimeValue binInValue = new DateTimeValue();
buildChannel(VACUUM_BIN_IN_TIME_CHANNEL_ID, binInValue, "Bin In Time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.bin_in_time}}")
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.bin_in_time}}")
.isAdvanced(true).build();

DateTimeValue lastBinOutValue = new DateTimeValue();
buildChannel(VACUUM_LAST_BIN_OUT_TIME_CHANNEL_ID, lastBinOutValue, "Last Bin Out Time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.last_bin_out}}")
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.last_bin_out}}")
.isAdvanced(true).build();

DateTimeValue lastBinFullValue = new DateTimeValue();
buildChannel(VACUUM_LAST_BIN_FULL_TIME_CHANNEL_ID, lastBinFullValue, "Last Bin Full Time",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.last_bin_full}}")
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.last_bin_full}}")
.isAdvanced(true).build();
}

NumberValue mainBrush = new NumberValue(null, null, null, null);
buildChannel(VACUUM_MAIN_BRUSH_CHANNEL_ID, mainBrush, "Main brush usage",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.mainBrush}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.mainBrush}}").build();

NumberValue sideBrush = new NumberValue(null, null, null, null);
buildChannel(VACUUM_SIDE_BRUSH_CHANNEL_ID, sideBrush, "Side brush usage",
componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.sideBrush}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.sideBrush}}").build();

NumberValue filterValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_FILTER_CHANNEL_ID, filterValue, "Filter time", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.filter}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.filter}}").build();

NumberValue sensorValue = new NumberValue(null, null, null, null);
buildChannel(VACUUM_SENSOR_CHANNEL_ID, sensorValue, "Sensor", componentConfiguration.getUpdateListener())
.stateTopic(channelConfiguration.jsonAttributesTopic, "{{value_json.sensor}}").build();
.stateTopic(channelConfiguration.json_attributes_topic, "{{value_json.sensor}}").build();

// if we have a custom command channel for zone cleanup, etc => create text channel
if (channelConfiguration.sendCommandTopic != null) {
if (channelConfiguration.send_command_topic != null) {
TextValue customCommandValue = new TextValue();
buildChannel(VACUUM_CUSMTOM_COMMAND_CHANNEL_ID, customCommandValue, "Custom Command",
componentConfiguration.getUpdateListener())
.commandTopic(channelConfiguration.sendCommandTopic, false, 1)
.stateTopic(channelConfiguration.sendCommandTopic).build();
.commandTopic(channelConfiguration.send_command_topic, false, 1)
.stateTopic(channelConfiguration.send_command_topic).build();
}
}
}

0 comments on commit ed62a4a

Please sign in to comment.