[mqtt.homeassistant] sensors with a state_class are numeric (#13398)

see reference in code comment, but a measurement sensor is assumed to
be numeric, even if it doesn't have a unit

Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
Cody Cutrer
2022-09-18 11:46:07 -06:00
committed by GitHub
parent 2fd2e5175f
commit a2b6dd77b7
2 changed files with 60 additions and 0 deletions

View File

@@ -49,6 +49,8 @@ public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
protected @Nullable String unitOfMeasurement;
@SerializedName("device_class")
protected @Nullable String deviceClass;
@SerializedName("state_class")
protected @Nullable String stateClass;
@SerializedName("force_update")
protected boolean forceUpdate = false;
@SerializedName("expire_after")
@@ -70,9 +72,14 @@ public class Sensor extends AbstractComponent<Sensor.ChannelConfiguration> {
Value value;
String uom = channelConfiguration.unitOfMeasurement;
String sc = channelConfiguration.stateClass;
if (uom != null && !uom.isBlank()) {
value = new NumberValue(null, null, null, UnitUtils.parseUnit(uom));
} else if (sc != null && !sc.isBlank()) {
// see state_class at https://developers.home-assistant.io/docs/core/entity/sensor#properties
// > If not None, the sensor is assumed to be numerical
value = new NumberValue(null, null, null, null);
} else {
value = new TextValue();
}