homebridge-ecobee-status
Version:
Homebridge plugin to control Ecobee thermostat Home/Away/Sleep status through HomeKit security system interface
84 lines • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutomationSwitchAccessory = void 0;
const types_1 = require("./types");
class AutomationSwitchAccessory {
constructor(platform, accessory, mainAccessory) {
this.platform = platform;
this.accessory = accessory;
this.lastTriggeredState = types_1.ClimateState.HOME;
this.mainAccessory = mainAccessory;
// Set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Ecobee')
.setCharacteristic(this.platform.Characteristic.Model, 'Away Switch')
.setCharacteristic(this.platform.Characteristic.SerialNumber, 'ECOBEEAWAY2');
// Use Switch for a toggle interface
this.service = this.accessory.getService(this.platform.Service.Switch) ||
this.accessory.addService(this.platform.Service.Switch);
// Handle switch state changes
this.service.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleSwitch.bind(this))
.onGet(this.getSwitchState.bind(this));
// Set the service name
this.service.setCharacteristic(this.platform.Characteristic.Name, 'Ecobee Away');
}
/**
* Handle switch events
* ON = Away mode
* OFF = Home mode
*/
async handleSwitch(value) {
const isOn = value;
try {
// Get the security system service from the main accessory
const securityService = this.mainAccessory.getService(this.platform.Service.SecuritySystem);
if (!securityService) {
throw new Error('Security service not found');
}
// Convert switch state to climate state (ON = Away, OFF = Home)
const climateState = isOn ? types_1.ClimateState.AWAY : types_1.ClimateState.HOME;
// Update the security system state
await securityService.setCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.mapClimateToSecurityState(climateState));
this.lastTriggeredState = climateState;
}
catch (error) {
this.platform.log.error('Failed to handle switch event:', error);
throw error;
}
}
/**
* Get current switch state based on climate state
* Returns true if Away, false if Home
*/
async getSwitchState() {
const securityService = this.mainAccessory.getService(this.platform.Service.SecuritySystem);
if (!securityService) {
throw new Error('Security service not found');
}
const currentState = await securityService.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState).value;
// Return true if Away, false if Home
return currentState === this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM;
}
/**
* Map climate state to security system state
*/
mapClimateToSecurityState(climate) {
switch (climate) {
case types_1.ClimateState.AWAY:
return this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM;
case types_1.ClimateState.HOME:
return this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM;
default:
return this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM;
}
}
/**
* Map security system state to switch state
*/
mapSecurityToSwitchState(securityState) {
return securityState === this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM;
}
}
exports.AutomationSwitchAccessory = AutomationSwitchAccessory;
//# sourceMappingURL=automationSwitchAccessory.js.map