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.
Files changed (2) hide show
  1. package/index.js +101 -64
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,76 +1,113 @@
1
- var Service, Characteristic;
2
- const axios = require('axios');
1
+ 'use strict';
3
2
 
4
- module.exports = function(homebridge) {
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
- function HomeAssistantGarageDoor(log, config) {
5
+ class HomeAssistantGarageDoor {
6
+ constructor(log, config, api) {
11
7
  this.log = log;
12
- this.name = config.name || "Garage Door";
13
- this.haUrl = config.haUrl || "http://192.168.68.239:8123";
8
+ this.name = config.name;
9
+ this.haUrl = config.haUrl;
14
10
  this.haToken = config.haToken;
15
- this.entityId = config.entityId || "switch.Puerta1";
16
-
17
- // Estado inicial SIEMPRE CERRADO
18
- this.currentState = Characteristic.CurrentDoorState.CLOSED;
19
-
20
- this.service = new Service.GarageDoorOpener(this.name);
21
- this.informationService = new Service.AccessoryInformation();
22
-
23
- this.informationService
24
- .setCharacteristic(Characteristic.Manufacturer, "Home Assistant")
25
- .setCharacteristic(Characteristic.Model, "Garage Door")
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.getCharacteristic(Characteristic.TargetDoorState)
36
- .setValue(Characteristic.CurrentDoorState.CLOSED);
23
+ this.service = new this.Service.GarageDoorOpener(this.name);
24
+ this.service.setCharacteristic(this.Characteristic.TargetDoorState, this.Characteristic.TargetDoorState.CLOSED);
37
25
 
38
- this.log("[%s] Initialized - HA: %s (%s)", this.name, this.haUrl, this.entityId);
39
- }
26
+ this.initHA();
27
+ }
40
28
 
41
- HomeAssistantGarageDoor.prototype = {
42
- getServices: function() {
43
- return [this.informationService, this.service];
44
- },
29
+ initHA() {
30
+ this.log(`[${this.name}] Initialized - HA: ${this.haUrl} (${this.entityId})`);
31
+ this.startPolling();
32
+ }
45
33
 
46
- setTargetState: function(targetState, callback) {
47
- this.log("[%s] Target state: %s", this.name, targetState === 0 ? "OPEN" : "CLOSED");
48
-
49
- const service = targetState === 0 ? "switch.turn_on" : "switch.turn_off";
50
-
51
- axios.post(`${this.haUrl}/api/services/switch/${service}`,
52
- { entity_id: this.entityId },
53
- {
54
- headers: {
55
- 'Authorization': `Bearer ${this.haToken}`,
56
- 'Content-Type': 'application/json'
57
- },
58
- timeout: 10000
59
- }
60
- )
61
- .then(() => {
62
- this.log("[%s] Command sent to HA: %s", this.name, service);
63
- callback(null);
64
- })
65
- .catch(err => {
66
- this.log("[%s] HA Error: %s", this.name, err.message);
67
- callback(err);
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
- getCurrentState: function(callback) {
72
- // SIEMPRE devuelve CERRADO (como tu plugin anterior)
73
- callback(null, this.currentState);
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-homeassistant-garagedoor",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Homebridge plugin to control Home Assistant switches as Garage Doors",
5
5
  "main": "index.js",
6
6
  "scripts": {