Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update format item state to convert all numerical item types #599

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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