@ronniepettersson/homebridge-dummy
Version:
Create Homebridge accessories to help with automation and control — scheduling, delays, sensors, commands, webhooks, and more
176 lines • 7.82 kB
JavaScript
import { DummyAccessory } from './base.js';
import { strings } from '../i18n/i18n.js';
import { AccessoryType, DefaultThermostatState, isValidTemperatureUnits, isValidThermostatState, printableValues, TemperatureUnits, WebhookCommand } from '../model/enums.js';
import { Webhook } from '../model/webhook.js';
import { storageGet_Deprecated, Storage } from '../tools/storage.js';
import { fromCelsius, toCelsius } from '../tools/temperature.js';
const DEFAULT_TEMPERATURE = 20;
export class ThermostatAccessory extends DummyAccessory {
STATE_AUTO;
STATE_COOL;
STATE_HEAT;
STATE_OFF;
state;
temperature;
constructor(Service, Characteristic, accessory, config, log, isGrouped) {
super(Service, Characteristic, accessory, config, log, isGrouped);
this.STATE_AUTO = Characteristic.TargetHeatingCoolingState.AUTO;
this.STATE_COOL = Characteristic.TargetHeatingCoolingState.COOL;
this.STATE_HEAT = Characteristic.TargetHeatingCoolingState.HEAT;
this.STATE_OFF = Characteristic.TargetHeatingCoolingState.OFF;
if (!isValidTemperatureUnits(config.temperatureUnits)) {
this.log.warning(strings.thermostat.badUnits, this.name, `'${config.temperatureUnits}'`, printableValues(TemperatureUnits));
}
if (!isValidThermostatState(config.defaultThermostatState)) {
this.log.warning(strings.thermostat.badDefault, this.name, `'${config.defaultThermostatState}'`, printableValues(DefaultThermostatState));
}
this.state = this.defaultState;
this.temperature = this.defaultTemperature;
this.accessoryService.getCharacteristic(this.Characteristic.TemperatureDisplayUnits)
.onGet(this.getUnits.bind(this));
this.accessoryService.getCharacteristic(this.Characteristic.CurrentHeatingCoolingState)
.onGet(this.getCurrentState.bind(this));
this.accessoryService.getCharacteristic(this.Characteristic.TargetHeatingCoolingState)
.setProps({
minStep: 1,
validValues: [
this.STATE_OFF,
this.STATE_HEAT,
this.STATE_COOL,
this.STATE_AUTO,
],
})
.onGet(this.getState.bind(this))
.onSet(this.setState.bind(this));
this.accessoryService.getCharacteristic(this.Characteristic.CurrentTemperature)
.onGet(this.getTemperature.bind(this));
this.accessoryService.getCharacteristic(this.Characteristic.TargetTemperature)
.onGet(this.getTemperature.bind(this))
.onSet(this.setTemperature.bind(this));
this.initializeThermostat();
}
async initializeThermostat() {
if (!this.isStateful) {
this.accessoryService.updateCharacteristic(this.Characteristic.TargetHeatingCoolingState, this.state);
this.accessoryService.updateCharacteristic(this.Characteristic.TargetTemperature, this.temperature);
this.accessoryService.updateCharacteristic(this.Characteristic.CurrentTemperature, this.temperature);
return;
}
const state = await storageGet_Deprecated(this.defaultStateStorageKey);
if (state !== undefined) {
await this.setState(state);
}
const temperature = await storageGet_Deprecated(this.defaulTemperatureStorageKey);
if (temperature !== undefined) {
await this.setTemperature(temperature);
}
}
get defaulTemperatureStorageKey() {
return `${this.identifier}:Temperature`;
}
getAccessoryType() {
return AccessoryType.Thermostat;
}
webhooks() {
return [
new Webhook(this.identifier, WebhookCommand.TargetHeatingCoolingState, (value) => {
this.setState(value);
return this.stateLogTemplateForCV(value).replace('%s', this.name);
}),
new Webhook(this.identifier, WebhookCommand.TargetTemperature, (value) => {
this.setTemperature(value);
return this.temperatureLogTemplateForCV(value).replace('%s', this.name);
}),
];
}
get defaultState() {
switch (this.config.defaultThermostatState) {
case DefaultThermostatState.AUTO:
return this.STATE_AUTO;
case DefaultThermostatState.COOL:
return this.STATE_COOL;
case DefaultThermostatState.HEAT:
return this.STATE_HEAT;
}
return this.STATE_OFF;
}
get defaultTemperature() {
return this.config.defaultTemperature ? toCelsius(this.config.defaultTemperature, this.config.temperatureUnits) : DEFAULT_TEMPERATURE;
}
get units() {
return this.config.temperatureUnits ?? TemperatureUnits.CELSIUS;
}
async getUnits() {
return this.units === TemperatureUnits.FAHRENHEIT
? this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT : this.Characteristic.TemperatureDisplayUnits.CELSIUS;
}
async getCurrentState() {
return this.Characteristic.CurrentHeatingCoolingState.OFF;
}
async getState() {
return this.state;
}
async setState(value) {
if (this.state !== value) {
this.logState(value);
if (this.config.commandOff && value === this.STATE_OFF) {
this.executeCommand(this.config.commandOff);
}
else if (this.config.commandOn && this.state === this.STATE_OFF && value !== this.STATE_OFF) {
this.executeCommand(this.config.commandOn);
}
}
this.state = value;
if (this.isStateful) {
await Storage.set(this.defaultStateStorageKey, this.state);
}
this.accessoryService.updateCharacteristic(this.Characteristic.TargetHeatingCoolingState, this.state);
}
async getTemperature() {
return this.temperature;
}
async setTemperature(value) {
if (this.temperature !== value) {
this.logTemperature(value);
if (this.config.commandTemperature) {
this.executeCommand(this.config.commandTemperature);
}
}
this.temperature = value;
if (this.isStateful) {
await Storage.set(this.defaulTemperatureStorageKey, this.temperature);
}
this.accessoryService.updateCharacteristic(this.Characteristic.TargetTemperature, this.temperature);
this.accessoryService.updateCharacteristic(this.Characteristic.CurrentTemperature, this.temperature);
}
async schedule() {
throw new Error(strings.thermostat.unsupportedFunction.replace('%s', `${this.schedule.name}()`));
}
async reset() {
throw new Error(strings.thermostat.unsupportedFunction.replace('%s', `${this.reset.name}()`));
}
stateLogTemplateForCV(value) {
switch (value) {
case this.STATE_AUTO:
return strings.thermostat.auto;
case this.STATE_COOL:
return strings.thermostat.cool;
case this.STATE_HEAT:
return strings.thermostat.heat;
default:
return strings.thermostat.off;
}
}
logState(value) {
this.logIfDesired(this.stateLogTemplateForCV(value));
}
temperatureLogTemplateForCV(value) {
const message = this.units === TemperatureUnits.FAHRENHEIT ? strings.thermostat.temperatureF : strings.thermostat.temperatureC;
const temperature = fromCelsius(value, this.config.temperatureUnits);
return message.replace('%d', temperature.toString());
}
logTemperature(value) {
this.logIfDesired(this.temperatureLogTemplateForCV(value));
}
}
//# sourceMappingURL=thermostat.js.map