diff --git a/functions/devices/charger.js b/functions/devices/charger.js index dd60a751..c2f00111 100644 --- a/functions/devices/charger.js +++ b/functions/devices/charger.js @@ -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) { @@ -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; diff --git a/functions/devices/climatesensor.js b/functions/devices/climatesensor.js index 29aa0aed..c6cf7c64 100644 --- a/functions/devices/climatesensor.js +++ b/functions/devices/climatesensor.js @@ -56,7 +56,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; } diff --git a/functions/devices/dimmablelight.js b/functions/devices/dimmablelight.js index 6ef330e5..105bbb75 100644 --- a/functions/devices/dimmablelight.js +++ b/functions/devices/dimmablelight.js @@ -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 diff --git a/functions/devices/fan.js b/functions/devices/fan.js index 9afbf5d8..86f531e0 100644 --- a/functions/devices/fan.js +++ b/functions/devices/fan.js @@ -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; } diff --git a/functions/devices/humiditysensor.js b/functions/devices/humiditysensor.js index 70ffe318..017229c9 100644 --- a/functions/devices/humiditysensor.js +++ b/functions/devices/humiditysensor.js @@ -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 diff --git a/functions/devices/openclosedevice.js b/functions/devices/openclosedevice.js index c7f3c5cd..a92c64be 100644 --- a/functions/devices/openclosedevice.js +++ b/functions/devices/openclosedevice.js @@ -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; } diff --git a/functions/devices/sensor.js b/functions/devices/sensor.js index 79f52363..c0876eaf 100644 --- a/functions/devices/sensor.js +++ b/functions/devices/sensor.js @@ -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) { @@ -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; } } diff --git a/functions/devices/speaker.js b/functions/devices/speaker.js index 1688b6dd..b0b587ff 100644 --- a/functions/devices/speaker.js +++ b/functions/devices/speaker.js @@ -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 }; } } diff --git a/functions/devices/specialcolorlight.js b/functions/devices/specialcolorlight.js index a46348ee..20382e98 100644 --- a/functions/devices/specialcolorlight.js +++ b/functions/devices/specialcolorlight.js @@ -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; } @@ -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; } diff --git a/functions/devices/tv.js b/functions/devices/tv.js index 185164a3..71a9441d 100644 --- a/functions/devices/tv.js +++ b/functions/devices/tv.js @@ -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; diff --git a/tests/devices/charger.test.js b/tests/devices/charger.test.js index 838441e3..4d8bd289 100644 --- a/tests/devices/charger.test.js +++ b/tests/devices/charger.test.js @@ -315,7 +315,7 @@ describe('Charger Device', () => { }, { name: 'CapacityRemaining', - state: '4000.123', + state: '4000.123 wh', metadata: { ga: { value: 'chargerCapacityRemaining' diff --git a/tests/devices/climatesensor.test.js b/tests/devices/climatesensor.test.js index f740bd2b..6f173e05 100644 --- a/tests/devices/climatesensor.test.js +++ b/tests/devices/climatesensor.test.js @@ -135,7 +135,7 @@ describe('ClimateSensor Device', () => { }, { name: 'Humidity', - state: '59.7', + state: '59.7 %', type: 'Number', metadata: { ga: { @@ -182,7 +182,7 @@ describe('ClimateSensor Device', () => { members: [ { name: 'Humidity', - state: '30.3', + state: '30.3 %', type: 'Number', metadata: { ga: { diff --git a/tests/devices/dimmablelight.test.js b/tests/devices/dimmablelight.test.js index bbcf2a7f..5713ee56 100644 --- a/tests/devices/dimmablelight.test.js +++ b/tests/devices/dimmablelight.test.js @@ -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 }); diff --git a/tests/devices/fan.test.js b/tests/devices/fan.test.js index a6048f18..7b0f28dd 100644 --- a/tests/devices/fan.test.js +++ b/tests/devices/fan.test.js @@ -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: { diff --git a/tests/devices/humiditysensor.test.js b/tests/devices/humiditysensor.test.js index a1c53388..3303d9d6 100644 --- a/tests/devices/humiditysensor.test.js +++ b/tests/devices/humiditysensor.test.js @@ -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 }); diff --git a/tests/devices/openclosedevice.test.js b/tests/devices/openclosedevice.test.js index 322b8143..ae5dbe43 100644 --- a/tests/devices/openclosedevice.test.js +++ b/tests/devices/openclosedevice.test.js @@ -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 diff --git a/tests/devices/sensor.test.js b/tests/devices/sensor.test.js index a978e567..e98cd83e 100644 --- a/tests/devices/sensor.test.js +++ b/tests/devices/sensor.test.js @@ -96,7 +96,7 @@ describe('Sensor Device', () => { } } }, - state: '10' + state: '10 ppm' }; expect(Device.getState(item)).toStrictEqual({ currentSensorStateData: [ @@ -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: { diff --git a/tests/devices/speaker.test.js b/tests/devices/speaker.test.js index 3d285205..6d6807d7 100644 --- a/tests/devices/speaker.test.js +++ b/tests/devices/speaker.test.js @@ -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({ diff --git a/tests/devices/specialcolorlight.test.js b/tests/devices/specialcolorlight.test.js index 2f167d81..e59afa16 100644 --- a/tests/devices/specialcolorlight.test.js +++ b/tests/devices/specialcolorlight.test.js @@ -425,7 +425,7 @@ describe('SpecialColorLight Device', () => { }, members: [ { - state: '0', + state: '0 %', metadata: { ga: { value: 'lightBrightness' @@ -433,7 +433,7 @@ describe('SpecialColorLight Device', () => { } }, { - state: '80', + state: '80 K', metadata: { ga: { value: 'lightColorTemperature' diff --git a/tests/devices/tv.test.js b/tests/devices/tv.test.js index d10ec901..4c4c75ea 100644 --- a/tests/devices/tv.test.js +++ b/tests/devices/tv.test.js @@ -500,7 +500,7 @@ describe('TV Device', () => { } }, { - state: '50', + state: '50 %', metadata: { ga: { value: 'tvVolume'