mqtt: add temp, humidity, flood

This commit is contained in:
Koushik Dutta
2023-12-05 22:29:36 -08:00
parent 8b6c0c4f7b
commit e64ec98211
3 changed files with 38 additions and 7 deletions

View File

@@ -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",

View File

@@ -41,5 +41,5 @@
"@scrypted/common": "file:../../common",
"@types/nunjucks": "^3.2.0"
},
"version": "0.0.72"
"version": "0.0.74"
}

View File

@@ -349,11 +349,8 @@ interface AutoDiscoveryConfig {
const autoDiscoveryMap = new Map<string, AutoDiscoveryConfig>();
function createBinarySensorConfig(mqttId: string, device: MixinDeviceBase<any>, prop: ScryptedInterfaceProperty, topic: string) {
function getAutoDiscoveryDevice(device: MixinDeviceBase<any>, 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<any>,
}
}
function createBinarySensorConfig(mqttId: string, device: MixinDeviceBase<any>, 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<any>, topic: string, autoDiscoveryPrefix = 'homeassistant') {
for (const iface of device.interfaces) {
const found = autoDiscoveryMap.get(iface);