homebridge-homeassistant-garagedoor 1.1.8 → 1.1.9
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 +72 -107
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,120 +1,85 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const request = require('request');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
module.exports = (homebridge) => {
|
|
6
|
+
const { Service, Characteristic } = homebridge.hap;
|
|
7
|
+
|
|
8
|
+
class HomeAssistantGarageDoor {
|
|
9
|
+
constructor(log, config) {
|
|
10
|
+
this.log = log;
|
|
11
|
+
this.name = config.name;
|
|
12
|
+
this.haUrl = config.haUrl;
|
|
13
|
+
this.entityId = config.entityId;
|
|
14
|
+
this.pollInterval = config.pollInterval || 30;
|
|
15
|
+
|
|
16
|
+
this.currentState = Characteristic.CurrentDoorState.CLOSED;
|
|
17
|
+
this.targetState = Characteristic.TargetDoorState.CLOSED;
|
|
18
|
+
|
|
19
|
+
this.service = new Service.GarageDoorOpener(this.name);
|
|
20
|
+
this.service.getCharacteristic(Characteristic.TargetDoorState)
|
|
21
|
+
.on('set', this.setTargetState.bind(this));
|
|
22
|
+
|
|
23
|
+
// SIEMPRE FUERZA CLOSED al inicio
|
|
24
|
+
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
|
|
25
|
+
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
|
|
26
|
+
|
|
27
|
+
this.log(`[${this.name}] Initialized - HA: ${this.haUrl} (${this.entityId})`);
|
|
28
|
+
this.startPolling();
|
|
29
|
+
}
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
setTargetState(newState, callback) {
|
|
32
|
+
this.log(`[${this.name}] Target state: ${newState === Characteristic.TargetDoorState.OPEN ? 'OPEN' : 'CLOSED'}`);
|
|
33
|
+
|
|
34
|
+
if (newState === Characteristic.TargetDoorState.OPEN) {
|
|
35
|
+
this.sendHACommand('turn_on');
|
|
36
|
+
|
|
37
|
+
// ✅ AUTO-CLOSED INMEDIATO (1.5s)
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
this.log(`[${this.name}] ✅ AUTO-CLOSED después OPEN (1.5s)`);
|
|
40
|
+
this.currentState = Characteristic.CurrentDoorState.CLOSED;
|
|
41
|
+
this.targetState = Characteristic.TargetDoorState.CLOSED;
|
|
42
|
+
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
|
|
43
|
+
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
|
|
44
|
+
}, 1500);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ✅ SIEMPRE fuerza CLOSED en TargetState
|
|
48
|
+
this.log(`[${this.name}] ✅ TargetState → Fuerza CurrentState CLOSED`);
|
|
49
|
+
this.currentState = Characteristic.CurrentDoorState.CLOSED;
|
|
50
|
+
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
|
|
51
|
+
|
|
52
|
+
callback();
|
|
38
53
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
},
|
|
50
|
-
timeout: 5000
|
|
54
|
+
|
|
55
|
+
async sendHACommand(command) {
|
|
56
|
+
const url = `${this.haUrl}/api/services/switch/${command}`;
|
|
57
|
+
request.post({
|
|
58
|
+
url,
|
|
59
|
+
headers: { 'Authorization': `Bearer ${process.env.HA_TOKEN}`, 'Content-Type': 'application/json' },
|
|
60
|
+
body: JSON.stringify({ entity_id: this.entityId })
|
|
61
|
+
}, (err, res) => {
|
|
62
|
+
if (res && res.statusCode === 200) {
|
|
63
|
+
this.log(`[${this.name}] HA ${command} → 200: []`);
|
|
51
64
|
}
|
|
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}`);
|
|
65
|
+
});
|
|
56
66
|
}
|
|
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;
|
|
67
|
+
|
|
68
|
+
async pollHA() {
|
|
70
69
|
this.log(`[${this.name}] Poll: ALWAYS CLOSED (forced)`);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
|
|
75
|
-
if (this.currentState !== doorState) {
|
|
76
|
-
this.currentState = doorState;
|
|
77
|
-
this.service.setCharacteristic(this.Characteristic.CurrentDoorState,
|
|
78
|
-
doorState === 'OPEN' ?
|
|
79
|
-
this.Characteristic.CurrentDoorState.OPEN :
|
|
80
|
-
this.Characteristic.CurrentDoorState.CLOSED
|
|
81
|
-
);
|
|
82
|
-
this.log(`[${this.name}] Poll: ${doorState} (${haState})`);
|
|
83
|
-
}
|
|
84
|
-
} catch (error) {
|
|
85
|
-
this.log(`[${this.name}] Poll error: ${error.message}`);
|
|
70
|
+
this.currentState = Characteristic.CurrentDoorState.CLOSED;
|
|
71
|
+
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
|
|
86
72
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (this.pollInterval > 0) {
|
|
91
|
-
this.polling = setInterval(() => this.pollHA(), this.pollInterval * 1000);
|
|
73
|
+
|
|
74
|
+
startPolling() {
|
|
75
|
+
setInterval(() => this.pollHA(), this.pollInterval * 1000);
|
|
92
76
|
this.log(`[${this.name}] Polling started (${this.pollInterval}s)`);
|
|
93
77
|
}
|
|
78
|
+
|
|
79
|
+
getServices() {
|
|
80
|
+
return [this.service];
|
|
81
|
+
}
|
|
94
82
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
this.service.getCharacteristic(this.Characteristic.TargetDoorState)
|
|
98
|
-
.on('set', (value, callback) => {
|
|
99
|
-
const newState = value === this.Characteristic.TargetDoorState.OPEN ? 'OPEN' : 'CLOSED';
|
|
100
|
-
this.log(`[${this.name}] Target state: ${newState}`);
|
|
101
|
-
this.targetState = newState;
|
|
102
|
-
this.sendHACommand(newState);
|
|
103
|
-
this.service.setCharacteristic(this.Characteristic.CurrentDoorState,
|
|
104
|
-
newState === 'OPEN' ?
|
|
105
|
-
this.Characteristic.CurrentDoorState.OPEN :
|
|
106
|
-
this.Characteristic.CurrentDoorState.CLOSED);
|
|
107
|
-
callback();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
this.service.getCharacteristic(this.Characteristic.CurrentDoorState)
|
|
111
|
-
.updateValue(this.Characteristic.CurrentDoorState.CLOSED);
|
|
112
|
-
|
|
113
|
-
return [this.service];
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
module.exports = (homebridge) => {
|
|
118
|
-
homebridge.registerAccessory("homebridge-homeassistant-garagedoor", "HomeAssistantGarageDoor", HomeAssistantGarageDoor);
|
|
83
|
+
|
|
84
|
+
homebridge.registerAccessory('homebridge-homeassistant-garagedoor', 'HomeAssistantGarageDoor', HomeAssistantGarageDoor);
|
|
119
85
|
};
|
|
120
|
-
|
package/package.json
CHANGED