Skip to content

Commit

Permalink
Use parseFloat for potential unit-containing states
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Krug <[email protected]>
  • Loading branch information
michikrug committed Nov 26, 2023
2 parents b11300e + 65ac812 commit 6441cd6
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If you have any issues, questions or an idea for additional features, please tak
## Latest Changes

::: tip State of this document
This documentation refers to release [v3.8.0](https://github.com/openhab/openhab-google-assistant/releases/tag/v3.8.0) of [openHAB Google Assistant](https://github.com/openhab/openhab-google-assistant) published on 2023-11-03
This documentation refers to release [v3.8.1](https://github.com/openhab/openhab-google-assistant/releases/tag/v3.8.1) of [openHAB Google Assistant](https://github.com/openhab/openhab-google-assistant) published on 2023-11-26
:::

### v3.8.0
Expand Down
4 changes: 2 additions & 2 deletions functions/devices/charger.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Charger extends DefaultDevice {
state.isPluggedIn = members[member].state === 'ON';
break;
case 'chargerCapacityRemaining': {
const capacity = Math.round(Number(members[member].state));
const capacity = Math.round(parseFloat(members[member].state));
if (!config.unit || config.unit === 'PERCENTAGE') {
let descCapacity = 'UNKNOWN';
if (capacity <= 10) {
Expand Down Expand Up @@ -68,7 +68,7 @@ class Charger extends DefaultDevice {
state.capacityUntilFull = [
{
unit: config.unit || 'PERCENTAGE',
rawValue: Math.round(Number(members[member].state))
rawValue: Math.round(parseFloat(members[member].state))
}
];
break;
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/climatesensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ClimateSensor extends DefaultDevice {
state.temperatureSetpointCelsius = temperature;
}
if ('humidityAmbient' in members) {
const humidity = Math.round(Number(members.humidityAmbient.state));
const humidity = Math.round(parseFloat(members.humidityAmbient.state));
state.humidityAmbientPercent = humidity;
state.humiditySetpointPercent = humidity;
}
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/dimmablelight.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DimmableLight extends DefaultDevice {
}

static getState(item) {
const brightness = Math.round(Number(item.state)) || 0;
const brightness = Math.round(parseFloat(item.state)) || 0;
return {
on: brightness > 0,
brightness: brightness
Expand Down
25 changes: 13 additions & 12 deletions functions/devices/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,30 @@ class Fan extends DefaultDevice {
const config = this.getConfig(item);
const itemType = item.groupType || item.type;
if (itemType !== 'Group') {
const itemState = Math.round(parseFloat(item.state));
const state = {
currentFanSpeedPercent: Math.round(Number(item.state)),
on: Number(item.state) > 0
currentFanSpeedPercent: itemState,
on: itemState > 0
};
if (config.fanSpeeds) {
state.currentFanSpeedSetting = item.state.toString();
state.currentFanSpeedSetting = itemState.toString();
}
return state;
} else {
const state = {};
const config = this.getConfig(item);
const members = this.getMembers(item);
if ('fanPower' in members) {
state.on = members.fanPower.state === 'ON';
} else if ('fanSpeed' in members) {
state.on = Number(members.fanSpeed.state) > 0;
}
if ('fanSpeed' in members) {
state.currentFanSpeedPercent = Number(members.fanSpeed.state);
const itemState = Math.round(parseFloat(members.fanSpeed.state));
state.on = itemState > 0;
state.currentFanSpeedPercent = itemState;
if (config.fanSpeeds) {
state.currentFanSpeedSetting = members.fanSpeed.state.toString();
state.currentFanSpeedSetting = itemState.toString();
}
}
if ('fanPower' in members) {
state.on = members.fanPower.state === 'ON';
}
if ('fanMode' in members && config.fanModeName && config.fanModeSettings) {
const modeNames = config.fanModeName.split(',').map((s) => s.trim());
state.currentModeSettings = {
Expand All @@ -152,7 +153,7 @@ class Fan extends DefaultDevice {
if ('fanFilterLifeTime' in members || 'fanPM25' in members) {
state.currentSensorStateData = [];
if ('fanFilterLifeTime' in members) {
const itemState = Number(members.fanFilterLifeTime.state);
const itemState = parseFloat(members.fanFilterLifeTime.state);
state.currentSensorStateData.push({
name: 'FilterLifeTime',
currentSensorState: this.translateFilterLifeTime(itemState),
Expand All @@ -162,7 +163,7 @@ class Fan extends DefaultDevice {
if ('fanPM25' in members) {
state.currentSensorStateData.push({
name: 'PM2.5',
rawValue: Number(members.fanPM25.state)
rawValue: parseFloat(members.fanPM25.state)
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/humiditysensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HumiditySensor extends DefaultDevice {
}

static getState(item) {
const state = Math.round(Number(item.state));
const state = Math.round(parseFloat(item.state));
return {
humidityAmbientPercent: state,
humiditySetpointPercent: state
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/openclosedevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OpenCloseDevice extends DefaultDevice {
let state = 0;
const itemType = item.groupType || item.type;
if (itemType === 'Rollershutter') {
state = Number(item.state);
state = Math.round(parseFloat(item.state));
} else {
state = item.state === 'ON' || item.state === 'OPEN' ? 0 : 100;
}
Expand Down
10 changes: 6 additions & 4 deletions functions/devices/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ class Sensor extends DefaultDevice {

static getState(item) {
const config = this.getConfig(item);
return {
const state = {
currentSensorStateData: [
{
name: config.sensorName,
currentSensorState: this.translateStateToGoogle(item),
rawValue: Number(item.state) || 0
currentSensorState: this.translateStateToGoogle(item)
}
]
};
const rawValue = parseFloat(item.state);
if (!isNaN(rawValue)) state.currentSensorStateData[0].rawValue = rawValue;
return state;
}

static translateStateToGoogle(item) {
Expand All @@ -53,7 +55,7 @@ class Sensor extends DefaultDevice {
const states = config.states.split(',').map((s) => s.trim());
for (const state of states) {
const [key, value] = state.split('=').map((s) => s.trim());
if (value === item.state) {
if (value === item.state || value === parseFloat(item.state).toFixed(0)) {
return key;
}
}
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/speaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Speaker extends DefaultDevice {

static getState(item) {
return {
currentVolume: Math.round(Number(item.state)) || 0
currentVolume: Math.round(parseFloat(item.state)) || 0
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions functions/devices/specialcolorlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SpecialColorLight extends DefaultDevice {
state.on = members[member].state === 'ON';
break;
case 'lightBrightness':
state.brightness = Math.round(Number(members[member].state)) || 0;
state.brightness = Math.round(parseFloat(members[member].state)) || 0;
if (!('lightPower' in members)) {
state.on = state.brightness > 0;
}
Expand Down Expand Up @@ -101,15 +101,15 @@ class SpecialColorLight extends DefaultDevice {
const colorUnit = this.getColorUnit(item);
if (colorUnit === 'kelvin') {
state.color = {
temperatureK: Math.round(Number(members[member].state))
temperatureK: Math.round(parseFloat(members[member].state))
};
} else if (colorUnit === 'mired') {
state.color = {
temperatureK: convertMired(Math.round(Number(members[member].state)))
temperatureK: convertMired(Math.round(parseFloat(members[member].state)))
};
} else {
const { temperatureMinK, temperatureMaxK } = this.getAttributes(item).colorTemperatureRange;
let percent = Number(members[member].state);
let percent = parseFloat(members[member].state);
if (this.getColorTemperatureInverted(item)) {
percent = 100 - percent;
}
Expand Down
2 changes: 1 addition & 1 deletion functions/devices/tv.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TV extends DefaultDevice {
state.playbackState = members[member].state;
break;
case 'tvVolume':
state.currentVolume = Math.round(Number(members[member].state)) || 0;
state.currentVolume = Math.round(parseFloat(members[member].state)) || 0;
break;
case 'tvChannel':
state.channelNumber = members[member].state;
Expand Down
2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openhab.google-assistant-smarthome.cloud-function",
"version": "3.8.0",
"version": "3.8.1",
"description": "A Google Assistant, Actions on Google based implementation for openHAB",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openhab.google-assistant-smarthome",
"version": "3.8.0",
"version": "3.8.1",
"description": "A Google Assistant, Actions on Google based implementation for openHAB",
"main": "functions/index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/charger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('Charger Device', () => {
},
{
name: 'CapacityRemaining',
state: '4000.123',
state: '4000.123 wh',
type: 'Number',
metadata: {
ga: {
Expand Down
4 changes: 2 additions & 2 deletions tests/devices/climatesensor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('ClimateSensor Device', () => {
},
{
name: 'Humidity',
state: '59.7',
state: '59.7 %',
type: 'Number',
metadata: {
ga: {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('ClimateSensor Device', () => {
members: [
{
name: 'Humidity',
state: '30.3',
state: '30.3 %',
type: 'Number',
metadata: {
ga: {
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/dimmablelight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('DimmableLight Device', () => {
});

test('getState', () => {
expect(Device.getState({ state: '50' })).toStrictEqual({
expect(Device.getState({ state: '50 %' })).toStrictEqual({
on: true,
brightness: 50
});
Expand Down
6 changes: 3 additions & 3 deletions tests/devices/fan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe('Fan Device', () => {
value: 'fanSpeed'
}
},
state: '50'
state: '50 %'
}
]
};
Expand Down Expand Up @@ -437,7 +437,7 @@ describe('Fan Device', () => {
value: 'fanFilterLifeTime'
}
},
state: '70'
state: '70 h'
}
]
};
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('Fan Device', () => {
value: 'fanPM25'
}
},
state: '20'
state: '20 ppm'
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/humiditysensor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('HumiditySensor Device', () => {
});

test('getState', () => {
expect(Device.getState({ state: '10.3' })).toStrictEqual({
expect(Device.getState({ state: '9.6 %' })).toStrictEqual({
humidityAmbientPercent: 10,
humiditySetpointPercent: 10
});
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/openclosedevice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('OpenCloseDevice Device', () => {
test('getState Rollershutter', () => {
const item = {
type: 'Rollershutter',
state: '25'
state: '25 %'
};
expect(Device.getState(item)).toStrictEqual({
openPercent: 75
Expand Down
25 changes: 24 additions & 1 deletion tests/devices/sensor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Sensor Device', () => {
}
}
},
state: '10'
state: '10 ppm'
};
expect(Device.getState(item)).toStrictEqual({
currentSensorStateData: [
Expand All @@ -110,6 +110,29 @@ describe('Sensor Device', () => {
});
});

test('getState with string state', () => {
const item = {
metadata: {
ga: {
config: {
sensorName: 'Sensor',
valueUnit: 'AQI',
states: 'good=good,moderate=moderate,poor=poor'
}
}
},
state: 'moderate'
};
expect(Device.getState(item)).toStrictEqual({
currentSensorStateData: [
{
currentSensorState: 'moderate',
name: 'Sensor'
}
]
});
});

test('getState no matching state', () => {
const item = {
metadata: {
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/speaker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Speaker Device', () => {
});

test('getState', () => {
expect(Device.getState({ state: '10' })).toStrictEqual({
expect(Device.getState({ state: '10 %' })).toStrictEqual({
currentVolume: 10
});
expect(Device.getState({ state: '90' })).toStrictEqual({
Expand Down
4 changes: 2 additions & 2 deletions tests/devices/specialcolorlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ describe('SpecialColorLight Device', () => {
},
members: [
{
state: '0',
state: '0 %',
type: 'Number',
metadata: {
ga: {
Expand All @@ -514,7 +514,7 @@ describe('SpecialColorLight Device', () => {
}
},
{
state: '20',
state: '20 K',
type: 'Number',
metadata: {
ga: {
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/tv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ describe('TV Device', () => {
}
},
{
state: '50',
state: '50 %',
type: 'Dimmer',
metadata: {
ga: {
Expand Down

0 comments on commit 6441cd6

Please sign in to comment.