diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/MqttEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/MqttEventHandler.java index 5f2104c..15ac5aa 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/MqttEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/MqttEventHandler.java @@ -42,12 +42,13 @@ protected DirigeraApi getApi() { } protected 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()); } @@ -57,6 +58,10 @@ protected 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 void subscribe(final String topic, final Class payloadType, final Consumer consumer) { log.debug("Subscribe to MQTT topic: topic={}, payload={}", topic, payloadType.getSimpleName()); try { diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassAirPurifierEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassAirPurifierEventHandler.java index dce4f08..6d467e1 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassAirPurifierEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassAirPurifierEventHandler.java @@ -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( @@ -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)); } diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassBlindsDeviceEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassBlindsDeviceEventHandler.java index 0ff2199..9bc08b2 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassBlindsDeviceEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassBlindsDeviceEventHandler.java @@ -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( @@ -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)); } diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEnvironmentSensorEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEnvironmentSensorEventHandler.java index 3e3a0ab..bda6787 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEnvironmentSensorEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEnvironmentSensorEventHandler.java @@ -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); } @@ -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)); } diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassLightDeviceEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassLightDeviceEventHandler.java index 4b0ae0d..4051e49 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassLightDeviceEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassLightDeviceEventHandler.java @@ -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), @@ -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); } diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassOutletDeviceEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassOutletDeviceEventHandler.java index 4dfe1d1..38b26cb 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassOutletDeviceEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassOutletDeviceEventHandler.java @@ -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( @@ -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); }