homebridge-homeassistant-garagedoor 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +101 -64
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,76 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
const axios = require('axios');
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
Service = homebridge.hap.Service;
|
|
6
|
-
Characteristic = homebridge.hap.Characteristic;
|
|
7
|
-
homebridge.registerAccessory("homebridge-homeassistant-garagedoor", "HomeAssistantGarageDoor", HomeAssistantGarageDoor);
|
|
8
|
-
};
|
|
3
|
+
const axios = require('axios');
|
|
9
4
|
|
|
10
|
-
|
|
5
|
+
class HomeAssistantGarageDoor {
|
|
6
|
+
constructor(log, config, api) {
|
|
11
7
|
this.log = log;
|
|
12
|
-
this.name = config.name
|
|
13
|
-
this.haUrl = config.haUrl
|
|
8
|
+
this.name = config.name;
|
|
9
|
+
this.haUrl = config.haUrl;
|
|
14
10
|
this.haToken = config.haToken;
|
|
15
|
-
this.entityId = config.entityId
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
.setCharacteristic(Characteristic.SerialNumber, this.entityId);
|
|
27
|
-
|
|
28
|
-
this.service.getCharacteristic(Characteristic.TargetDoorState)
|
|
29
|
-
.on('set', this.setTargetState.bind(this));
|
|
30
|
-
|
|
31
|
-
this.service.getCharacteristic(Characteristic.CurrentDoorState)
|
|
32
|
-
.on('get', this.getCurrentState.bind(this))
|
|
33
|
-
.setValue(this.currentState);
|
|
11
|
+
this.entityId = config.entityId;
|
|
12
|
+
this.pollInterval = config.pollInterval || 30;
|
|
13
|
+
this.currentState = 'CLOSED';
|
|
14
|
+
this.targetState = 'CLOSED';
|
|
15
|
+
this.polling = null;
|
|
16
|
+
|
|
17
|
+
// ✅ FIX: API params para Service/Characteristic
|
|
18
|
+
this.Service = api.hap.Service;
|
|
19
|
+
this.Characteristic = api.hap.Characteristic;
|
|
20
|
+
|
|
21
|
+
this.log(`[${this.name}] Initializing HomeAssistantGarageDoor accessory...`);
|
|
34
22
|
|
|
35
|
-
this.service.
|
|
36
|
-
|
|
23
|
+
this.service = new this.Service.GarageDoorOpener(this.name);
|
|
24
|
+
this.service.setCharacteristic(this.Characteristic.TargetDoorState, this.Characteristic.TargetDoorState.CLOSED);
|
|
37
25
|
|
|
38
|
-
this.
|
|
39
|
-
}
|
|
26
|
+
this.initHA();
|
|
27
|
+
}
|
|
40
28
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
29
|
+
initHA() {
|
|
30
|
+
this.log(`[${this.name}] Initialized - HA: ${this.haUrl} (${this.entityId})`);
|
|
31
|
+
this.startPolling();
|
|
32
|
+
}
|
|
45
33
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
34
|
+
async sendHACommand(state) {
|
|
35
|
+
if (this.currentState === state) {
|
|
36
|
+
this.log(`[${this.name}] State matches target (${state}) → Skipping HA request`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const service = state === 'OPEN' ? 'turn_on' : 'turn_off';
|
|
41
|
+
try {
|
|
42
|
+
const response = await axios.post(
|
|
43
|
+
`${this.haUrl}/api/services/switch/${service}`,
|
|
44
|
+
{ entity_id: this.entityId },
|
|
45
|
+
{
|
|
46
|
+
headers: {
|
|
47
|
+
'Authorization': `Bearer ${this.haToken}`,
|
|
48
|
+
'Content-Type': 'application/json'
|
|
49
|
+
},
|
|
50
|
+
timeout: 5000
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
this.log(`[${this.name}] HA ${service} → ${response.status}: ${JSON.stringify(response.data)}`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
this.log(`[${this.name}] HA Error: ${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async pollHA() {
|
|
60
|
+
try {
|
|
61
|
+
const response = await axios.get(
|
|
62
|
+
`${this.haUrl}/api/states/${this.entityId}`,
|
|
63
|
+
{
|
|
64
|
+
headers: { 'Authorization': `Bearer ${this.haToken}` },
|
|
65
|
+
timeout: 5000
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const haState = response.data.state;
|
|
70
|
+
const doorState = haState === 'on' ? 'OPEN' : 'CLOSED';
|
|
71
|
+
|
|
72
|
+
if (this.currentState !== doorState) {
|
|
73
|
+
this.currentState = doorState;
|
|
74
|
+
this.service.setCharacteristic(this.Characteristic.CurrentDoorState,
|
|
75
|
+
doorState === 'OPEN' ?
|
|
76
|
+
this.Characteristic.CurrentDoorState.OPEN :
|
|
77
|
+
this.Characteristic.CurrentDoorState.CLOSED
|
|
78
|
+
);
|
|
79
|
+
this.log(`[${this.name}] Poll: ${doorState} (${haState})`);
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
this.log(`[${this.name}] Poll error: ${error.message}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
70
85
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
86
|
+
startPolling() {
|
|
87
|
+
if (this.pollInterval > 0) {
|
|
88
|
+
this.polling = setInterval(() => this.pollHA(), this.pollInterval * 1000);
|
|
89
|
+
this.log(`[${this.name}] Polling started (${this.pollInterval}s)`);
|
|
74
90
|
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
getServices() {
|
|
94
|
+
this.service.getCharacteristic(this.Characteristic.TargetDoorState)
|
|
95
|
+
.on('set', (value, callback) => {
|
|
96
|
+
const newState = value === this.Characteristic.TargetDoorState.OPEN ? 'OPEN' : 'CLOSED';
|
|
97
|
+
this.log(`[${this.name}] Target state: ${newState}`);
|
|
98
|
+
this.targetState = newState;
|
|
99
|
+
this.sendHACommand(newState);
|
|
100
|
+
callback();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
this.service.getCharacteristic(this.Characteristic.CurrentDoorState)
|
|
104
|
+
.updateValue(this.Characteristic.CurrentDoorState.CLOSED);
|
|
105
|
+
|
|
106
|
+
return [this.service];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports = (homebridge) => {
|
|
111
|
+
homebridge.registerAccessory("homebridge-homeassistant-garagedoor", "HomeAssistantGarageDoor", HomeAssistantGarageDoor);
|
|
75
112
|
};
|
|
76
113
|
|