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

Implement basic function for VINDSTYRKA Sensor #91

Merged
merged 1 commit into from
Jun 28, 2023
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 @@ -11,9 +11,6 @@

public class EnvironmentSensorDevice extends Device<EnvironmentSensorAttributes, EnvironmentSensorConfigurationAttributes> {

public EnvironmentSensorDevice() {
}

public EnvironmentSensorDevice(
final String id,
final LocalDateTime createdAt,
Expand All @@ -26,4 +23,7 @@ public EnvironmentSensorDevice(
) {
super(id, SENSOR, ENVIRONMENT_SENSOR, createdAt, isReachable, lastSeen, attributes, capabilities, remoteLinks, environmentSensorConfigurationAttributes);
}

public EnvironmentSensorDevice() {
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package de.dvdgeisler.iot.dirigera.client.api.model.device.environmentsensor;

import com.fasterxml.jackson.annotation.JsonInclude;
import de.dvdgeisler.iot.dirigera.client.api.model.device.DeviceStateAttributes;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class EnvironmentSensorStateAttributes extends DeviceStateAttributes {

public EnvironmentSensorStateAttributes(final String customName) {
public Integer currentTemperature;
public Integer currentRH;
public Integer currentPM25;
public Integer maxMeasuredPM25;
public Integer minMeasuredPM25;
public Integer vocIndex;
public EnvironmentSensorStateAttributes(final String customName, final Integer currentTemperature, final Integer currentRH, final Integer currentPM25,
final Integer maxMeasuredPM25, final Integer minMeasuredPM25, final Integer vocIndex) {
super(customName);
this.currentTemperature = currentTemperature;
this.currentRH = currentRH;
this.currentPM25 = currentPM25;
this.maxMeasuredPM25 = maxMeasuredPM25;
this.minMeasuredPM25 = minMeasuredPM25;
this.vocIndex = vocIndex;
}

public EnvironmentSensorStateAttributes() {
Expand Down
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);
}
}
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() {
}

}