From 0572af9d6f15bb16bf393f33873a7dbcd78b09d3 Mon Sep 17 00:00:00 2001 From: ghosty2004 Date: Mon, 1 Jul 2024 19:03:58 +0300 Subject: [PATCH] fix: issue #1 --- package.json | 2 +- src/accessory/TemperatureAccessory.ts | 67 +++++++++++++++++++++------ src/utils.ts | 7 +++ 3 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 src/utils.ts diff --git a/package.json b/package.json index c65ee21..25bc31a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": false, "displayName": "Homebridge ConnectLife AC", "name": "homebridge-connectlife-ac", - "version": "1.2.1", + "version": "1.3.0", "description": "Control your ConnectLife air conditioner with Homebridge", "license": "Apache-2.0", "homepage": "https://github.com/ghosty2004/homebridge-connectlife-ac", diff --git a/src/accessory/TemperatureAccessory.ts b/src/accessory/TemperatureAccessory.ts index a19ac7c..4f6078e 100644 --- a/src/accessory/TemperatureAccessory.ts +++ b/src/accessory/TemperatureAccessory.ts @@ -2,6 +2,7 @@ import { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'; import { ConnectLifeAcPlatformPlugin } from '../platform'; import { ConnectLifeApi } from '../lib'; import { WorkModes } from '../constants'; +import { celsiusToFahrenheit, fahrenheitToCelsius } from '../utils'; export class TemperatureAccessory { private deviceNickName: string; @@ -64,6 +65,11 @@ export class TemperatureAccessory { ) .onSet(this.setHeatingThresholdTemperature.bind(this)) .onGet(this.getHeatingThresholdTemperature.bind(this)); + + this.service + .getCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits) + .onSet(this.setTemperatureDisplayUnits.bind(this)) + .onGet(this.getTemperatureDisplayUnits.bind(this)); } async setActive(value: CharacteristicValue) { @@ -104,14 +110,13 @@ export class TemperatureAccessory { } async getCurrentTemperature(): Promise { - const { t_temp } = await this.connectLifeApi.getDeviceProperties( - this.deviceNickName, - { + const { t_temp, t_temp_type } = + await this.connectLifeApi.getDeviceProperties(this.deviceNickName, { t_temp: 'integer', - }, - ); + t_temp_type: 'integer', + }); - return t_temp; + return t_temp_type === 1 ? fahrenheitToCelsius(t_temp as number) : t_temp; } setSwingMode(value: CharacteristicValue) { @@ -122,18 +127,26 @@ export class TemperatureAccessory { return 0; } - setCoolingThresholdTemperature(value: CharacteristicValue) { + async setCoolingThresholdTemperature(value: CharacteristicValue) { + const { t_temp_type } = await this.connectLifeApi.getDeviceProperties( + this.deviceNickName, + { + t_temp_type: 'integer', + }, + ); + this.connectLifeApi.changeDeviceProperties(this.deviceNickName, { - t_temp: value, + t_temp: t_temp_type === 1 ? celsiusToFahrenheit(value as number) : value, t_work_mode: WorkModes.Cool, }); this.platform.log.info('Set CoolingThresholdTemperature', value); } async getCoolingThresholdTemperature(): Promise { - const { t_temp, t_work_mode } = + const { t_temp, t_temp_type, t_work_mode } = await this.connectLifeApi.getDeviceProperties(this.deviceNickName, { t_temp: 'integer', + t_temp_type: 'integer', t_work_mode: 'integer', }); @@ -141,21 +154,29 @@ export class TemperatureAccessory { return 10; } - return t_temp; + return t_temp_type === 1 ? fahrenheitToCelsius(t_temp as number) : t_temp; } - setHeatingThresholdTemperature(value: CharacteristicValue) { + async setHeatingThresholdTemperature(value: CharacteristicValue) { + const { t_temp_type } = await this.connectLifeApi.getDeviceProperties( + this.deviceNickName, + { + t_temp_type: 'integer', + }, + ); + this.connectLifeApi.changeDeviceProperties(this.deviceNickName, { - t_temp: value, + t_temp: t_temp_type === 1 ? celsiusToFahrenheit(value as number) : value, t_work_mode: WorkModes.Heat, }); this.platform.log.info('Set HeatingThresholdTemperature', value); } async getHeatingThresholdTemperature(): Promise { - const { t_temp, t_work_mode } = + const { t_temp, t_temp_type, t_work_mode } = await this.connectLifeApi.getDeviceProperties(this.deviceNickName, { t_temp: 'integer', + t_temp_type: 'integer', t_work_mode: 'integer', }); @@ -163,6 +184,24 @@ export class TemperatureAccessory { return 0; } - return t_temp; + return t_temp_type === 1 ? fahrenheitToCelsius(t_temp as number) : t_temp; + } + + setTemperatureDisplayUnits(value: CharacteristicValue) { + this.connectLifeApi.changeDeviceProperties(this.deviceNickName, { + t_temp_type: value, + }); + this.platform.log.info('Set TemperatureDisplayUnits', value); + } + + async getTemperatureDisplayUnits(): Promise { + const { t_temp_type } = await this.connectLifeApi.getDeviceProperties( + this.deviceNickName, + { + t_temp_type: 'integer', + }, + ); + + return t_temp_type; } } diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..e3bb8ea --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,7 @@ +export const celsiusToFahrenheit = (celsius: number) => { + return (celsius * 9) / 5 + 32; +}; + +export const fahrenheitToCelsius = (fahrenheit: number) => { + return ((fahrenheit - 32) * 5) / 9; +};