diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEventHandler.java index 529d932..83a4ba4 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEventHandler.java @@ -56,22 +56,35 @@ protected String topic(final MqttBridge mqtt, final _Device device, final String .collect(Collectors.joining("/")); } - protected MqttBridgeMessage<_Device> build(final MqttBridge mqtt, final _Device device, final String topic, final T payload) { + protected MqttBridgeMessage<_Device> build( + final MqttBridge mqtt, + final _Device device, + final String topic, + final T payload) { try { return new MqttBridgeMessage<>( device, this, this.topic(mqtt, device, topic), - new MqttMessage(this.objectMapper.writeValueAsBytes(payload))); + new MqttMessage(this.objectMapper.writeValueAsBytes(payload)){{ + this.setQos(1); + this.setRetained(true); + }}); } catch (JsonProcessingException e) { log.error(e.getMessage()); } return null; } - protected void subscribe(final MqttBridge mqtt, final DirigeraApi api, final _Device device) { + protected void subscribe(final MqttBridge mqtt, final DirigeraApi api, final _Device device, final String topicSuffix) { + final String topic; + try { - mqtt.subscribe(this.topic(mqtt, device, "set"), (topic, message) -> { + topic = this.topic(mqtt, device, topicSuffix); + log.info("Subscribe to device: topic={}, id={}, name={}, category={}, type={}", + topic, device.id, device.attributes.state.customName, device.deviceType, device.type); + + mqtt.subscribe(this.topic(mqtt, device, topicSuffix), (t, message) -> { final _DeviceState state; try { diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/LightEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/LightEventHandler.java index b0e0ca0..5a8438d 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/LightEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/LightEventHandler.java @@ -51,8 +51,8 @@ public void setDevice(final MqttBridge mqtt, final DirigeraApi api, final LightD Optional.ofNullable(status.color_temp) .filter(t -> canReceive(device, "colorTemperature")) - .map(t -> max(t, getMaxTemperatureMireds(device))) // todo: getMaxTemperatureMireds may return null - .map(t -> min(t, getMinTemperatureMireds(device))) // todo: getMinTemperatureMireds may return null + .flatMap(t -> Optional.ofNullable(getMaxTemperatureMireds(device)).map(maxT -> min(t, maxT))) + .flatMap(t -> Optional.ofNullable(getMinTemperatureMireds(device)).map(minT -> max(t, minT))) .map(LightUtils::miredsToKelvin) .map(t -> api.device.light.setTemperature(device, t)) .ifPresent(Mono::block); @@ -81,7 +81,7 @@ public Stream> addDevice( final List colorModes; colorModes = colorModes(device); - this.subscribe(mqtt, api, device); + this.subscribe(mqtt, api, device, TOPIC_SET); return Stream.of(this.build(mqtt, device, TOPIC_CONFIG, new LightConfig( device.id, diff --git a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/OutletEventHandler.java b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/OutletEventHandler.java index 91263ea..38b1935 100644 --- a/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/OutletEventHandler.java +++ b/dirigera-client-mqtt/src/main/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/OutletEventHandler.java @@ -55,7 +55,7 @@ public Stream> addDevice( final DirigeraApi api, final OutletDevice device) { - this.subscribe(mqtt, api, device); + this.subscribe(mqtt, api, device, TOPIC_SET); return Stream.of(this.build(mqtt, device, TOPIC_CONFIG, new OutletConfig( device.id,