Skip to content

Commit

Permalink
Merge pull request #90 from justjam2013/add-statefulness-to-valve
Browse files Browse the repository at this point in the history
Added stateful property to valve
  • Loading branch information
justjam2013 authored Jan 10, 2025
2 parents cd3031e + b58bf33 commit a2cdadd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"description": "Accessory state survives Homebridge restart",
"type": "boolean",
"condition": {
"functionBody": "return ['switch', 'lock', 'garagedoor', 'windowcovering'].includes(model.devices[arrayIndices].accessoryType) && [undefined, false].includes(model.devices[arrayIndices].accessoryHasResetTimer);"
"functionBody": "return ['garagedoor', 'lock', 'switch', 'valve', 'windowcovering'].includes(model.devices[arrayIndices].accessoryType) && [undefined, false].includes(model.devices[arrayIndices].accessoryHasResetTimer);"
}
},
"accessoryHasResetTimer": {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "homebridge-virtual-accessories",
"displayName": "Virtual Accessories for Homebridge",
"type": "module",
"version": "1.2.7",
"version": "1.2.8-beta.1",
"description": "Virtual HomeKit accessories for Homebridge.",
"author": "justjam2013",
"license": "Apache-2.0",
Expand Down
17 changes: 17 additions & 0 deletions src/accessories/virtualAccessoryValve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class Valve extends Accessory {
private valveType: number;
private durationTimer: Timer;

private readonly stateStorageKey: string = 'ValveActive';

/**
* These are just used to create a working example
* You should implement your own code to track the state of your accessory
Expand Down Expand Up @@ -56,6 +58,16 @@ export class Valve extends Accessory {
break;
}

// If the accessory is stateful retrieve stored state, otherwise set to default state
if (this.accessoryConfiguration.accessoryIsStateful) {
const cachedState = this.loadState(this.storagePath, this.stateStorageKey) as number;

if (cachedState !== undefined) {
this.states.ValveActive = cachedState;
this.states.ValveInUse = (this.states.ValveActive === Valve.ACTIVE) ? Valve.IN_USE : Valve.NOT_IN_USE;
}
}

this.durationTimer = new Timer(this.accessoryConfiguration.valveDuration, Timer.Units.Seconds);

// set accessory information
Expand Down Expand Up @@ -142,6 +154,11 @@ export class Valve extends Accessory {

this.platform.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting In Use: ${this.getInUseName(this.states.ValveInUse)}`);

// Store device state if stateful
if (this.accessoryConfiguration.accessoryIsStateful) {
this.saveState(this.storagePath, this.stateStorageKey, this.states.ValveActive);
}

this.durationTimer.stop();
if (this.states.ValveActive === Valve.ACTIVE) {
this.durationTimer.start(
Expand Down

0 comments on commit a2cdadd

Please sign in to comment.