homebridge-miot
Version:
Homebridge plugin for devices supporting the miot protocol
219 lines (157 loc) • 6.86 kB
JavaScript
let Service, Characteristic, Accessory, HapStatusError, HAPStatus;
const BaseAccessory = require('../../base/BaseAccessory.js');
const Constants = require('../../constants/Constants.js');
const DevTypes = require('../../constants/DevTypes.js');
class BathHeaterAccessory extends BaseAccessory {
constructor(name, device, uuid, config, api, logger) {
Service = api.hap.Service;
Characteristic = api.hap.Characteristic;
Accessory = api.platformAccessory;
HapStatusError = api.hap.HapStatusError;
HAPStatus = api.hap.HAPStatus;
super(name, device, uuid, config, api, logger);
}
/*----------========== INIT ==========----------*/
initAccessoryObject() {
super.initAccessoryObject();
}
/*----------========== ACCESSORY INFO ==========----------*/
getAccessoryType() {
return DevTypes.BATH_HEATER;
}
/*----------========== INIT ACCESSORIES ==========----------*/
initAccessories(name, uuid) {
return [new Accessory(name, uuid, this.api.hap.Categories.AIR_HEATER)];
}
/*----------========== SETUP SERVICES ==========----------*/
setupMainAccessoryService() {
this.heaterService = new Service.HeaterCooler(this.getName(), 'heaterService');
this.heaterService
.getCharacteristic(Characteristic.Active)
.onGet(this.getHeaterActiveState.bind(this))
.onSet(this.setHeaterActiveState.bind(this));
this.heaterService
.getCharacteristic(Characteristic.CurrentHeaterCoolerState)
.onGet(this.getCurrentHeaterCoolerState.bind(this))
.setProps({
maxValue: Characteristic.CurrentHeaterCoolerState.HEATING,
validValues: [
Characteristic.CurrentHeaterCoolerState.INACTIVE,
Characteristic.CurrentHeaterCoolerState.IDLE,
Characteristic.CurrentHeaterCoolerState.HEATING,
],
});
this.heaterService
.getCharacteristic(Characteristic.TargetHeaterCoolerState)
.onGet(this.getTargetHeaterCoolerState.bind(this))
.onSet(this.setTargetHeaterCoolerState.bind(this))
.setProps({
maxValue: Characteristic.TargetHeatingCoolingState.HEAT,
validValues: [
Characteristic.TargetHeaterCoolerState.HEAT
]
});
this.addCurrentTemperatureCharacteristic(this.heaterService);
if (this.getDevice().supportsTargetTemperature()) {
this.heaterService
.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.onGet(this.getHeatingThresholdTemperature.bind(this))
.onSet(this.setHeatingThresholdTemperature.bind(this))
.setProps({
minValue: this.getDevice().targetTemperatureRange()[0],
maxValue: this.getDevice().targetTemperatureRange()[1],
minStep: this.getDevice().targetTemperatureRange()[2]
});
}
this.heaterService
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.setProps({
maxValue: Characteristic.TemperatureDisplayUnits.CELSIUS,
validValues: [
Characteristic.TemperatureDisplayUnits.CELSIUS
]
})
.setValue(Characteristic.TemperatureDisplayUnits.CELSIUS);
this.addLockPhysicalControlsCharacteristic(this.heaterService);
this.addAccessoryService(this.heaterService);
}
setupAdditionalAccessoryServices() {
this.prepareLightService();
super.setupAdditionalAccessoryServices(); // make sure we call super
}
/*----------========== CREATE ADDITIONAL SERVICES ==========----------*/
prepareLightService() {
if (this.getDevice().hasLightService()) {
this.addLightService('lightService', this.getName() + ' Light', this.getDevice().getLightService());
}
}
prepareModeControlServices() {
super.prepareModeControlServices();
if (this.getDevice().supportsHeaterMode()) {
this.addPropWrapper('Heater Mode', this.getDevice().heaterModeProp(), this.getDevice().heatingProp(), null, null, null);
}
}
prepareLightModeControlServices() {
if (this.getDevice().supportsLightModes()) {
this.addPropValueListWrapper('Light Mode', Properties.LIGHT_MODE, null);
}
}
/*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/
getHeaterActiveState() {
if (this.isMiotDeviceConnected()) {
return this.getDevice().isBathHeaterEnabled() ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE;
}
return Characteristic.Active.INACTIVE;
}
setHeaterActiveState(state) {
if (this.isMiotDeviceConnected()) {
let value = state === Characteristic.Active.ACTIVE;
this.getDevice().setBathHeaterEnabled(value);
} else {
throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
getCurrentHeaterCoolerState() {
if (this.isMiotDeviceConnected()) {
return this.getDevice().isBathHeaterEnabled() ? Characteristic.CurrentHeaterCoolerState.HEATING : Characteristic.CurrentHeaterCoolerState.IDLE;
}
return Characteristic.CurrentHeaterCoolerState.INACTIVE;
}
getTargetHeaterCoolerState() {
return Characteristic.TargetHeaterCoolerState.HEAT;
}
setTargetHeaterCoolerState(state) {
if (this.isMiotDeviceConnected()) {
this.getDevice().startHeatingIfNecessary(); // start heating
} else {
throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
getHeatingThresholdTemperature() {
if (this.isMiotDeviceConnected()) {
return this.getDevice().getTargetTemperatureSafe();
}
return this.getDevice().targetTemperatureRange()[0]; // return minimum value
}
setHeatingThresholdTemperature(temp) {
if (this.isMiotDeviceConnected()) {
this.getDevice().setTargetTemperature(temp);
} else {
throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
// ----- additional services
/*----------========== STATUS ==========----------*/
updateAccessoryStatus() {
if (this.heaterService) this.heaterService.getCharacteristic(Characteristic.CurrentHeaterCoolerState).updateValue(this.getCurrentHeaterCoolerState());
if (this.heaterService) this.heaterService.getCharacteristic(Characteristic.TargetHeaterCoolerState).updateValue(this.getTargetHeaterCoolerState());
if (this.heaterService && this.getDevice().supportsTargetTemperature()) this.heaterService.getCharacteristic(Characteristic.HeatingThresholdTemperature).updateValue(this.getHeatingThresholdTemperature());
super.updateAccessoryStatus();
}
/*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/
/*----------========== GETTERS ==========----------*/
/*----------========== PROPERTY WRAPPERS ==========----------*/
/*----------========== PROPERTY HELPERS ==========----------*/
/*----------========== HELPERS ==========----------*/
}
module.exports = BathHeaterAccessory;