forked from tuya/tuya-homebridge
-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8539193
commit 29c0ba6
Showing
1 changed file
with
55 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,60 @@ | ||
import BaseAccessory from "./BaseAccessory"; | ||
import BaseAccessory from './BaseAccessory'; | ||
|
||
export default class TemperatureHumiditySensorAccessory extends BaseAccessory { | ||
configureServices(): void { | ||
const { temperatureSchemas, humiditySchemas } = | ||
this.getDynamicSchemaCodes(); | ||
|
||
temperatureSchemas.forEach((schema, index) => { | ||
const serviceName = `Temperature Sensor ${index + 1}`; | ||
const serviceSubtype = `temperature_sensor_${index + 1}`; | ||
const service = | ||
this.accessory.getServiceById( | ||
this.Service.TemperatureSensor, | ||
serviceSubtype | ||
) || | ||
this.accessory.addService( | ||
this.Service.TemperatureSensor, | ||
serviceName, | ||
serviceSubtype | ||
); | ||
|
||
service | ||
.getCharacteristic(this.Characteristic.CurrentTemperature) | ||
.onGet(() => { | ||
const status = this.getStatus(schema.code); | ||
if (status) { | ||
const property = this.getSchema(schema.code) | ||
?.property as any; | ||
const multiple = Math.pow(10, property.scale || 0); | ||
return Math.min( | ||
Math.max((status.value as number) / multiple, -100), | ||
100 | ||
); | ||
} | ||
return 0; // Default value if no status is found | ||
}); | ||
}); | ||
|
||
humiditySchemas.forEach((schema, index) => { | ||
const serviceName = `Humidity Sensor ${index + 1}`; | ||
const serviceSubtype = `humidity_sensor_${index + 1}`; | ||
const service = | ||
this.accessory.getServiceById( | ||
this.Service.HumiditySensor, | ||
serviceSubtype | ||
) || | ||
this.accessory.addService( | ||
this.Service.HumiditySensor, | ||
serviceName, | ||
serviceSubtype | ||
); | ||
|
||
service | ||
.getCharacteristic(this.Characteristic.CurrentRelativeHumidity) | ||
.onGet(() => { | ||
const status = this.getStatus(schema.code); | ||
if (status) { | ||
return status.value as number; | ||
} | ||
return 0; // Default value if no status is found | ||
}); | ||
configureServices(): void { | ||
const { temperatureSchemas, humiditySchemas } = this.getDynamicSchemaCodes(); | ||
|
||
temperatureSchemas.forEach((schema, index) => { | ||
const serviceName = `Temperature Sensor ${index + 1}`; | ||
const serviceSubtype = `temperature_sensor_${index + 1}`; | ||
const service = | ||
this.accessory.getServiceById(this.Service.TemperatureSensor, serviceSubtype) || | ||
this.accessory.addService(this.Service.TemperatureSensor, serviceName, serviceSubtype); | ||
|
||
service | ||
.getCharacteristic(this.Characteristic.CurrentTemperature) | ||
.onGet(() => { | ||
const status = this.getStatus(schema.code); | ||
if (status) { | ||
const property = this.getSchema(schema.code)?.property as { scale: number }; | ||
const multiple = Math.pow(10, property.scale || 0); | ||
return Math.min(Math.max((status.value as number) / multiple, -100), 100); | ||
} | ||
return 0; // Default value if no status is found | ||
}); | ||
} | ||
|
||
private getDynamicSchemaCodes() { | ||
const temperatureSchemas: any[] = []; | ||
const humiditySchemas: any[] = []; | ||
|
||
this.device.schema.forEach((schema) => { | ||
if (schema.code.includes("ToutCh")) { | ||
temperatureSchemas.push(schema); | ||
} else if (schema.code.includes("HoutCh")) { | ||
humiditySchemas.push(schema); | ||
} | ||
}); | ||
|
||
humiditySchemas.forEach((schema, index) => { | ||
const serviceName = `Humidity Sensor ${index + 1}`; | ||
const serviceSubtype = `humidity_sensor_${index + 1}`; | ||
const service = | ||
this.accessory.getServiceById(this.Service.HumiditySensor, serviceSubtype) || | ||
this.accessory.addService(this.Service.HumiditySensor, serviceName, serviceSubtype); | ||
|
||
service | ||
.getCharacteristic(this.Characteristic.CurrentRelativeHumidity) | ||
.onGet(() => { | ||
const status = this.getStatus(schema.code); | ||
if (status) { | ||
return status.value as number; | ||
} | ||
return 0; // Default value if no status is found | ||
}); | ||
|
||
return { temperatureSchemas, humiditySchemas }; | ||
} | ||
}); | ||
} | ||
|
||
private getDynamicSchemaCodes() { | ||
const temperatureSchemas: { code: string }[] = []; | ||
const humiditySchemas: { code: string }[] = []; | ||
|
||
this.device.schema.forEach((schema) => { | ||
if (schema.code.includes('ToutCh')) { | ||
temperatureSchemas.push(schema); | ||
} else if (schema.code.includes('HoutCh')) { | ||
humiditySchemas.push(schema); | ||
} | ||
}); | ||
|
||
return { temperatureSchemas, humiditySchemas }; | ||
} | ||
} |