Skip to content

Commit

Permalink
Expose "acknowledge alarm" functionality over MQTT and HTTP
Browse files Browse the repository at this point in the history
Closes #114

A button is exposed to Home Assistant and a POST /alarm/acknowledge route is exposed via HTTP
  • Loading branch information
Jalle19 committed Aug 15, 2024
1 parent 0701b9c commit 83883ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
20 changes: 20 additions & 0 deletions app/homeassistant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,22 @@ export const configureMqttDiscovery = async (modbusClient, mqttClient) => {
'defrosting': createDeviceStateConfiguration(configurationBase, 'defrosting', 'Defrosting'),
}

// Button for acknowledging alarms
const buttonConfigurationMap = {
'acknowledgeAlarm': createButtonConfiguration(
configurationBase,
'acknowledgeAlarm',
'Acknowledge newest alarm'
),
}

// Final map that describes everything we want to be auto-discovered
const configurationMap = {
'sensor': sensorConfigurationMap,
'number': numberConfigurationMap,
'switch': switchConfigurationMap,
'binary_sensor': binarySensorConfigurationMap,
'button': buttonConfigurationMap,
}

// Publish configurations
Expand Down Expand Up @@ -476,3 +486,13 @@ const createDeviceStateConfiguration = (configurationBase, stateName, entityName
'entity_category': 'diagnostic',
}
}

const createButtonConfiguration = (configurationBase, buttonName, entityName) => {
return {
...configurationBase,
'unique_id': `eda-button-${buttonName}`,
'name': entityName,
'object_id': `eda_button_${buttonName}`,
'command_topic': `${TOPIC_PREFIX_ALARM}/acknowledge`,
}
}
14 changes: 14 additions & 0 deletions app/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getSettings,
setMode as modbusSetMode,
setSetting as modbusSetSetting,
acknowledgeAlarm as modbusAcknowledgeAlarm,
getDeviceState,
getNewestAlarm,
getAlarmSummary,
Expand Down Expand Up @@ -92,6 +93,16 @@ const setSetting = async (modbusClient, req, res) => {
}
}

const acknowledgeAlarm = async (modbusClient, req, res) => {
try {
logger.info('Acknowledging currently active alarm (if any)')

await modbusAcknowledgeAlarm(modbusClient)
} catch (e) {
handleError(e, res)
}
}

export const configureRoutes = (httpServer, modbusClient) => {
httpServer.get('/', root)
httpServer.get('/summary', (req, res) => {
Expand All @@ -106,6 +117,9 @@ export const configureRoutes = (httpServer, modbusClient) => {
httpServer.post('/setting/:setting/:value', (req, res) => {
return setSetting(modbusClient, req, res)
})
httpServer.post('/alarm/acknowledge', (req, res) => {
return acknowledgeAlarm(modbusClient, req, res)
})
}

const handleError = (e, res, statusCode = undefined) => {
Expand Down
4 changes: 4 additions & 0 deletions app/modbus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ export const getNewestAlarm = async (modbusClient) => {
}
}

export const acknowledgeAlarm = async (modbusClient) => {
await tryWriteHoldingRegister(modbusClient, 386, 1)
}

export const getDeviceState = async (modbusClient) => {
const result = await mutex.runExclusive(async () => tryReadHoldingRegisters(modbusClient, 44, 1))

Expand Down
17 changes: 14 additions & 3 deletions app/mqtt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
setMode,
getAlarmSummary,
getDeviceState,
acknowledgeAlarm,
} from './modbus.mjs'
import { createLogger } from './logger.mjs'

Expand Down Expand Up @@ -121,8 +122,12 @@ const publishTopics = async (mqttClient, topicMap, publishOptions = {}) => {
}

export const subscribeToChanges = async (modbusClient, mqttClient) => {
// Subscribe to settings and mode changes
const topicNames = [`${TOPIC_PREFIX_MODE}/+/set`, `${TOPIC_PREFIX_SETTINGS}/+/set`]
// Subscribe to writable topics
const topicNames = [
`${TOPIC_PREFIX_MODE}/+/set`,
`${TOPIC_PREFIX_SETTINGS}/+/set`,
`${TOPIC_PREFIX_ALARM}/acknowledge`,
]

for (const topicName of topicNames) {
logger.info(`Subscribing to topic(s) ${topicName}`)
Expand All @@ -136,21 +141,27 @@ export const handleMessage = async (modbusClient, mqttClient, topicName, rawPayl

const payload = parsePayload(rawPayload)

// Handle settings updates
if (topicName.startsWith(TOPIC_PREFIX_SETTINGS) && topicName.endsWith('/set')) {
// Handle settings updates
const settingName = topicName.substring(TOPIC_PREFIX_SETTINGS.length + 1, topicName.lastIndexOf('/'))

logger.info(`Updating setting ${settingName} to ${payload}`)

await setSetting(modbusClient, settingName, payload)
await publishSettings(modbusClient, mqttClient)
} else if (topicName.startsWith(TOPIC_PREFIX_MODE) && topicName.endsWith('/set')) {
// Handle mode changes
const mode = topicName.substring(TOPIC_PREFIX_MODE.length + 1, topicName.lastIndexOf('/'))

logger.info(`Updating mode ${mode} to ${payload}`)

await setMode(modbusClient, mode, payload)
await publishModeSummary(modbusClient, mqttClient)
} else if (topicName.startsWith(TOPIC_PREFIX_ALARM) && topicName.endsWith('/acknowledge')) {
// Acknowledge alarm
logger.info('Acknowledging currently active alarm (if any)')

await acknowledgeAlarm(modbusClient)
}
}

Expand Down

0 comments on commit 83883ce

Please sign in to comment.