homebridge-homeassistant-garagedoor 1.1.7 → 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.
Files changed (2) hide show
  1. package/index.js +72 -107
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,120 +1,85 @@
1
1
  'use strict';
2
2
 
3
- const axios = require('axios');
3
+ const request = require('request');
4
4
 
5
- class HomeAssistantGarageDoor {
6
- constructor(log, config, api) {
7
- this.log = log;
8
- this.name = config.name;
9
- this.haUrl = config.haUrl;
10
- this.haToken = config.haToken;
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...`);
22
-
23
- this.service = new this.Service.GarageDoorOpener(this.name);
24
- this.service.setCharacteristic(this.Characteristic.TargetDoorState, this.Characteristic.TargetDoorState.CLOSED);
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
- this.initHA();
27
- }
28
-
29
- initHA() {
30
- this.log(`[${this.name}] Initialized - HA: ${this.haUrl} (${this.entityId})`);
31
- this.startPolling();
32
- }
33
-
34
- async sendHACommand(state) {
35
- if (this.currentState === state) {
36
- this.log(`[${this.name}] State matches target (${state}) → Skipping HA request`);
37
- return;
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
- 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
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
- this.currentState = 'CLOSED';
72
- this.service.setCharacteristic(this.Characteristic.CurrentDoorState, this.Characteristic.CurrentDoorState.CLOSED);
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
- startPolling() {
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
- getServices() {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-homeassistant-garagedoor",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "displayName": "HA Garage Door",
5
5
  "description": "Homebridge plugin to control Home Assistant switches as Garage Doors",
6
6
  "main": "index.js",