-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathHomeAssistant.js
159 lines (147 loc) · 5.21 KB
/
HomeAssistant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require('fs')
const pinyin = require("node-pinyin")
const pk = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf-8'))
function object_id(name) {
let arr = pinyin(name, { style: 'normal' })
return arr.map(ele => ele[0]).join('_').replace(/ /g, "_")
}
const DiscoveryDevice = {}
module.exports = class HomeAssistant {
constructor(node, cfg, { device_info, retain }) {
this.retain = retain === true
this.device_info = device_info
node.config = cfg.config
this.node = node
const { name } = cfg
const entity_id = object_id(name)
const type = node.type.replace('ha-mqtt-', '')
const topic = `ha-mqtt/${type}/${entity_id}/`
this.config = {
name,
unique_id: topic.replace(/\//g, '_'),
discovery_topic: `homeassistant/${type}/${entity_id}/config`,
state_topic: `${topic}state`,
json_attr_t: `${topic}attr`,
command_topic: `${topic}set`,
position_topic: `${topic}position/state`,
set_position_topic: `${topic}position/set`,
availability_topic: `${topic}availability/state`,
power_command_topic: `${topic}power/set`,
pause_command_topic: `${topic}pause/set`,
dock_command_topic: `${topic}dock/set`,
start_mowing_command_topic: `${topic}start_mowing/set`,
effect_state_topic: `${topic}effect/state`,
effect_command_topic: `${topic}effect/set`,
brightness_state_topic: `${topic}brightness/state`,
brightness_command_topic: `${topic}brightness/set`,
current_temperature_topic: `${topic}current_temperature`,
current_humidity_topic: `${topic}current_humidity`,
target_humidity_state_topic: `${topic}target_humidity/state`,
target_humidity_command_topic: `${topic}target_humidity/set`,
temperature_state_topic: `${topic}temperature/state`,
temperature_command_topic: `${topic}temperature/set`,
mode_state_topic: `${topic}mode/state`,
mode_command_topic: `${topic}mode/set`,
fan_mode_state_topic: `${topic}fan_mode/state`,
fan_mode_command_topic: `${topic}fan_mode/set`,
swing_mode_state_topic: `${topic}swing_mode/state`,
swing_mode_command_topic: `${topic}swing_mode/set`,
oscillation_state_topic: `${topic}oscillation/state`,
oscillation_command_topic: `${topic}oscillation/set`,
percentage_state_topic: `${topic}percentage/state`,
percentage_command_topic: `${topic}percentage/set`,
preset_mode_state_topic: `${topic}preset_mode/state`,
preset_mode_command_topic: `${topic}preset_mode/set`,
tilt_state_topic: `${topic}tilt/state`,
tilt_status_topic: `${topic}tilt/status`,
tilt_command_topic: `${topic}tilt/set`,
battery_level_topic: `${topic}battery_level/state`,
charging_topic: `${topic}charging/state`,
cleaning_topic: `${topic}cleaning/state`,
docked_topic: `${topic}docked/state`,
error_topic: `${topic}error/state`,
fan_speed_topic: `${topic}fan_speed/state`,
set_fan_speed_topic: `${topic}set_fan_speed/set`,
send_command_topic: `${topic}send_command/set`,
}
}
static get version() {
return pk.version
}
static AutoDiscovery(nodes) {
for (const node_id in DiscoveryDevice) {
// delete empty nodes
if (nodes && nodes.getNode(node_id) == null) {
delete DiscoveryDevice[node_id]
continue
}
DiscoveryDevice[node_id]()
}
}
discovery(config, callback) {
const node_id = this.node.id
DiscoveryDevice[node_id] = () => {
if (this.node.config) {
config = Object.assign(config, JSON.parse(this.node.config))
}
this.publish_config(config)
callback()
}
this.subscribe('homeassistant/status', (payload) => {
if (payload === 'online') {
HomeAssistant.AutoDiscovery()
}
})
// publish config
DiscoveryDevice[node_id]()
}
publish_config(data) {
const { name, unique_id, discovery_topic, state_topic, json_attr_t } = this.config
const mergeConfig = Object.assign({
name,
unique_id,
state_topic,
json_attr_t,
device: this.device_info
}, data)
// Delete the property of NULL
Object.keys(mergeConfig).forEach(key => {
if (mergeConfig[key] === null) {
delete mergeConfig[key]
}
})
this.publish(discovery_topic, mergeConfig)
}
subscribe(topic, callback) {
this.node.server.subscribe(topic, { qos: 0 }, function (mtopic, mpayload, mpacket) {
callback(mpayload.toString())
})
}
send_payload(payload, i, len = 4) {
let arr = []
arr.length = len
arr[i - 1] = { payload }
this.node.send(arr)
}
publish(topic, payload, msg = "") {
const type = Object.prototype.toString.call(payload)
switch (type) {
case '[object Uint8Array]':
this.node.server.client.publish(topic, payload, { retain: false })
return;
case '[object Object]':
payload = JSON.stringify(payload)
break;
case '[object Number]':
payload = String(payload)
break;
}
this.node.server.client.publish(topic, payload, { retain: this.retain })
if (msg) {
this.node.status({ fill: "green", shape: "ring", text: `${msg}:${payload}` });
}
}
isEmpty(value) {
return value !== 0 && !value
}
}