-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from 0x6e3078/enviroment_sensor_function
Implement basic function for VINDSTYRKA Sensor
- Loading branch information
Showing
4 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 16 additions & 2 deletions
18
.../dirigera/client/api/model/device/environmentsensor/EnvironmentSensorStateAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...n/java/de/dvdgeisler/iot/dirigera/client/mqtt/hass/HassEnvironmentSensorEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package de.dvdgeisler.iot.dirigera.client.mqtt.hass; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import de.dvdgeisler.iot.dirigera.client.api.DirigeraApi; | ||
import de.dvdgeisler.iot.dirigera.client.api.EnvironmentSensorDeviceApi; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.device.environmentsensor.EnvironmentSensorAttributes; | ||
import de.dvdgeisler.iot.dirigera.client.api.model.device.environmentsensor.EnvironmentSensorDevice; | ||
import de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.DeviceAvailability; | ||
import de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.DeviceAvailabilityState; | ||
import de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.environmentsensor.EnvironmentSensorConfig; | ||
import org.eclipse.paho.client.mqttv3.MqttClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class HassEnvironmentSensorEventHandler extends HassDeviceEventHandler<EnvironmentSensorDevice> { | ||
|
||
private static final String HASS_COMPONENT = "sensor"; | ||
private static final String TOPIC_PM25 = "pm25"; | ||
private static final String TOPIC_HUMIDITY = "humidity"; | ||
private static final String TOPIC_TEMPERATURE = "temperature"; | ||
private static final String TOPIC_VOCINDEX = "vocindex"; | ||
private static final String DEFAULT_VALUE = "{{value}}"; | ||
|
||
private final EnvironmentSensorDeviceApi api; | ||
|
||
|
||
public HassEnvironmentSensorEventHandler( | ||
final MqttClient mqtt, | ||
final DirigeraApi api, | ||
@Value("${dirigera.mqtt.hass.prefix:homeassistant}") | ||
final String topicPrefix, | ||
final ObjectMapper objectMapper) { | ||
super(mqtt, api, EnvironmentSensorDevice.class, topicPrefix, objectMapper); | ||
|
||
this.api = api.device.environmentSensor; | ||
Objects.requireNonNull(this.api.all() | ||
.block()) | ||
.forEach(this::onDeviceCreated); | ||
} | ||
|
||
@Override | ||
protected void onDeviceCreated(EnvironmentSensorDevice device) { | ||
final EnvironmentSensorConfig config; | ||
|
||
config = new EnvironmentSensorConfig(); | ||
config.unique_id = config.object_id = device.id; | ||
config.name = getDefaultName(device); | ||
config.device = this.getDeviceConfig(device); | ||
|
||
config.command_topic = this.getTopic(device, HASS_COMPONENT, TOPIC_SET); | ||
config.state_topic = this.getTopic(device, HASS_COMPONENT, TOPIC_STATE); | ||
config.schema = "json"; | ||
|
||
config.currentTemperature = DEFAULT_VALUE; | ||
config.currentRH = DEFAULT_VALUE; | ||
config.currentPM25 = DEFAULT_VALUE; | ||
config.maxMeasuredPM25 = DEFAULT_VALUE; | ||
config.minMeasuredPM25 = DEFAULT_VALUE; | ||
config.vocIndex = DEFAULT_VALUE; | ||
config.value_template = DEFAULT_VALUE; | ||
|
||
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); | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_CONFIG), config); | ||
this.onDeviceStateChanged(device); | ||
} | ||
|
||
@Override | ||
protected void onDeviceStateChanged(EnvironmentSensorDevice device) { | ||
getAttributes(device).ifPresent(state -> | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_STATE), state)); | ||
|
||
getCurrentPM25(device).ifPresent(value -> | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_PM25), value)); | ||
getCurrentRH(device).ifPresent(value -> | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_HUMIDITY), value)); | ||
getCurrentTemperature(device).ifPresent(value -> | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_TEMPERATURE), value)); | ||
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)); | ||
} | ||
|
||
@Override | ||
protected void onDeviceRemoved(EnvironmentSensorDevice device) { | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_AVAILABILITY), DeviceAvailabilityState.OFFLINE); | ||
this.publish(this.getTopic(device, HASS_COMPONENT, TOPIC_REMOVE), null); | ||
this.unsubscribe(this.getTopic(device, HASS_COMPONENT, TOPIC_SET)); | ||
} | ||
|
||
public static Optional<EnvironmentSensorAttributes> getAttributes(final EnvironmentSensorDevice device) { | ||
return Optional.of(device) | ||
.map(d->d.attributes); | ||
} | ||
|
||
public static Optional<Integer> getCurrentPM25(final EnvironmentSensorDevice device) { | ||
return Optional.of(device) | ||
.map(d->d.attributes) | ||
.map(d->d.currentPM25); | ||
} | ||
|
||
public static Optional<Integer> getCurrentRH(final EnvironmentSensorDevice device) { | ||
return Optional.of(device) | ||
.map(d->d.attributes) | ||
.map(d->d.currentRH); | ||
} | ||
|
||
public static Optional<Integer> getCurrentTemperature(final EnvironmentSensorDevice device) { | ||
return Optional.of(device) | ||
.map(d->d.attributes) | ||
.map(d->d.currentTemperature); | ||
} | ||
|
||
public static Optional<Integer> getVocIndex(final EnvironmentSensorDevice device) { | ||
return Optional.of(device) | ||
.map(d->d.attributes) | ||
.map(d->d.vocIndex); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...eisler/iot/dirigera/client/mqtt/hass/model/environmentsensor/EnvironmentSensorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.environmentsensor; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.Device; | ||
import de.dvdgeisler.iot.dirigera.client.mqtt.hass.model.DeviceAvailability; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class EnvironmentSensorConfig { | ||
|
||
public String object_id; | ||
public String unique_id; | ||
public String name; | ||
public Device device; | ||
|
||
public String command_topic; | ||
public String state_topic; | ||
public String schema; | ||
|
||
public String currentTemperature; | ||
public String currentRH; | ||
public String currentPM25; | ||
public String maxMeasuredPM25; | ||
public String minMeasuredPM25; | ||
public String vocIndex; | ||
public String identifyStarted; | ||
public String identifyPeriod; | ||
|
||
public String value_template; | ||
public DeviceAvailability availability; | ||
|
||
public EnvironmentSensorConfig(String object_id, String unique_id, String name, Device device, | ||
String currentTemperature, String currentRH, String currentPM25, String maxMeasuredPM25, | ||
String minMeasuredPM25, String vocIndex, String identifyStarted, String identifyPeriod, | ||
String value_template, DeviceAvailability availability, final String command_topic, final String state_topic, final String schema) { | ||
this.object_id = object_id; | ||
this.unique_id = unique_id; | ||
this.name = name; | ||
this.device = device; | ||
|
||
this.currentTemperature=currentTemperature; | ||
this.currentRH=currentRH; | ||
this.currentPM25=currentPM25; | ||
this.maxMeasuredPM25=maxMeasuredPM25; | ||
this.minMeasuredPM25=minMeasuredPM25; | ||
this.vocIndex=vocIndex; | ||
this.identifyStarted=identifyStarted; | ||
this.identifyPeriod=identifyPeriod; | ||
|
||
this.value_template = value_template; | ||
this.availability = availability; | ||
this.command_topic = command_topic; | ||
this.state_topic = state_topic; | ||
this.schema = schema; | ||
} | ||
|
||
public EnvironmentSensorConfig() { | ||
} | ||
|
||
} |