Skip to content

Commit

Permalink
Update format item state to convert all numerical item types
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton committed Aug 14, 2023
1 parent ed456d2 commit dd88029
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
24 changes: 8 additions & 16 deletions lambda/openhab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 2 additions & 9 deletions lambda/test/openhab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dd88029

Please sign in to comment.