diff --git a/lib/converters/genericConverter.js b/lib/converters/genericConverter.js index 3b39bb95f..1e5b393bd 100644 --- a/lib/converters/genericConverter.js +++ b/lib/converters/genericConverter.js @@ -1,3 +1,54 @@ +exports.numericDeviceClasses = [ + 'voltage', + 'sulphur_dioxide', + 'carbon_dioxide', + 'current', + 'volume_storage', + 'volume', + 'ozone', + 'speed', + 'atmospheric_pressure', + 'carbon_monoxide', + 'aqi', + 'pm25', + 'nitrogen_monoxide', + 'power_factor', + 'pm1', + 'precipitation', + 'volatile_organic_compounds', + 'humidity', + 'pressure', + 'battery', + 'irradiance', + 'wind_speed', + 'pm10', + 'ph', + 'reactive_power', + 'temperature', + 'precipitation_intensity', + 'sound_pressure', + 'data_rate', + 'frequency', + 'volatile_organic_compounds_parts', + 'energy', + 'apparent_power', + 'weight', + 'duration', + 'gas', + 'water', + 'power', + 'monetary', + 'signal_strength', + 'data_size', + 'energy_storage', + 'nitrogen_dioxide', + 'nitrous_oxide', + 'illuminance', + 'moisture', + 'distance' +]; + +const numericDeviceClassesIob = exports.numericDeviceClasses.concat(['timestamp']); exports.iobState2EntityState = function (entity, val, attribute) { let type = entity.context.type; @@ -10,8 +61,8 @@ exports.iobState2EntityState = function (entity, val, attribute) { return val ? 'on' : 'off'; } else if (type === 'binary_sensor') { return val ? 'on' : 'off'; - } else if (typeof val === 'number' && entity.attributes && ['timestamp'].includes(entity.attributes.device_class)) { - return val; //do not convert timestamps. + } else if (typeof val === 'number' && entity.attributes && numericDeviceClassesIob.includes(entity.attributes.device_class)) { + return val; //do not convert timestamps or values that have a unit of measurement.. } else if (typeof val === 'number' && entity.attributes && ['date'].includes(entity.attributes.device_class)) { const dateStr = new Date(val).toDateString(); //convert to date string return dateStr === 'Invalid Date' ? 'unknown' : dateStr; diff --git a/lib/server.js b/lib/server.js index 61c4b5dcc..3caff4211 100644 --- a/lib/server.js +++ b/lib/server.js @@ -31,6 +31,7 @@ const entityData = require('./dataSingleton'); const bindings = require('./bindings'); const getFriendlyName = require('./entities/friendly_name').getFriendlyName; const iobState2EntityState = require('./converters/genericConverter').iobState2EntityState; +const NUMERIC_DEVICE_CLASSES = require('./converters/genericConverter').numericDeviceClasses; const HistoryModule = require('./modules/history'); const ConversationModule = require('./modules/conversation'); const LogbookModule = require('./modules/logbook'); @@ -2472,6 +2473,9 @@ class WebServer { }; ws.send(JSON.stringify(t)); }); + } else if (message.type === 'sensor/numeric_device_classes') { + //it seems frontend now asks backend for what device_classes are numeric. Ok. Let's use that. ;-) + this._sendResponse(ws, message.id, { numeric_device_classes: NUMERIC_DEVICE_CLASSES }); } else if (message.type.startsWith('browser_mod')) { await this._browserMod.processBrowserModMessage(ws, message); } else if (message.type.startsWith('history/')) {