Skip to content

Commit

Permalink
Added support for setting the programme mode
Browse files Browse the repository at this point in the history
  • Loading branch information
njh committed Aug 27, 2023
1 parent c575ce3 commit a05037b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Commands:
set-up-down-limit <limit> Set a limit on the use of the up and down keys
set-sensors <mode> Set the sensor selection mode. See extended help for values.
set-programme-periods <periods> Set the number of programme periods
set-programme-mode <mode> Set the type of programme / schedule mode. See extended help for details.
set-units <units> Set the temperature units used by the thermostat
set-time Sync the system clock to the thermostat
set-auto-dst <on or off> Enable or disable automatic adjustment for Daylight Saving Time
Expand Down
21 changes: 21 additions & 0 deletions bin/hmmb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ program
})
})

program
.command('set-programme-mode')
.argument('<mode>', 'the programme mode number or name')
.description('Set the type of programme / schedule mode. See extended help for details.')
.addHelpText('after',
'\nProgramme modes:\n' +
' 0 5day_2day One schedule for weekdays, another for weekends (Default)\n' +
' 1 7day Different schedule for each day of the week\n' +
' 2 24hour Same schedule every day\n' +
' 3 none Non-Programmable - temperature control only\n'
)
.action((mode) => {
if (mode.match(/^\d$/) !== null) {
mode = parseInt(mode)
}
runClient(program, async (thermostat) => {
console.log('Setting programme mode to: ', mode)
return await thermostat.setProgrammeMode(mode)
})
})

program
.command('set-units')
.argument('<units>', 'the temperature units (C or F)')
Expand Down
25 changes: 25 additions & 0 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export default class Client {
readonly port: string
private readonly modbus: ModbusRTU

static readonly programmeModes = [
'5day_2day',
'7day',
'24hour',
'none'
]

constructor (port: string) {
this.thermostats = new Map<number, Thermostat>()
this.port = port
Expand Down Expand Up @@ -121,6 +128,24 @@ export default class Client {
return await this.modbus.writeRegister(27, value)
}

async setProgrammeMode (id: number, mode: number | string): Promise<any> {
let value: number
if (typeof mode === 'number') {
// FIXME: validate mode number?
value = mode
} else if (typeof mode === 'string') {
value = Client.programmeModes.indexOf(mode)
if (value === -1) {
throw new Error(`invalid programme mode: ${mode}`)
}
} else {
throw new Error(`unknown type passed to set programme mode: ${typeof mode}`)
}

this.modbus.setID(id)
return await this.modbus.writeRegister(28, value)
}

async setTargetTemperature (id: number, temperature: number): Promise<any> {
this.modbus.setID(id)
return await this.modbus.writeRegister(33, Math.round(temperature * 10))
Expand Down
4 changes: 4 additions & 0 deletions lib/thermostat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export default class Thermostat {
return await this.client.setProgrammePeriods(this.id, periods)
}

async setProgrammeMode (mode: number | string): Promise<any> {
return await this.client.setProgrammeMode(this.id, mode)
}

async setTargetTemperature (temperature: number): Promise<any> {
return await this.client.setTargetTemperature(this.id, temperature)
}
Expand Down

0 comments on commit a05037b

Please sign in to comment.