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

Set availability payload to pure string instead of json #101

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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