Skip to content

Commit

Permalink
Update WeatherStationAccessory.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanBrotherton authored Aug 30, 2024
1 parent 8539193 commit 29c0ba6
Showing 1 changed file with 55 additions and 74 deletions.
129 changes: 55 additions & 74 deletions src/accessory/WeatherStationAccessory.ts
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 };
}
}

0 comments on commit 29c0ba6

Please sign in to comment.