Skip to content

Commit

Permalink
Set availability payload to pure string instead of json
Browse files Browse the repository at this point in the history
- Before this change the payload_available/payload_not_available was set to a json string, and thus the the configuration json had a json within the json (quoted strings). This PR set the availabilty to be only just a string.
  • Loading branch information
Johboh committed Dec 22, 2023
1 parent 46f6976 commit 4a47df8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ protected DirigeraApi getApi() {
}

protected <T> void publish(final String topic, final T payload, final int qos, final boolean retained) {
final String json;
publishString(topic, this.toJSON(payload), qos, retained);
}

json = this.toJSON(payload);
protected void publishString(final String topic, final String payload, final int qos, final boolean retained) {
try {
log.debug("Publish to MQTT: topic={}, payload={}, qos={}, retained={}", topic, json, qos, retained);
this.mqtt.publish(topic, json.getBytes(StandardCharsets.UTF_8), qos, retained);
log.debug("Publish to MQTT: topic={}, payload={}, qos={}, retained={}", topic, payload, qos, retained);
this.mqtt.publish(topic, payload.getBytes(StandardCharsets.UTF_8), qos, retained);
} catch (MqttException e) {
log.error("Error while publishing to MQTT topic: {}", e.getMessage());
}
Expand All @@ -57,6 +58,10 @@ protected <T> void publish(final String topic, final T payload) {
this.publish(topic, payload, 1, true);
}

protected void publishString(final String topic, final String payload) {
this.publishString(topic, payload, 1, true);
}

protected <T> void subscribe(final String topic, final Class<T> payloadType, final Consumer<T> consumer) {
log.debug("Subscribe to MQTT topic: topic={}, payload={}", topic, payloadType.getSimpleName());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ protected void onDeviceCreated(final AirPurifierDevice device) {

config.availability = new DeviceAvailability();
config.availability.topic = this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY);
config.availability.payload_available = this.toJSON(DeviceAvailabilityState.ONLINE);
config.availability.payload_not_available = this.toJSON(DeviceAvailabilityState.OFFLINE);
config.availability.payload_available = DeviceAvailabilityState.ONLINE.toString();
config.availability.payload_not_available = DeviceAvailabilityState.OFFLINE.toString();
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config);

this.subscribe(
Expand All @@ -80,12 +80,12 @@ protected void onDeviceStateChanged(final AirPurifierDevice device) {
getState(device).ifPresent(state ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_STATE), state));
getAvailability(device).ifPresent(s ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s));
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s.toString()));
}

@Override
protected void onDeviceRemoved(final AirPurifierDevice device) {
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE);
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE.toString());
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null);
this.unsubscribe(this.getTopic(device, HASS_COMPONENT, TOPIC_SET));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ protected void onDeviceCreated(final BlindsDevice device) {

config.availability = new DeviceAvailability();
config.availability.topic = this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY);
config.availability.payload_available = this.toJSON(DeviceAvailabilityState.ONLINE);
config.availability.payload_not_available = this.toJSON(DeviceAvailabilityState.OFFLINE);
config.availability.payload_available = DeviceAvailabilityState.ONLINE.toString();
config.availability.payload_not_available = DeviceAvailabilityState.OFFLINE.toString();
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config);

this.subscribe(
Expand All @@ -86,12 +86,12 @@ protected void onDeviceStateChanged(final BlindsDevice device) {
getPosition(device).ifPresent(position ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_POSITION), position));
getAvailability(device).ifPresent(s ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s));
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s.toString()));
}

@Override
protected void onDeviceRemoved(final BlindsDevice device) {
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE);
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE.toString());
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null);
this.unsubscribe(this.getTopic(device, HASS_COMPONENT, TOPIC_SET));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected void onDeviceCreated(EnvironmentSensorDevice device) {
config.availability = new DeviceAvailability();
config.availability.topic = this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY);

config.availability.payload_available = this.toJSON(DeviceAvailabilityState.ONLINE);
config.availability.payload_not_available = this.toJSON(DeviceAvailabilityState.OFFLINE);
config.availability.payload_available = DeviceAvailabilityState.ONLINE.toString();
config.availability.payload_not_available = DeviceAvailabilityState.OFFLINE.toString();
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config);
this.onDeviceStateChanged(device);
}
Expand All @@ -86,12 +86,12 @@ protected void onDeviceStateChanged(EnvironmentSensorDevice device) {
getVocIndex(device).ifPresent(value ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_VOCINDEX), value));
getAvailability(device).ifPresent(s ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s));
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s.toString()));
}

@Override
protected void onDeviceRemoved(EnvironmentSensorDevice device) {
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE);
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE.toString());
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null);
this.unsubscribe(this.getTopic(device, HASS_COMPONENT, TOPIC_SET));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ protected void onDeviceCreated(final LightDevice device) {

config.availability = new DeviceAvailability();
config.availability.topic = this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY);
config.availability.payload_available = this.toJSON(DeviceAvailabilityState.ONLINE);
config.availability.payload_not_available = this.toJSON(DeviceAvailabilityState.OFFLINE);
config.availability.payload_available = DeviceAvailabilityState.ONLINE.toString();
config.availability.payload_not_available = DeviceAvailabilityState.OFFLINE.toString();
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config);

this.subscribe(this.getTopic(device, HASS_COMPONENT, TOPIC_SET),
Expand Down Expand Up @@ -133,12 +133,12 @@ protected void onDeviceStateChanged(final LightDevice device) {
status);

getAvailability(device).ifPresent(s ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s));
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s.toString()));
}

@Override
protected void onDeviceRemoved(final LightDevice device) {
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE);
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE.toString());
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected void onDeviceCreated(final OutletDevice device) {

config.availability = new DeviceAvailability();
config.availability.topic = this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY);
config.availability.payload_available = this.toJSON(DeviceAvailabilityState.ONLINE);
config.availability.payload_not_available = this.toJSON(DeviceAvailabilityState.OFFLINE);
config.availability.payload_available = DeviceAvailabilityState.ONLINE.toString();
config.availability.payload_not_available = DeviceAvailabilityState.OFFLINE.toString();
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config);

this.subscribe(
Expand All @@ -73,12 +73,12 @@ protected void onDeviceStateChanged(final OutletDevice device) {
getState(device).ifPresent(state ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_STATE), state));
getAvailability(device).ifPresent(s ->
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s));
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), s.toString()));
}

@Override
protected void onDeviceRemoved(final OutletDevice device) {
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE);
this.publishString(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE.toString());
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null);
}

Expand Down

0 comments on commit 4a47df8

Please sign in to comment.