Skip to content

Commit

Permalink
Use parseFloat for other potential unit 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
1 parent 99dbc70 commit 64d7cdb
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 27 deletions.
4 changes: 2 additions & 2 deletions functions/devices/charger.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 @@ -64,7 +64,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/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
7 changes: 4 additions & 3 deletions functions/devices/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ class Fan extends DefaultDevice {
}

static getState(item) {
const itemState = Math.round(parseFloat(item.state));
const state = {
on: Number(item.state) > 0
on: itemState > 0
};
const config = this.getConfig(item);
if (config && config.speeds) {
state.currentFanSpeedSetting = item.state.toString();
state.currentFanSpeedSetting = itemState.toString();
} else {
state.currentFanSpeedPercent = Math.round(Number(item.state));
state.currentFanSpeedPercent = itemState;
}
return 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 @@ -32,15 +32,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 @@ -49,7 +51,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 @@ -54,7 +54,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 @@ -83,15 +83,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 @@ -112,7 +112,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 tests/devices/charger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('Charger Device', () => {
},
{
name: 'CapacityRemaining',
state: '4000.123',
state: '4000.123 wh',
metadata: {
ga: {
value: 'chargerCapacityRemaining'
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
4 changes: 2 additions & 2 deletions tests/devices/fan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ describe('Fan Device', () => {
});

test('getState', () => {
expect(Device.getState({ state: '50' })).toStrictEqual({
expect(Device.getState({ state: '50 %' })).toStrictEqual({
currentFanSpeedPercent: 50,
on: true
});
expect(
Device.getState({
state: '50',
state: '50 upm',
metadata: {
ga: {
config: {
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 @@ -96,7 +96,7 @@ describe('Sensor Device', () => {
}
}
},
state: '10'
state: '10 ppm'
};
expect(Device.getState(item)).toStrictEqual({
currentSensorStateData: [
Expand All @@ -109,6 +109,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 @@ -425,15 +425,15 @@ describe('SpecialColorLight Device', () => {
},
members: [
{
state: '0',
state: '0 %',
metadata: {
ga: {
value: 'lightBrightness'
}
}
},
{
state: '80',
state: '80 K',
metadata: {
ga: {
value: 'lightColorTemperature'
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 @@ -500,7 +500,7 @@ describe('TV Device', () => {
}
},
{
state: '50',
state: '50 %',
metadata: {
ga: {
value: 'tvVolume'
Expand Down

0 comments on commit 64d7cdb

Please sign in to comment.