@ronniepettersson/homebridge-dummy
Version:
Create Homebridge accessories to help with automation and control — scheduling, delays, sensors, commands, webhooks, and more
125 lines • 4.9 kB
JavaScript
import { DummyAccessory } from '../base.js';
import { strings } from '../../i18n/i18n.js';
import { DefaultPosition, isValidPosition, printableValues, WebhookCommand } from '../../model/enums.js';
import { Webhook } from '../../model/webhook.js';
import { storageGet_Deprecated, Storage } from '../../tools/storage.js';
export class PositionAccessory extends DummyAccessory {
position;
constructor(Service, Characteristic, accessory, config, log, isGrouped) {
super(Service, Characteristic, accessory, config, log, isGrouped);
if (!isValidPosition(config.defaultPosition)) {
this.log.warning(strings.position.badDefault, this.name, `'${config.defaultPosition}'`, printableValues(DefaultPosition));
}
this.position = this.defaultPosition;
if (this.hasPositionState) {
this.accessoryService.getCharacteristic(Characteristic.PositionState)
.onGet(this.getState.bind(this));
}
this.accessoryService.getCharacteristic(this.targetCharacteristic)
.onGet(this.getPosition.bind(this))
.onSet(this.setPosition.bind(this));
this.accessoryService.getCharacteristic(this.currentCharacteristic)
.onGet(this.getPosition.bind(this));
this.initializePosition();
}
get hasPositionState() {
return true;
}
get positionClosed() {
return 0;
}
get positionOpen() {
return 100;
}
get targetCharacteristic() {
return this.Characteristic.TargetPosition;
}
get currentCharacteristic() {
return this.Characteristic.CurrentPosition;
}
get webhookCommand() {
return WebhookCommand.TargetPosition;
}
webhooks() {
return [
new Webhook(this.identifier, this.webhookCommand, (value) => {
this.setPosition(value);
return this.logTemplateForCV(value).replace('%s', this.name);
}),
];
}
async initializePosition() {
if (!this.isStateful) {
this.accessoryService.updateCharacteristic(this.targetCharacteristic, this.position);
this.accessoryService.updateCharacteristic(this.currentCharacteristic, this.position);
return;
}
const position = await storageGet_Deprecated(this.defaultStateStorageKey);
if (position === undefined) {
return;
}
await this.setPosition(position);
}
get defaultPosition() {
return this.config.defaultPosition === DefaultPosition.OPEN ? this.positionOpen : this.positionClosed;
}
async getState() {
return this.Characteristic.PositionState.STOPPED;
}
async getPosition() {
return this.position;
}
async setPosition(value) {
const targetPosition = value === this.positionClosed ? this.positionClosed : this.positionOpen;
if (this.position !== targetPosition) {
this.logPosition(targetPosition);
if (this.config.commandOpen && targetPosition !== this.positionClosed) {
this.executeCommand(this.config.commandOpen);
}
else if (this.config.commandClose && targetPosition === this.positionClosed) {
this.executeCommand(this.config.commandClose);
}
}
this.position = targetPosition;
if (this.isStateful) {
await Storage.set(this.defaultStateStorageKey, this.position);
}
if (this.position !== this.defaultPosition) {
this.startTimer();
}
else {
this.cancelTimer();
}
this.accessoryService.updateCharacteristic(this.targetCharacteristic, this.position);
this.accessoryService.updateCharacteristic(this.currentCharacteristic, this.position);
if (this.sensor) {
if (!this.sensor.timerControlled) {
this.sensor.active = this.position !== this.defaultPosition;
}
else if (this.position !== this.defaultPosition) {
this.sensor.active = false;
}
}
}
async schedule() {
if (this.position === this.defaultPosition) {
const opposite = this.position === this.positionClosed ? this.positionOpen : this.positionClosed;
await this.setPosition(opposite);
}
}
async reset() {
if (this.position !== this.defaultPosition) {
await this.setPosition(this.defaultPosition);
if (this.sensor?.timerControlled) {
this.sensor.active = true;
}
}
}
logTemplateForCV(value) {
return value === this.positionClosed ? strings.position.closed : strings.position.open;
}
logPosition(value) {
this.logIfDesired(this.logTemplateForCV(value));
}
}
//# sourceMappingURL=position.js.map