mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-16 09:44:36 +00:00
* Default to generic device * Improved debugging granularity/increased categories * Add heartbeat monitoring for availability * Catch more failure cases with retry (still some missing I'd guess) * Switch to MathJS evaluate for simple math transforms * RGBTW: Switch base scale for all friendly topics to 100 (automatic conversion on backend) * RGBTW: Add color temperature support * RGBTW: Improve autodetection * RGBTW: Improved white/color mode handling (still work to do here)
46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
const TuyaDevice = require('./tuya-device')
|
|
const debug = require('debug')('tuya-mqtt:device')
|
|
const debugDiscovery = require('debug')('tuya-mqtt:discovery')
|
|
const utils = require('../lib/utils')
|
|
|
|
class SimpleSwitch extends TuyaDevice {
|
|
async init() {
|
|
// Set device specific variables
|
|
this.config.dpsPower = this.config.dpsPower ? this.config.dpsPower : 1
|
|
|
|
this.deviceData.mdl = 'Switch/Socket'
|
|
|
|
// Map generic DPS topics to device specific topic names
|
|
this.deviceTopics = {
|
|
state: {
|
|
key: this.config.dpsPower,
|
|
type: 'bool'
|
|
}
|
|
}
|
|
|
|
// Send home assistant discovery data and give it a second before sending state updates
|
|
this.initDiscovery()
|
|
await utils.sleep(1)
|
|
|
|
// Get initial states and start publishing topics
|
|
this.getStates()
|
|
}
|
|
|
|
initDiscovery() {
|
|
const configTopic = 'homeassistant/switch/'+this.config.id+'/config'
|
|
|
|
const discoveryData = {
|
|
name: (this.config.name) ? this.config.name : this.config.id,
|
|
state_topic: this.baseTopic+'state',
|
|
command_topic: this.baseTopic+'command',
|
|
unique_id: this.config.id,
|
|
device: this.deviceData
|
|
}
|
|
|
|
debugDiscovery('Home Assistant config topic: '+configTopic)
|
|
debugDiscovery(discoveryData)
|
|
this.publishMqtt(configTopic, JSON.stringify(discoveryData))
|
|
}
|
|
}
|
|
|
|
module.exports = SimpleSwitch |