Skip to content

Commit

Permalink
add support for numeric_device_classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfonso committed Dec 9, 2023
1 parent f3a8620 commit cb3811c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
55 changes: 53 additions & 2 deletions lib/converters/genericConverter.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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/')) {
Expand Down

0 comments on commit cb3811c

Please sign in to comment.