Skip to content

Commit

Permalink
[Fix] fix thermo and dehumidifier update (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
madchicken authored Jul 1, 2020
1 parent afde710 commit bef2392
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 95 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-comelit-platform",
"version": "2.1.0",
"version": "2.2.0",
"author": "Pierpaolo Follia",
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,20 +38,21 @@
},
"dependencies": {
"@sentry/node": "5.12.2",
"async-mqtt": "^2.4.2",
"comelit-client": "1.6.2",
"async-mqtt": "^2.6.1",
"comelit-client": "1.7.2",
"express": "^4.17.1",
"lodash": "^4.17.15",
"mqtt": "^3.0.0",
"mqtt-packet": "^6.3.0",
"prom-client": "^11.5.3",
"mqtt": "^4.1.0",
"mqtt-packet": "^6.3.2",
"prom-client": "^12.0.0",
"typescript": "^3.7.4"
},
"devDependencies": {
"@types/express": "^4.17.2",
"@types/lodash": "^4.14.149",
"@types/mqtt": "^2.5.0",
"@types/node": "^11.15.3",
"@types/jest": "^25.2.3",
"@typescript-eslint/eslint-plugin": "^2.14.0",
"@typescript-eslint/parser": "^2.14.0",
"hap-nodejs": "^0.7.3",
Expand Down
3 changes: 3 additions & 0 deletions src/accessories/__tests__/thermostat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe('Thermostat', () => {
it('should update thermostat status', async () => {});
});
12 changes: 10 additions & 2 deletions src/accessories/comelit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { ComelitClient, DeviceData } from 'comelit-client';
import { AccessoryPlugin, Controller, Logger, PlatformAccessory, Service } from 'homebridge';
import {
AccessoryPlugin,
Controller,
Logger,
PlatformAccessory,
PlatformAccessoryEvent,
Service,
} from 'homebridge';
import { ComelitPlatform } from '../comelit-platform';

export abstract class ComelitAccessory<T extends DeviceData> implements AccessoryPlugin {
export abstract class ComelitAccessory<T extends DeviceData> {
readonly platform: ComelitPlatform;
readonly accessory: PlatformAccessory;
readonly log: Logger;
Expand All @@ -24,6 +31,7 @@ export abstract class ComelitAccessory<T extends DeviceData> implements Accessor
this.client = client;
this.services = this.initServices();
this.reachable = true;
this.accessory.on(PlatformAccessoryEvent.IDENTIFY, () => this.identify());
}

getServices(): Service[] {
Expand Down
53 changes: 26 additions & 27 deletions src/accessories/dehumidifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { ComelitAccessory } from './comelit';
import { ClimaMode, ClimaOnOff, ComelitClient, ThermostatDeviceData } from 'comelit-client';
import {
ClimaMode,
ClimaOnOff,
ComelitClient,
STATUS_ON,
ThermoSeason,
ThermostatDeviceData,
} from 'comelit-client';
import client from 'prom-client';
import { ComelitPlatform } from '../comelit-platform';
import { CharacteristicEventTypes, PlatformAccessory, Service, VoidCallback } from 'homebridge';
Expand Down Expand Up @@ -105,10 +112,18 @@ export class Dehumidifier extends ComelitAccessory<ThermostatDeviceData> {

public update(data: ThermostatDeviceData): void {
const Characteristic = this.platform.Characteristic;
const isOff = this.isOff(data);
const isAuto = this.isAuto(data);
this.log.info(
`Dehumidifier ${this.accessory.displayName} auto mode is ${isAuto}, off ${isOff}`
const auto_man = data.auto_man_umi;
const isOff = auto_man === ClimaMode.OFF_AUTO || auto_man === ClimaMode.OFF_MANUAL;
const isOn = auto_man === ClimaMode.AUTO || auto_man === ClimaMode.MANUAL;
const isAuto = auto_man === ClimaMode.OFF_AUTO || auto_man === ClimaMode.AUTO;
const isWorking = isOn && data.status === STATUS_ON;

console.log(
`Dehumidifier status is ${isOff ? 'OFF' : 'ON'}, ${
isAuto ? 'auto mode' : 'manual mode'
}, Humidity level ${parseInt(data.umidita)}%, threshold ${
data.soglia_attiva_umi
}%\nGeneral status is ${data.status === '1' ? 'ON' : 'OFF'}`
);

this.dehumidifierService
Expand All @@ -125,34 +140,18 @@ export class Dehumidifier extends ComelitAccessory<ThermostatDeviceData> {
.updateValue(
isOff
? CurrentHumidifierDehumidifierState.INACTIVE
: isAuto
? CurrentHumidifierDehumidifierState.IDLE
: CurrentHumidifierDehumidifierState.DEHUMIDIFYING
: isWorking
? CurrentHumidifierDehumidifierState.DEHUMIDIFYING
: CurrentHumidifierDehumidifierState.IDLE
);
this.dehumidifierService
.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState)
.updateValue(
isAuto
? TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER
: TargetHumidifierDehumidifierState.DEHUMIDIFIER
);
.updateValue(TargetHumidifierDehumidifierState.DEHUMIDIFIER);
this.dehumidifierService
.getCharacteristic(Characteristic.Active)
.updateValue(isOff ? Active.INACTIVE : Active.ACTIVE);
.updateValue(isWorking ? Active.INACTIVE : Active.ACTIVE);

dehumidifierStatus.set({ dehumidifier_name: data.descrizione }, isOff ? 0 : 1);
dehumidifierStatus.set({ dehumidifier_name: data.descrizione }, isWorking ? 0 : 1);
dehumidifierHumidity.set({ dehumidifier_name: data.descrizione }, parseInt(data.umidita));
}

private isAuto(data: ThermostatDeviceData) {
return data.auto_man_umi === ClimaMode.AUTO;
}

private isOff(data: ThermostatDeviceData) {
return (
data.auto_man_umi === ClimaMode.OFF_MANUAL ||
data.auto_man_umi === ClimaMode.NONE ||
data.auto_man_umi === ClimaMode.OFF_AUTO
);
}
}
78 changes: 37 additions & 41 deletions src/accessories/thermostat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { TargetHeatingCoolingState, TemperatureDisplayUnits } from './hap';
import client from 'prom-client';
import { ComelitPlatform } from '../comelit-platform';
import { PlatformAccessory, CharacteristicEventTypes, Service, VoidCallback } from 'homebridge';
import { CharacteristicEventTypes, PlatformAccessory, Service, VoidCallback } from 'homebridge';

const thermostatStatus = new client.Gauge({
name: 'comelit_thermostat_status',
Expand Down Expand Up @@ -43,14 +43,7 @@ export class Thermostat extends ComelitAccessory<ThermostatDeviceData> {
.getCharacteristic(Characteristic.TargetTemperature)
.on(CharacteristicEventTypes.SET, async (temperature: number, callback: VoidCallback) => {
try {
const currentTemperature = this.thermostatService.getCharacteristic(
Characteristic.TargetTemperature
).value;
const normalizedTemp = temperature * 10;
if (currentTemperature !== temperature) {
await this.client.setTemperature(this.device.id, normalizedTemp);
this.device.temperatura = `${normalizedTemp}`;
}
await this.setTargetTemperature(temperature);
callback();
} catch (e) {
callback(e);
Expand Down Expand Up @@ -103,24 +96,44 @@ export class Thermostat extends ComelitAccessory<ThermostatDeviceData> {
return [accessoryInformation, this.thermostatService];
}

private async setTargetTemperature(temperature: number) {
const Characteristic = this.platform.Characteristic;
const currentTemperature = this.thermostatService.getCharacteristic(
Characteristic.TargetTemperature
).value;
const normalizedTemp = temperature * 10;
if (currentTemperature !== temperature) {
await this.client.setTemperature(this.device.id, normalizedTemp);
this.device.temperatura = `${normalizedTemp}`;
}
}

public update(data: ThermostatDeviceData): void {
const Characteristic = this.platform.Characteristic;
const currentCoolingState = this.isOff()

const auto_man = data.auto_man;
const isOff = auto_man === ClimaMode.OFF_AUTO || auto_man === ClimaMode.OFF_MANUAL;
const isWinter = data.est_inv === ThermoSeason.WINTER;
const isAuto = auto_man === ClimaMode.OFF_AUTO || auto_man === ClimaMode.AUTO;
const isOn = auto_man === ClimaMode.AUTO || auto_man === ClimaMode.MANUAL;
const isWorking = isOn && data.status === STATUS_ON;

const currentCoolingState = isOff
? TargetHeatingCoolingState.OFF
: this.isWinter()
: isWinter
? TargetHeatingCoolingState.HEAT
: TargetHeatingCoolingState.COOL;
this.thermostatService
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.updateValue(currentCoolingState);

let targetState;
if (this.isOff()) {
let targetState: TargetHeatingCoolingState;
if (isOff || !isWorking) {
targetState = TargetHeatingCoolingState.OFF;
} else if (this.isAuto()) {
} else if (isAuto) {
targetState = TargetHeatingCoolingState.AUTO;
} else {
if (this.isWinter()) {
if (isWinter) {
targetState = TargetHeatingCoolingState.HEAT;
} else {
targetState = TargetHeatingCoolingState.COOL;
Expand All @@ -130,44 +143,27 @@ export class Thermostat extends ComelitAccessory<ThermostatDeviceData> {
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.updateValue(targetState);

console.log(
`Current cooling state is now ${currentCoolingState}, target state is ${targetState}`
);
const temperature = data.temperatura ? parseFloat(data.temperatura) / 10 : 0;
this.log.info(`Temperature for ${this.accessory.displayName} is ${temperature}`);
const targetTemperature = data.soglia_attiva ? parseFloat(data.soglia_attiva) / 10 : 0;
this.log.info(
`${data.objectId} - ${this.accessory.displayName}:\nThermostat status ${
isOff ? 'OFF' : 'ON'
}, ${isAuto ? 'auto mode' : 'manual mode'}, ${
data.est_inv === ThermoSeason.WINTER ? 'winter' : 'summer'
}, Temperature ${temperature}°, threshold ${targetTemperature}°`
);
this.thermostatService
.getCharacteristic(Characteristic.CurrentTemperature)
.updateValue(temperature);

const activeThreshold = data.soglia_attiva;
const targetTemperature = activeThreshold ? parseFloat(activeThreshold) / 10 : 0;
this.log.info(`Threshold for ${this.accessory.displayName} is ${targetTemperature}`);
this.thermostatService
.getCharacteristic(Characteristic.TargetTemperature)
.updateValue(targetTemperature);
this.thermostatService
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.updateValue(TemperatureDisplayUnits.CELSIUS);

thermostatStatus.set({ thermostat_name: data.descrizione }, parseInt(this.device.status));
thermostatStatus.set({ thermostat_name: data.descrizione }, isWorking ? 0 : 1);
thermostatTemperature.set({ thermostat_name: data.descrizione }, temperature);
}

isOff(): boolean {
return (
this.device.auto_man === ClimaMode.OFF_AUTO || this.device.auto_man === ClimaMode.OFF_MANUAL
);
}

isRunning(): boolean {
return this.device.status === STATUS_ON;
}

isAuto(): boolean {
return this.device.auto_man === ClimaMode.OFF_AUTO || this.device.auto_man === ClimaMode.AUTO;
}

isWinter(): boolean {
return this.device.est_inv === ThermoSeason.WINTER;
}
}
18 changes: 9 additions & 9 deletions src/comelit-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class ComelitPlatform implements DynamicPlatformPlugin {
this.mappedNames = {};
await this.login();
this.log.info('Building accessories list...');
const homeIndex = await this.client.fecthHomeIndex();
const homeIndex = await this.client.fetchHomeIndex();
if (this.config.hide_lights !== true) {
this.mapLights(homeIndex);
}
Expand Down Expand Up @@ -290,14 +290,14 @@ export class ComelitPlatform implements DynamicPlatformPlugin {
await this.shutdown();
this.log.info('Creating MQTT client and logging in...');
this.client = this.client || new ComelitClient(this.updateAccessory.bind(this), this.log);
await this.client.init(
this.config.broker_url,
this.config.username,
this.config.password,
this.config.hub_username,
this.config.hub_password,
this.config.client_id
);
await this.client.init({
host: this.config.broker_url,
username: this.config.username,
password: this.config.password,
hub_username: this.config.hub_username,
hub_password: this.config.hub_password,
clientId: this.config.client_id,
});
if (!this.server && this.config.export_prometheus_metrics) {
this.server = expr.listen(this.config.exporter_http_port || DEFAULT_HTTP_PORT);
}
Expand Down
Loading

0 comments on commit bef2392

Please sign in to comment.