Skip to content

Commit

Permalink
improve logic to have auto reset
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenicholl committed Dec 11, 2024
1 parent 8fb21de commit f7d09e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[![verified-by-homebridge](https://badgen.net/badge/homebridge/verified/purple)](https://github.com/homebridge/homebridge/wiki/Verified-Plugins) ![npm-version](https://badgen.net/npm/v/homebridge-weatherflow-tempest?icon=npm&label) ![npm-downloads](https://badgen.net/npm/dt/homebridge-weatherflow-tempest?icon=npm&label) [![donate](https://badgen.net/badge/donate/paypal/yellow)](https://paypal.me/chasenicholl)

<table align="center" border="0">
<tr>
<td sytle="border: 0px;">
<img src="https://user-images.githubusercontent.com/3979615/78016493-9b89a800-7396-11ea-9442-414ad9ffcdf2.png" width="200" />
</td>
<td sytle="border: 0px;">
<img src="https://t9s9z3m3.rocketcdn.me/wp-content/uploads/2016/05/Tempest-powered-by-01.svg" width="250" />
</td>
</tr>
<table align="center">
<tr>
<td>
<img src="https://user-images.githubusercontent.com/3979615/78016493-9b89a800-7396-11ea-9442-414ad9ffcdf2.png" width="200" />
</td>
<td>
<img src="https://t9s9z3m3.rocketcdn.me/wp-content/uploads/2016/05/Tempest-powered-by-01.svg" width="250" />
</td>
</tr>
</table>

*New* in v4.0.0 Local API Support!
Expand Down Expand Up @@ -52,7 +52,6 @@ Local API is now supported which requires no authentication. If you choose to us
- `sensors[].motion_properties.trigger_value`: _(Required with Motion Sensor)_ At what point (value) to trigger motion detected on/off. Minimum 1.
- `sensors[].occupancy_properties.trigger_value`: _(Required with Occupancy Sensor)_ At what point (value) to trigger occupancy detected on/off. Minimum 0.
- `sensors[].contact_properties.trigger_distance`: _(Required with Contact Sensor)_ The minimum distance (in kilometers) at which the strike was detected to activate the contact sensor.
- `sensors[].contact_properties.trigger_time`: _(Required with Contact Sensor)_ The minimum time interval (in seconds) between the detected strike to activate the contact sensor.

`{1}` Replace with Sensor: temperature, humidity, light, fan

Expand Down Expand Up @@ -177,8 +176,7 @@ sensor_type `{2}` | value_key | metric units | std units | additional_properties
"name": "Lightening Detector",
"sensor_type": "Contact Sensor",
"contact_properties": {
"trigger_distance": 10,
"trigger_time": 120
"trigger_distance": 10
}
}
],
Expand Down Expand Up @@ -300,8 +298,7 @@ sensor_type `{2}` | value_key | metric units | std units | additional_properties
"name": "Lightening Detector",
"sensor_type": "Contact Sensor",
"contact_properties": {
"trigger_distance": 10,
"trigger_time": 120
"trigger_distance": 10
}
}
],
Expand Down
6 changes: 0 additions & 6 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,6 @@
"minimum": 0,
"default": 0,
"description": "The minimum distance (in kilometers) at which the strike was detected to activate the contact sensor."
},
"trigger_time": {
"type": "number",
"minimum": 0,
"default": 0,
"description": "The minimum time interval (in seconds) between the detected strike to activate the contact sensor."
}
}
}
Expand Down
31 changes: 21 additions & 10 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,13 @@ class BatterySensor {

class ContactSensor {
private service: Service;

private state: number;
constructor(
private readonly platform: WeatherFlowTempestPlatform,
private readonly accessory: PlatformAccessory,
) {

this.state = 0;
this.service = this.accessory.getService(this.platform.Service.ContactSensor) ||
this.accessory.addService(this.platform.Service.ContactSensor);

Expand All @@ -621,11 +622,20 @@ class ContactSensor {
// Set initial value
this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(0);

// Update value based on user defined global interval
const interval = (this.platform.config.interval as number || 10) * 1000;
// Update value based on a 1 second check interval
let tick = 0;
setInterval( () => {
tick++;
if (tick % 5 === 0) {
tick = 0;
// Reset Contact sensor every 5 seconds if CONTACT_NOT_DETECTED
if (this.state === 1) {
this.state = 0;
this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(0); // CONTACT_DETECTED
}
}
this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(this.getState());
}, interval);
}, 1000);

}

Expand All @@ -635,27 +645,28 @@ class ContactSensor {
const lightning_strike_last_epoch: number = this.platform.observation_data.lightning_strike_last_epoch;
const lightning_strike_last_distance: number = this.platform.observation_data.lightning_strike_last_distance;
const trigger_distance: number = this.accessory.context.device.contact_properties.trigger_distance;
const trigger_time: number = this.accessory.context.device.contact_properties.trigger_time;
const current_epoch_now = Math.floor(Date.now() / 1000);
if (lightning_strike_last_epoch > 0
&& lightning_strike_last_distance > 0
&& lightning_strike_last_distance <= trigger_distance
&& (current_epoch_now - lightning_strike_last_epoch) <= trigger_time) {
return 1; // trigger CONTACT_NOT_DETECTED.
&& (current_epoch_now - lightning_strike_last_epoch) <= 5) {
this.state = 1; // trigger CONTACT_NOT_DETECTED.
} else {
this.state = 0;
}
return 0;
} catch(exception) {
this.platform.log.error(exception as string);
return 0;
this.state = 0;
}
return this.state;

}

private handleCurrentStateGet(): number {

this.platform.log.debug('Triggered GET handleCurrentStateGet for Contact Sensor state');
return this.getState();

Check warning on line 669 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Trailing spaces not allowed

Check warning on line 669 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Trailing spaces not allowed

Check warning on line 669 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Trailing spaces not allowed
}

}
Expand Down

0 comments on commit f7d09e4

Please sign in to comment.