From e64ec982118b8a74dd85d17925af3cc3e2fc0fb6 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 5 Dec 2023 22:29:36 -0800 Subject: [PATCH] mqtt: add temp, humidity, flood --- plugins/mqtt/package-lock.json | 4 ++-- plugins/mqtt/package.json | 2 +- plugins/mqtt/src/autodiscovery.ts | 39 +++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/plugins/mqtt/package-lock.json b/plugins/mqtt/package-lock.json index bcefd3a7fc..aa68aba26c 100644 --- a/plugins/mqtt/package-lock.json +++ b/plugins/mqtt/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/mqtt", - "version": "0.0.72", + "version": "0.0.74", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/mqtt", - "version": "0.0.72", + "version": "0.0.74", "dependencies": { "@types/node": "^16.6.1", "aedes": "^0.46.1", diff --git a/plugins/mqtt/package.json b/plugins/mqtt/package.json index 823f37648e..579b0c749a 100644 --- a/plugins/mqtt/package.json +++ b/plugins/mqtt/package.json @@ -41,5 +41,5 @@ "@scrypted/common": "file:../../common", "@types/nunjucks": "^3.2.0" }, - "version": "0.0.72" + "version": "0.0.74" } diff --git a/plugins/mqtt/src/autodiscovery.ts b/plugins/mqtt/src/autodiscovery.ts index 66d1af40ca..517a545ec3 100644 --- a/plugins/mqtt/src/autodiscovery.ts +++ b/plugins/mqtt/src/autodiscovery.ts @@ -349,11 +349,8 @@ interface AutoDiscoveryConfig { const autoDiscoveryMap = new Map(); -function createBinarySensorConfig(mqttId: string, device: MixinDeviceBase, prop: ScryptedInterfaceProperty, topic: string) { +function getAutoDiscoveryDevice(device: MixinDeviceBase, mqttId: string) { return { - state_topic: `${topic}/${prop}`, - payload_on: 'true', - payload_off: 'false', dev: { name: device.name, // what the hell is this @@ -365,6 +362,15 @@ function createBinarySensorConfig(mqttId: string, device: MixinDeviceBase, } } +function createBinarySensorConfig(mqttId: string, device: MixinDeviceBase, prop: ScryptedInterfaceProperty, topic: string) { + return { + state_topic: `${topic}/${prop}`, + payload_on: 'true', + payload_off: 'false', + ...getAutoDiscoveryDevice(device, mqttId), + } +} + function addBinarySensor(iface: ScryptedInterface, prop: ScryptedInterfaceProperty) { autoDiscoveryMap.set(iface, { component: 'binary_sensor', @@ -377,8 +383,33 @@ function addBinarySensor(iface: ScryptedInterface, prop: ScryptedInterfaceProper addBinarySensor(ScryptedInterface.MotionSensor, ScryptedInterfaceProperty.motionDetected); addBinarySensor(ScryptedInterface.BinarySensor, ScryptedInterfaceProperty.binaryState); addBinarySensor(ScryptedInterface.OccupancySensor, ScryptedInterfaceProperty.occupied); +addBinarySensor(ScryptedInterface.FloodSensor, ScryptedInterfaceProperty.flooded); addBinarySensor(ScryptedInterface.AudioSensor, ScryptedInterfaceProperty.audioDetected); +autoDiscoveryMap.set(ScryptedInterface.Thermometer, { + component: 'sensor', + create(mqttId, device, topic) { + return { + state_topic: `${topic}/${ScryptedInterfaceProperty.temperature}`, + value_template: '{{ value_json }}', + unit_of_measurement: 'C', + ...getAutoDiscoveryDevice(device, mqttId), + } + } +}); + +autoDiscoveryMap.set(ScryptedInterface.HumiditySensor, { + component: 'sensor', + create(mqttId, device, topic) { + return { + state_topic: `${topic}/${ScryptedInterfaceProperty.humidity}`, + value_template: '{{ value_json }}', + unit_of_measurement: '%', + ...getAutoDiscoveryDevice(device, mqttId), + } + } +}); + export function publishAutoDiscovery(mqttId: string, client: Client, device: MixinDeviceBase, topic: string, autoDiscoveryPrefix = 'homeassistant') { for (const iface of device.interfaces) { const found = autoDiscoveryMap.get(iface);