grover / homebridge-calendar

A calendar plugin for homebridge (https://github.com/nfarina/homebridge), which allows flexible scheduling of triggers using any iCal calendar.
MIT License
73 stars 13 forks source link

Invert the sensor state when calendar invent starts #27

Open AdeTheux opened 3 years ago

AdeTheux commented 3 years ago

Is there any way to invert the way the sensor is displayed in the Home app? Currently, if there's a calendar event happening at a specific time (no offset configured), the sensor state will appear as "opened". I'd like it to be "closed", as in the office is closed because a meeting is starting. Thanks

CHuOK68 commented 2 years ago

Good idea

AdeTheux commented 2 years ago

I've managed to do this. Go to /usr/lib/node_modules/homebridge-calendar/src Open calendarsensor.js and compare the changes in the code below, I don't remember precisely what I changed a while back… But it now displays the open sensor/door in homebridge for my office when I don't have anything in my work diary.


'use strict';

class CalendarSensor {

  constructor(log, name, sensor, characteristic, onValue, offValue) {
    this.log = log;
    this.name = name;
    this.sensor = sensor;
    this._characteristic = characteristic;
    this._onValue = onValue;
    this._offValue = offValue;

    this.reset();
    this.pushState();
  }

  reset() {
    this._state = 1;
  }

  off() {
    this._state++;
  }

  on() {
    if (this._state > 0) {
      this._state--;
    }
  }

  pushState() {
    let value = this._offValue;
    if (this._state > 0) {
      value = this._onValue;
    }

    this.log(`Pushing calendar sensor '${this.name}' state ${this._state} - value ${value}`);
    this.sensor
      .getCharacteristic(this._characteristic)
      .updateValue(value);
  }
}

module.exports = CalendarSensor;