From dd88029e52d8a534e03cc848fd736d93c59c6840 Mon Sep 17 00:00:00 2001 From: jsetton Date: Mon, 14 Aug 2023 19:37:17 -0400 Subject: [PATCH] Update format item state to convert all numerical item types Signed-off-by: jsetton --- docs/USAGE.md | 2 +- lambda/openhab/index.js | 24 ++++++++---------------- lambda/test/openhab.test.js | 11 ++--------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/docs/USAGE.md b/docs/USAGE.md index ce16a905..1d4b35b6 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -279,7 +279,7 @@ Switch Power "Power" (WaterHeater) {alexa="PowerSta ## Item State -Item states, reported back to Alexa, are formatted based on their [item state presentation](https://www.openhab.org/docs/configuration/items.html#state-presentation) definition if configured. This means you can control the precision of number values (e.g. `%.1f °C` will limit reported temperature value to one decimal point). +Item states, reported back to Alexa, for numerical item types are formatted based on their [item state presentation](https://www.openhab.org/docs/configuration/items.html#state-presentation) definition if configured. This means you can control the precision of number values (e.g. `%.1f °C` will limit reported temperature value to one decimal point). For items that don't have a state, these can be configured as not retrievable, automatically when the item [parameter `autoupdate`](https://www.openhab.org/docs/configuration/items.html#parameter-autoupdate) is set as `autoupdate="false"` or by using metadata parameter `retrievable="false"`. In that case, the skill will not report back the state of the given item to Alexa. It is important to note that this will affect the usability of some of the advanced features in the Alexa app that require state reporting. diff --git a/lambda/openhab/index.js b/lambda/openhab/index.js index a2b5260d..3af2d91e 100644 --- a/lambda/openhab/index.js +++ b/lambda/openhab/index.js @@ -243,25 +243,17 @@ export default class OpenHAB { * @return {String} */ static formatItemState(item) { - const format = item.stateDescription?.pattern?.match(/%(?:[.0]\d+)?[dfs]/); + const format = item.stateDescription?.pattern?.match(/%(?:[.0]\d+)?[df]/)?.[0] || '%f'; const state = item.state; const type = item.groupType || item.type; - if (format) { - try { - switch (type.split(':')[0]) { - case ItemType.DIMMER: - case ItemType.NUMBER: - case ItemType.ROLLERSHUTTER: - return sprintf(format[0], parseFloat(state)); - case ItemType.STRING: - return sprintf(format[0], state); - } - } catch { - // ignore formatting errors - } + switch (type.split(':')[0]) { + case ItemType.DIMMER: + case ItemType.NUMBER: + case ItemType.ROLLERSHUTTER: + return sprintf(format, parseFloat(state)); + default: + return state; } - - return state; } } diff --git a/lambda/test/openhab.test.js b/lambda/test/openhab.test.js index 07c0c8d1..7c7171f0 100644 --- a/lambda/test/openhab.test.js +++ b/lambda/test/openhab.test.js @@ -167,16 +167,9 @@ describe('OpenHAB Tests', () => { expect(nock.isDone()).to.be.true; }); - it('string state with pattern', async () => { + it('string state with no state description', async () => { // set environment - nock(baseURL) - .get('/rest/items/foo') - .reply(200, { - name: 'foo', - state: 'bar', - stateDescription: { pattern: '%s' }, - type: 'String' - }); + nock(baseURL).get('/rest/items/foo').reply(200, { name: 'foo', state: 'bar', type: 'String' }); // run test expect(await openhab.getItemState('foo')).to.equal('bar'); expect(nock.isDone()).to.be.true;