This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·142 lines (125 loc) · 4.37 KB
/
index.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
const miio = require('miio')
let Service, Characteristic
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory("homebridge-mi-led-desk-lamp", "mi-led-desk-lamp", MiLedDesklamp)
}
class MiLedDesklamp {
constructor(log, config) {
// Setup configuration
this.log = log
this.name = config['name'] || 'Mi desk lamp'
if (!config['ip']) {
this.log('No IP address define for', this.name)
return
}
if (!config['token']) {
this.log('No token define for', this.name)
return
}
this.ip = config['ip']
this.token = config['token']
// Setup services
this.lamp = new Service.Lightbulb(this.name)
this.lamp.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this))
this.lamp.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this))
this.lamp.getCharacteristic(Characteristic.ColorTemperature)
.on('get', this.getColorTemperature.bind(this))
.on('set', this.setColorTemperature.bind(this))
this.listenLampState().catch(error => this.log.error(error))
}
async getLamp() {
if (this.lampDevice) return this.lampDevice
this.log('Connect to device')
try {
this.lampDevice = await miio.device({address: this.ip, token: this.token})
} catch (e) {
this.lampDevice = undefined
this.log.error('Device not connected', e)
}
return this.lampDevice
}
async listenLampState(){
const device = await this.getLamp()
device.on('powerChanged', isOn => this.lamp.getCharacteristic(Characteristic.On).updateValue(isOn))
device.on('colorChanged', color => this.lamp.getCharacteristic(Characteristic.ColorTemperature).updateValue(Math.round(1000000 / color.values[0])))
device.on('brightnessChanged', brightness => this.lamp.getCharacteristic(Characteristic.Brightness).updateValue(brightness))
}
async getState(callback) {
this.log('Get state...')
try {
const device = await this.getLamp()
const power = await device.power()
callback(null, power)
} catch (e) {
this.log.error('Error getting state', e)
callback(e)
}
}
async setState(state, callback) {
this.log('Set state to', state)
try {
const device = await this.getLamp()
await device.power(state)
callback(null)
} catch (e) {
this.log.error('Error setting state', e)
callback(e)
}
}
async getBrightness(callback) {
this.log('Get brightness...')
try {
const device = await this.getLamp()
const brightness = await device.brightness()
callback(null, brightness)
} catch (e) {
this.log.error('Error getting brightness', e)
callback(e)
}
}
async setBrightness(state, callback) {
this.log('Set brightness to', state)
try {
const device = await this.getLamp()
await device.brightness('' + state)
callback(null)
} catch (e) {
this.log.error('Error setting brightness', e)
callback(e)
}
}
async getColorTemperature(callback) {
this.log('Get color...')
try {
const device = await this.getLamp()
const color = await device.color()
const miredColor = Math.round(1000000 / color.values[0])
callback(null, miredColor)
} catch (e) {
this.log.error('Error getting brightness', e)
callback(e)
}
}
async setColorTemperature(miredValue, callback) {
this.log('Set color to', miredValue)
let kelvinValue = Math.round(1000000 / miredValue)
kelvinValue = Math.max(Math.min(kelvinValue, 6500), 2700);
try {
const device = await this.getLamp()
await device.call("set_ct_abx", [kelvinValue, 'smooth', 1000])
callback(null)
} catch (e) {
this.log.error('Error setting color', e)
callback(e)
}
}
getServices() {
return [this.lamp]
}
}