Skip to content
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
6 changes: 3 additions & 3 deletions app/modbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,22 @@ const parseSettingValue = (settingConfig: HoldingRegisterSettingConfiguration, v
export const setSetting = async (modbusClient: ModbusRTU, setting: string, value: string | boolean) => {
const settingConfig = AVAILABLE_SETTINGS[setting]
if (settingConfig === undefined) {
throw new Error('Unknown setting')
throw new Error(`Unknown setting "${setting}"`)
}

switch (settingConfig.registerType) {
case 'holding': {
// Holding registers expect numeric (string) values
if (typeof value !== 'string') {
throw new TypeError(`Setting '${setting}' expects a numeric value, got ${typeof value}`)
throw new TypeError(`Setting "${setting}" expects a numeric value, got ${typeof value}`)
}
const numericValue = parseSettingValue(settingConfig, value)
return setNumericSetting(modbusClient, settingConfig, numericValue)
}
case 'coil': {
// Coils expect boolean values
if (typeof value !== 'boolean') {
throw new TypeError(`Setting '${setting}' expects a boolean value, got ${typeof value}`)
throw new TypeError(`Setting "${setting}" expects a boolean value, got ${typeof value}`)
}
return setBooleanSetting(modbusClient, settingConfig, value)
}
Expand Down
8 changes: 5 additions & 3 deletions tests/modbus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('setSetting', () => {

test('should reject boolean values for numeric settings', async () => {
await expect(setSetting(mockClient, 'temperatureTarget', true)).rejects.toThrow(
"Setting 'temperatureTarget' expects a numeric value, got boolean"
'Setting "temperatureTarget" expects a numeric value, got boolean'
)
})

Expand Down Expand Up @@ -116,14 +116,16 @@ describe('setSetting', () => {

test('should reject string values for coil settings', async () => {
await expect(setSetting(mockClient, 'coolingAllowed', '1')).rejects.toThrow(
"Setting 'coolingAllowed' expects a boolean value, got string"
'Setting "coolingAllowed" expects a boolean value, got string'
)
})
})

describe('unknown settings', () => {
test('should reject unknown setting names', async () => {
await expect(setSetting(mockClient, 'nonExistentSetting', '123')).rejects.toThrow('Unknown setting')
await expect(setSetting(mockClient, 'nonExistentSetting', '123')).rejects.toThrow(
'Unknown setting "nonExistentSetting"'
)
})
})
})
Loading