Skip to content

Commit

Permalink
MQTT credentials optional, add will message and availablity
Browse files Browse the repository at this point in the history
  • Loading branch information
matijse committed Jan 28, 2021
1 parent dcc7813 commit 1ebf5a2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 1 addition & 3 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class Config {
if (
typeof this.eufyUsername === "undefined" ||
typeof this.eufyPassword === "undefined" ||
typeof this.mqttUrl === "undefined" ||
typeof this.mqttUsername === "undefined" ||
typeof this.mqttPassword === "undefined"
typeof this.mqttUrl === "undefined"
) {
winston.error('Missing configuration, please check config.yml')
throw new Error('Missing configuration, please check config.yml')
Expand Down
28 changes: 23 additions & 5 deletions mqtt/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ class MqttClient {
onMqttMessage = null

async connect() {
this.client = await MQTT.connectAsync(config.mqttUrl, {
username: config.mqttUsername,
password: config.mqttPassword,
let options = {
keepalive: 60,
reconnectPeriod: 1000
})
reconnectPeriod: 1000,
will: {
topic: HaDiscovery.availabilityTopic,
payload: 'offline',
retain: false
}
}

if (config.mqttUsername) {
options.username = config.mqttUsername
}
if (config.mqttPassword) {
options.password = config.mqttPassword
}

this.client = await MQTT.connectAsync(config.mqttUrl, options)

this.client.on('error', error => {
winston.error(`MQTT error`, { error })
})

this.client.on('reconnect', () => {
winston.info(`MQTT reconnect`)
this.sendAvailable()
})

this.client.on('close', () => {
Expand Down Expand Up @@ -58,6 +71,11 @@ class MqttClient {
}
// Give Home Assistant some time to setup all sensors
await sleep(5 * 1000)
await this.sendAvailable()
}

async sendAvailable () {
await this.client.publish(HaDiscovery.availabilityTopic, 'online')
}

async processPushNotification (notification) {
Expand Down
6 changes: 6 additions & 0 deletions mqtt/ha-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { DeviceCapabilities } = require('../enums/device_type')

class HaDiscovery {

availabilityTopic = `homeassistant/eufy/available`

discoveryConfigs (device) {
let configs = []
const deviceName = device.name
Expand Down Expand Up @@ -116,6 +118,7 @@ class HaDiscovery {
message: JSON.stringify({
name: sensorName,
device_class: sensorDeviceClass,
availability_topic: this.availabilityTopic,
state_topic: `${sensorBaseTopic}/state`,
json_attributes_topic: `${sensorBaseTopic}/attributes`,
payload_on: sensorPayloadOn,
Expand All @@ -134,6 +137,7 @@ class HaDiscovery {
topic: `${sensorBaseTopic}/config`,
message: JSON.stringify({
name: sensorName,
availability_topic: this.availabilityTopic,
topic: `${sensorBaseTopic}`,
unique_id: sensorId,
device: {
Expand All @@ -150,6 +154,7 @@ class HaDiscovery {
message: JSON.stringify({
name: sensorName,
device_class: sensorDeviceClass,
availability_topic: this.availabilityTopic,
state_topic: `${sensorBaseTopic}/state`,
unit_of_measurement: '%',
unique_id: sensorId,
Expand All @@ -168,6 +173,7 @@ class HaDiscovery {
message: JSON.stringify({
name: sensorName,
device_class: sensorDeviceClass,
availability_topic: this.availabilityTopic,
state_topic: `${sensorBaseTopic}/state`,
json_attributes_topic: `${sensorBaseTopic}/attributes`,
payload_on: sensorPayloadOn,
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ are automatically discovered in Home Assistant.
| Indoor Cam 2K (T8400) | :heavy_check_mark: | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Indoor Cam Pan & Tilt (T8410) | :heavy_check_mark: | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Motion Sensor (T8910) | :heavy_check_mark: | :x: | :x: | :x: | :x: | :x: | :x: | :heavy_check_mark: |
| Eufy video doorbell 1080P (battery) (T8220) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :heavy_check_mark: |
| Eufy video doorbell 1080P (powered) (T8221 / T8222) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :x: |
| Eufy video doorbell 1080P (battery) (T8220 / T8222) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :heavy_check_mark: |
| Eufy video doorbell 1080P (powered) (T8221) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :x: |
| Eufy video doorbell 2K (battery) (T8210) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :heavy_check_mark: |
| Eufy video doorbell 2K (powered) (T8200 / T8202) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :heavy_check_mark: | :x: |

Expand Down

0 comments on commit 1ebf5a2

Please sign in to comment.