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

feat: support start stop commands for multilevel switch #40

Merged
merged 18 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ app.use(
)
app.use(cookieParser())

app.use(history({
index: '/'
}))

app.get('/', renderIndex)

app.use('/', express.static(utils.joinPath(false, 'dist')))

app.use(cors())

app.use(history())

function hasProperty (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}
Expand Down
9 changes: 5 additions & 4 deletions hass/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ module.exports = {
type: 'climate',
object_id: 'climate',
discovery_payload: {
min_temp: 7,
max_temp: 30,
modes: ['off', 'auto', 'heat'],
min_temp: 5,
max_temp: 40,
temp_step: 0.5,
modes: [],
mode_state_topic: true,
mode_state_template: '{{ value_json.mode }}',
mode_state_template: '{{ value_json.value }}',
mode_command_topic: true,
current_temperature_topic: true,
current_temperature_template: '{{ value_json.value }}',
Expand Down
22 changes: 11 additions & 11 deletions hass/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const THERMOSTAT_2GIG = {
on: 1
},
setpoint_topic: {
Heat: '67-1-setpoint-1',
Cool: '67-1-setpoint-2'
1: '67-1-setpoint-1',
2: '67-1-setpoint-2'
},
default_setpoint: '67-1-setpoint-1',
discovery_payload: {
Expand Down Expand Up @@ -79,8 +79,8 @@ const STELLA_ZWAVE = {
],
mode_map: { off: 0, heat: 1, cool: 11 },
setpoint_topic: {
Comfort: '67-0-setpoint-1',
'Energy Saving': '67-0-setpoint-11'
1: '67-0-setpoint-1',
11: '67-0-setpoint-11'
},
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
Expand Down Expand Up @@ -109,8 +109,8 @@ const SPIRIT_ZWAVE_PLUS = {
],
mode_map: { off: 0, heat: 1, cool: 11 },
setpoint_topic: {
Heat: '67-0-setpoint-1',
'Heat Eco': '67-0-setpoint-11'
1: '67-0-setpoint-1',
11: '67-0-setpoint-11'
},
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
Expand All @@ -131,7 +131,7 @@ const DANFOSS_TRV_ZWAVE = {
type: 'climate',
object_id: 'thermostat',
values: ['49-0-Air temperature', '67-0-setpoint-1'],
setpoint_topic: { Heat: '67-0-setpoint-1' },
setpoint_topic: { 1: '67-0-setpoint-1' },
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
min_temp: 4,
Expand Down Expand Up @@ -171,7 +171,7 @@ module.exports = {
off: 0,
heat: 1
},
setpoint_topic: { Heat: '67-0-setpoint-1' },
setpoint_topic: { 1: '67-0-setpoint-1' },
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
min_temp: 5,
Expand All @@ -197,8 +197,8 @@ module.exports = {
],
mode_map: { off: 0, heat: 1, cool: 2 },
setpoint_topic: {
'Heat (Default)': '67-0-setpoint-1',
Cool: '67-0-setpoint-2'
1: '67-0-setpoint-1',
2: '67-0-setpoint-2'
},
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
Expand Down Expand Up @@ -226,7 +226,7 @@ module.exports = {
'67-0-setpoint-2'
],
mode_map: { off: 0, heat: 1, cool: 2 },
setpoint_topic: { Heat: '67-0-setpoint-1', Cool: '67-0-setpoint-2' },
setpoint_topic: { 1: '67-0-setpoint-1', 2: '67-0-setpoint-2' },
default_setpoint: '67-0-setpoint-1',
discovery_payload: {
min_temp: 15,
Expand Down
144 changes: 142 additions & 2 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ function onValueChanged (valueId, node, changed) {
this.zwave.sendToSocket(this.zwave.socketEvents.valueUpdated, valueId)
}

const isDiscovered = this.discovered[valueId.id]

// check if this value isn't discovered yet (values added after node is ready)
if (this.config.hassDiscovery && !isDiscovered) {
this.discoverValue(node, valueId.id.replace(valueId.nodeId + '-', ''))
}

const result = this.valueTopic(node, valueId, true)

if (!result) return
Expand Down Expand Up @@ -343,6 +350,9 @@ function onNodeStatus (node) {
}
}

// check if there are climates to discover
this.discoverClimates(node)

const nodeDevices = allDevices[node.deviceId] || []
nodeDevices.forEach(device => this.discoverDevice(node, device))

Expand Down Expand Up @@ -627,8 +637,11 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {

if (isDimmer) {
// TODO: should we use valueId.max instead of 99 ?
if (typeof payload === 'boolean') payload = payload ? 99 : 0
else payload = Math.round((payload / 255) * 99)
if (typeof payload === 'boolean') {
payload = payload ? 99 : 0
} else if (!isNaN(payload)) {
payload = Math.round((payload / 255) * 99)
}
}

// map modes coming from hass
Expand Down Expand Up @@ -1135,6 +1148,133 @@ Gateway.prototype.discoverDevice = function (node, hassDevice) {
}
}

/**
* Discover climate devices
*
* @param {NodeObj} node Internal node object
*/
Gateway.prototype.discoverClimates = function (node) {
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/config/config/deviceClasses.json#L177
// check if device it's a thermostat
if (!node.deviceClass || node.deviceClass.generic !== 0x08) {
return
}

try {
const nodeDevices = allDevices[node.deviceId] || []

// skip if there is already a climate device
if (nodeDevices.length > 0 && nodeDevices.find(d => d.type === 'climate')) {
return
}

// arrays of strings valueIds (without the node prefix)
const setpoints = []
const temperatures = []
const modes = []

for (const vId in node.values) {
const v = node.values[vId]
if (
v.commandClass === CommandClasses['Thermostat Setpoint'] &&
v.property === 'setpoint'
) {
setpoints.push(vId)
} else if (
v.commandClass === CommandClasses['Multilevel Sensor'] &&
v.property === 'Air temperature'
) {
temperatures.push(vId)
} else if (
v.commandClass === CommandClasses['Thermostat Mode'] &&
v.property === 'mode'
) {
modes.push(vId)
}
}

// take the first as valid (there shouldn't be more then one)
const modeId = modes[0]

// TODO: if the device supports multiple endpoints how could we identify the correct one to use?
const temperatureId = temperatures[0]

if (!temperatureId || !modeId) {
debug(
'Unable to discover climate device, there is no valid temperature/thermostat mode valueIds'
)
return
}

const mode = node.values[modeId]

// [0, 1, 2 ... ] (['off', 'heat', 'cold', ...])
const availableModes = mode.states.map(s => s.value)

// Zwave modes: https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/commandclass/ThermostatModeCC.ts#L54
// Available hass modes: [“auto”, “off”, “cool”, “heat”, “dry”, “fan_only”]
const hassModes = [
'off',
'heat',
'cool',
'auto',
undefined, // auxiliary
undefined, // resume
'fan_only',
undefined, // furnace
'dry',
undefined, // moist
'auto', // auto changeover
'heat', // energy heat
'cool', // energy cool
'off', // away
'heat', // full power
undefined // manufacturer specific
]

// generic configuration
const config = copy(hassCfg.thermostat)

config.mode_map = {}
config.setpoint_topic = {}
config.values = [modeId, temperatureId]

config.discovery_payload.mode_state_topic = modeId
config.discovery_payload.current_temperature_topic = temperatureId

// for all available modes update the modes map and setpoint topics
for (const m of availableModes) {
if (hassModes[m] === undefined) continue

config.mode_map[hassModes[m]] = availableModes[m]
config.discovery_payload.modes.push(hassModes[m])
if (m > 0) {
// find the mode setpoint, ignore off
const setId = setpoints.find(v => v.endsWith('-' + m))
const setpoint = node.values[setId]
if (setpoint) {
if (setpoint.propertyKey === 1) {
config.default_setpoint = setId
}

config.values.push(setId)
config.setpoint_topic[m] = setId
}
}
}

// add the new climate config to the nodeDevices so it will be
// discovered later when we call `discoverDevice`
nodeDevices.push(config)

debug('New climate device discovered: ' + JSON.stringify(config))

allDevices[node.deviceId] = nodeDevices
} catch (error) {
debug('Unable to discover climate device: ' + error.message)
}
}

/**
* Try to guess the best way to discover this valueId in Hass
*
Expand Down
Loading