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

Improve state presentation symbol extract logic #267

Merged
merged 1 commit into from
Aug 29, 2019
Merged
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
26 changes: 22 additions & 4 deletions lambda/smarthome/alexa/propertyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ const normalizeParameters = {
if (!property.parameters.unitOfMeasure || !getUnitOfMeasure({id: property.parameters.unitOfMeasure})) {
property.parameters.unitOfMeasure = getUnitOfMeasure({
dimension: item.type.split(':')[1],
symbol: sprintf(item.stateDescription && item.stateDescription.pattern, '42')
.split(/\d+\s*(?=\S)/).pop().trim(),
symbol: getStatePresentationSymbol(item.stateDescription && item.stateDescription.pattern),
system: settings.regional &&
(settings.regional.measurementSystem || settings.regional.region),
property: 'id'
Expand All @@ -211,8 +210,7 @@ const normalizeParameters = {
if (!temperatureScale) {
temperatureScale = getUnitOfMeasure({
dimension: 'Temperature',
symbol: sprintf(item.stateDescription && item.stateDescription.pattern, '42')
.split(/\d+\s*(?=\S)/).pop().trim(),
symbol: getStatePresentationSymbol(item.stateDescription && item.stateDescription.pattern),
system: settings.regional &&
(settings.regional.measurementSystem || settings.regional.region),
property: 'unit'
Expand Down Expand Up @@ -249,6 +247,26 @@ const normalizeParameters = {
}
};

/**
* Defines item state presentation pattern
* @type {RegExp}
*/
const STATE_PRESENTATION_PATTERN = /[\d.]+\s*(?=.{1,4}$)([%'"]|°[CF]?|[a-zA-Z/]+\d?)$/;

/**
* Returns item state presentation symbol
* @param {String} format
* @return {String}
*/
function getStatePresentationSymbol(format) {
try {
// Use a random number to format the state presentation
return sprintf(format, Math.random()).match(STATE_PRESENTATION_PATTERN)[1];
} catch (e) {
return '';
}
}

/**
* Defines property map class to associate items to an endpoint from metadata, per the description below:
*
Expand Down