Skip to content

Commit

Permalink
mqtt: add temp, humidity, flood
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 6, 2023
1 parent 8b6c0c4 commit e64ec98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions plugins/mqtt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/mqtt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
"@scrypted/common": "file:../../common",
"@types/nunjucks": "^3.2.0"
},
"version": "0.0.72"
"version": "0.0.74"
}
39 changes: 35 additions & 4 deletions plugins/mqtt/src/autodiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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);
Expand Down

0 comments on commit e64ec98

Please sign in to comment.