Skip to content

Commit

Permalink
fix: duration type handling
Browse files Browse the repository at this point in the history
Fixes #185
  • Loading branch information
robertsLando committed Jan 13, 2021
1 parent f8df1d6 commit 536fbbb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
33 changes: 13 additions & 20 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ function parseValue (zwaveNode, zwaveValue, zwaveValueMeta) {
zwaveValue.newValue !== undefined ? zwaveValue.newValue : prevValue
}

if (valueId.type === 'duration' && valueId.value === undefined) {
valueId.value = new Duration(0, 'seconds')
}

if (isCurrentValue(valueId)) {
valueId.isCurrentValue = true
const targetValue = findTargetValue(valueId, zwaveNode.getDefinedValueIDs())
Expand All @@ -909,8 +913,6 @@ function parseValue (zwaveNode, zwaveValue, zwaveValueMeta) {
}
}

parseDurations(valueId)

node.values[vID] = valueId

return valueId
Expand All @@ -934,8 +936,6 @@ function updateValue (zwaveNode, args) {
valueId.value = args.newValue
valueId.stateless = !!args.stateless

parseDurations(valueId)

this.emit('valueChanged', valueId, node, args.prevValue !== args.newValue)

const self = this
Expand Down Expand Up @@ -986,16 +986,6 @@ function parseNotification (parameters) {
}
}

function parseDurations (valueId) {
// support for duration valueIds
if (valueId.value instanceof Duration) {
valueId.type = 'number'
valueId.isDuration = true
valueId.unit = valueId.value.unit
valueId.value = valueId.value.value
}
}

/**
* Get the device id of a specific node
*
Expand Down Expand Up @@ -2171,10 +2161,11 @@ ZwaveClient.prototype.callApi = async function (apiName, ...args) {
*/
ZwaveClient.prototype.writeValue = async function (valueId, value) {
if (this.driverReady) {
logger.info(`Writing ${value} to ${getValueID(valueId)}`)
logger.log('info', `Writing %o to ${getValueID(valueId)}`, value)

let result = false

// coerce string to numbers when value type is number and received a string
if (
valueId.type === 'number' &&
typeof value === 'string' &&
Expand All @@ -2186,8 +2177,10 @@ ZwaveClient.prototype.writeValue = async function (valueId, value) {
try {
const zwaveNode = await this.getNode(valueId.nodeId)

const isDuration = value instanceof Duration

// handle multilevel switch 'start' and 'stop' commands
if (
if (!isDuration &&
valueId.commandClass === CommandClasses['Multilevel Switch'] &&
isNaN(value)
) {
Expand All @@ -2203,16 +2196,16 @@ ZwaveClient.prototype.writeValue = async function (valueId, value) {
result = await this.getNode(valueId.nodeId).setValue(valueId, value)
}
} catch (error) {
logger.error(
`Error while writing ${value} on ${getValueID(valueId)}: ${
logger.log('error',
`Error while writing %o on ${getValueID(valueId)}: ${
error.message
}`,
error
value
)
}
// https://zwave-js.github.io/node-zwave-js/#/api/node?id=setvalue
if (result === false) {
logger.error(`Unable to write ${value} on ${getValueID(valueId)}`)
logger.log('error', `Unable to write %o on ${getValueID(valueId)}`, value)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/ValueId.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
@click:append-outer="updateValue(value)"
></v-text-field>

<v-text-field
v-if="
value.type === 'duration'
"
:label="'[' + value.id + '] ' + value.label"
:type="value.type === 'number' ? 'number' : 'text'"
:append-outer-icon="!disable_send ? 'send' : null"
:suffix="value.value ? value.value.unit : value.unit"
:min="value.min != value.max ? value.min : null"
:step="1"
:max="value.min != value.max ? value.max : null"
:hint="value.description || ''"
v-model.number="value.newValue.value"
@click:append-outer="updateValue(value)"
></v-text-field>

<v-select
v-if="value.list"
:items="value.states"
Expand Down

0 comments on commit 536fbbb

Please sign in to comment.