UNPKG

homebridge-plugin-lares4

Version:

a homebridge plugin to expose Ksenia Lares 4 home automation systems on the Apple ecosystem

131 lines 7.49 kB
import { DateTime } from 'luxon'; import { setThermostatTarget, setThermostatMode, setThermostatManualTimeout, Lares4ThermostatActModes, Lares4ThermostatSeasons, } from 'lares4-ts'; function getThermostatTimeoutValue(timeoutValue) { if (timeoutValue === 0) { return '00'; } const now = DateTime.now(); const nearestHour = now.minute >= 30 ? now.hour + 1 : now.hour; const target = DateTime.fromObject({ hour: nearestHour }).plus({ hour: timeoutValue }); return target.toFormat('HH'); } export class Lares4PlatformThermostat { platform; accessory; thermostat; constructor(platform, accessory) { this.platform = platform; this.accessory = accessory; this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Ksenia') .setCharacteristic(this.platform.Characteristic.Model, 'Lares4') .setCharacteristic(this.platform.Characteristic.SerialNumber, '12345671'); this.thermostat = this.accessory.getService(this.platform.Service.Thermostat) || this.accessory.addService(this.platform.Service.Thermostat); this.thermostat.setCharacteristic(this.platform.Characteristic.Name, `Thermostat ${accessory.context.sensor.details.ID}`); this.thermostat.getCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState) .onGet(this.getCurrentHeatingCoolingState.bind(this)); this.thermostat.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState) .onGet(this.getTargetHeatingCoolingState.bind(this)) .onSet(this.setTargetHeatingCoolingState.bind(this)) .setProps({ validValues: [ this.platform.Characteristic.TargetHeatingCoolingState.OFF, this.platform.Characteristic.TargetHeatingCoolingState.HEAT, this.platform.Characteristic.TargetHeatingCoolingState.COOL, ], }); this.thermostat.getCharacteristic(this.platform.Characteristic.CurrentTemperature) .onGet(this.getCurrentTemperature.bind(this)); this.thermostat.getCharacteristic(this.platform.Characteristic.TargetTemperature) .onGet(this.getTargetTemperature.bind(this)) .onSet(this.setTargetTemperature.bind(this)); this.platform.lares4?.sensors_status_emitter.subscribe((output_status) => { if (output_status.id === this.accessory.context.sensor.id) { this.setSensorStatus(output_status.status); } }); this.platform.lares4?.temperatures_status_emitter.subscribe((temperature_status) => { if (temperature_status.id === this.accessory.context.sensor.id) { this.setTemperatureStatus(temperature_status.status); } }); } setSensorStatus(accessoryStatus) { const domus = accessoryStatus.DOMUS; const { TEM, HUM } = domus; this.thermostat.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, parseFloat(TEM)); this.thermostat.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, parseFloat(HUM)); } setTemperatureStatus(temperatureStatus) { const { THERM: { ACT_MODEL, ACT_SEA, OUT_STATUS } } = temperatureStatus; let currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF; let targetHeatingCoolingState = this.platform.Characteristic.TargetHeatingCoolingState.OFF; if (OUT_STATUS === 'ON') { if (ACT_SEA === Lares4ThermostatSeasons.WINTER) { currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT; } if (ACT_SEA === Lares4ThermostatSeasons.SUMMER) { currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.COOL; } } if (ACT_MODEL !== Lares4ThermostatActModes.OFF) { if (ACT_SEA === Lares4ThermostatSeasons.WINTER) { targetHeatingCoolingState = this.platform.Characteristic.TargetHeatingCoolingState.HEAT; } if (ACT_SEA === Lares4ThermostatSeasons.SUMMER) { targetHeatingCoolingState = this.platform.Characteristic.TargetHeatingCoolingState.COOL; } } this.thermostat.updateCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState, currentHeatingCoolingState); this.thermostat.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, targetHeatingCoolingState); } getCurrentTemperature() { return parseFloat((this.platform.lares4.status.sensors?.[this.accessory.context.sensor.id].DOMUS).TEM); } getTargetTemperature() { const configuration = this.platform.lares4.configuration.thermostats?.[this.accessory.context.configuration.id]; const season = configuration.ACT_SEA; return parseFloat(configuration[season].TM); } setTargetTemperature(value) { const configuration = this.platform.lares4.configuration.thermostats?.[this.accessory.context.configuration.id]; const season = configuration.ACT_SEA; setThermostatTarget(this.platform.lares4, this.accessory.context.configuration.id, season, value); } getCurrentHeatingCoolingState() { const { THERM: { ACT_SEA, OUT_STATUS } } = this.platform.lares4.status.temperatures?.[this.accessory.context.temperature.id]; if (OUT_STATUS === 'ON') { if (ACT_SEA === Lares4ThermostatSeasons.WINTER) { return this.platform.Characteristic.CurrentHeatingCoolingState.HEAT; } if (ACT_SEA === Lares4ThermostatSeasons.SUMMER) { return this.platform.Characteristic.CurrentHeatingCoolingState.COOL; } } return this.platform.Characteristic.CurrentHeatingCoolingState.OFF; } getTargetHeatingCoolingState() { const { THERM: { ACT_MODEL, ACT_SEA } } = this.platform.lares4.status.temperatures?.[this.accessory.context.temperature.id]; if (ACT_MODEL !== Lares4ThermostatActModes.OFF) { if (ACT_SEA === Lares4ThermostatSeasons.WINTER) { return this.platform.Characteristic.TargetHeatingCoolingState.HEAT; } if (ACT_SEA === Lares4ThermostatSeasons.SUMMER) { return this.platform.Characteristic.TargetHeatingCoolingState.COOL; } } return this.platform.Characteristic.TargetHeatingCoolingState.OFF; } setTargetHeatingCoolingState(value) { const timeoutValue = getThermostatTimeoutValue(this.platform.config.thermostatManualTimeout); if (value === this.platform.Characteristic.TargetHeatingCoolingState.OFF) { setThermostatMode(this.platform.lares4, this.accessory.context.configuration.details.ID, Lares4ThermostatActModes.OFF); } else { setThermostatMode(this.platform.lares4, this.accessory.context.configuration.details.ID, Lares4ThermostatActModes.MANUAL_TIMER); setThermostatManualTimeout(this.platform.lares4, this.accessory.context.configuration.details.ID, timeoutValue); } this.thermostat.updateCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState, value); } } //# sourceMappingURL=Lares4PlatformThermostat.js.map