Skip to content

Commit

Permalink
feat: support dynamically set min_temp and max_temp in climate #445 (#…
Browse files Browse the repository at this point in the history
…507)

* feat: support dinamically set min_temp and max_temp in climate #445

* fix: use max/min value instead of topic

* use function to retrieve value

* add test

Co-authored-by: Chris Nesbitt-Smith <chris@cns.me.uk>
  • Loading branch information
robertsLando and chrisns authored Jun 3, 2020
1 parent f3e6456 commit 460ba97
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,22 @@ function getMappedValuesTemplate (valueId, modeMap, integerList, defaultValue) {
return `{{ ${JSON.stringify(finalMap)}[value_json.value${valueFilter}] | default('${defaultValue}') }}`
}

/**
* Retrives the value of a property from the node valueId
*
* @param {Object} payload discovery payload
* @param {String} prop property name
* @param {Object} node node object
*/
function setDiscoveryValue (payload, prop, node) {
if (typeof payload[prop] === 'string') {
const valueId = node.values[payload[prop]]
if (valueId && valueId.value != null) {
payload[prop] = valueId.value
}
}
}

/**
* Parse the value of the payload received from mqtt
* based on the type of the payload and the gateway config
Expand Down Expand Up @@ -765,6 +781,10 @@ Gateway.prototype.discoverDevice = function (node, hassDevice) {
setId = hassDevice.default_setpoint
}

// set properties dinamically using their configuration values
setDiscoveryValue(payload, 'max_temp', node)
setDiscoveryValue(payload, 'min_temp', node)

const setpoint = node.values[setId]
payload.temperature_state_topic = this.mqtt.getTopic(this.valueTopic(node, setpoint))
payload.temperature_command_topic = payload.temperature_state_topic + '/set'
Expand Down
60 changes: 60 additions & 0 deletions test/lib/Gateway.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const chai = require('chai')
const rewire = require('rewire')
chai.use(require('sinon-chai'))
chai.should()

const mod = rewire('../../lib/Gateway')

describe('#Gateway', () => {
describe('#setDiscoveryValue()', () => {
let untouchedPayload
const func = mod.__get__('setDiscoveryValue')
let payload
const node = {
values: {
c: { value: 'a' },
d: { value: null },
e: false
}
}
beforeEach(() => {
payload = {
a: 1,
b: 'c',
c: 'd',
d: 'e'
}
untouchedPayload = JSON.parse(JSON.stringify(payload))
})

describe('payload prop not string', () => {
it('should not change payload', () => {
func(payload, 'a', node)
payload.should.deep.equal(untouchedPayload)
})
})
describe('no valueId', () => {
it('should not change payload', () => {
func(payload, 'd', node)
payload.should.deep.equal(untouchedPayload)
})
})
describe('no valueId.value', () => {
it('should not change payload', () => {
func(payload, 'c', node)
payload.should.deep.equal(untouchedPayload)
})
})
describe('happy path', () => {
it('should not change payload', () => {
func(payload, 'b', node)
payload.should.deep.equal({
a: 1,
b: 'a',
c: 'd',
d: 'e'
})
})
})
})
})

0 comments on commit 460ba97

Please sign in to comment.