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

fix: improve hass discovery of climates #68

Merged
merged 5 commits into from
Dec 14, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,8 @@ You can specify your custom devices configuration inside `store/customDevices(.j
],
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
124 changes: 73 additions & 51 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,74 +1200,96 @@ Gateway.prototype.discoverClimates = function (node) {
}
}

// 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) {
if (!temperatureId) {
debug(
'Unable to discover climate device, there is no valid temperature/thermostat mode valueIds'
'Unable to discover climate device, there is no valid temperature valueId'
)
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.discovery_payload.current_temperature_topic = temperatureId

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

config.discovery_payload.mode_state_topic = modeId
config.discovery_payload.current_temperature_topic = temperatureId
// take the first as valid
const modeId = modes[0]

// for all available modes update the modes map and setpoint topics
for (const m of availableModes) {
if (hassModes[m] === undefined) continue
// some thermostats could support just one mode so haven't a thermostat mode CC
if (modeId) {
config.values.push(modeId)

const mode = node.values[modeId]

config.discovery_payload.mode_state_topic = modeId
config.discovery_payload.mode_command_topic = modeId + '/set'

// [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
const allowedModes = ['off', 'heat', 'cool', 'auto', '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
]

config.mode_map = {}
config.setpoint_topic = {}

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

let hM = hassModes[m]

// it could happen that mode_map already have defined a mode for this value, in this case
// map that mode to the first one available in the allowed hass modes
const i = 1 // skip 'off'
while (
config.discovery_payload.modes.includes(hM) &&
i < allowedModes.length
) {
hM = allowedModes[i]
}

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.mode_map[hM] = availableModes[m]
config.discovery_payload.modes.push(hM)
if (m > 0) {
// find the mode setpoint, ignore off
const setId = setpoints.find(v => v.endsWith('-' + m))
const setpoint = node.values[setId]
if (setpoint) {
config.values.push(setId)
config.setpoint_topic[m] = setId
}

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

// set the default setpoint to 'heat' or to the first setpoint available
config.default_setpoint =
config.setpoint_topic[1] ||
config.setpoint_topic[Object.keys(config.setpoint)[0]]
} else {
config.default_setpoint = setpoints[0]
}

// add the new climate config to the nodeDevices so it will be
Expand Down