Skip to content

Commit

Permalink
Weather station support (#495)
Browse files Browse the repository at this point in the history
* Update AccessoryFactory.ts

* Weather Station Accessory

* Update AccessoryFactory.ts

* Update WeatherStationAccessory.ts

* Fix import
  • Loading branch information
TristanBrotherton authored Sep 2, 2024
1 parent db51db3 commit 4427ad1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/accessory/AccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import IRGenericAccessory from './IRGenericAccessory';
import IRAirConditionerAccessory from './IRAirConditionerAccessory';
import SecuritySystemAccessory from './SecuritySystemAccessory';
import VibrationSensorAccessory from './VibrationSensorAccessory';
import WeatherStationAccessory from './WeatherStationAccessory';
import DoorbellAccessory from './DoorbellAccessory';
import PetFeederAccessory from './PetFeederAccessory';

Expand Down Expand Up @@ -197,6 +198,9 @@ export default class AccessoryFactory {
case 'wxml':
handler = new DoorbellAccessory(platform, accessory);
break;
case 'qxj':
handler = new WeatherStationAccessory(platform, accessory);
break;

// Other
case 'scene':
Expand Down
60 changes: 60 additions & 0 deletions src/accessory/WeatherStationAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 { 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
});
});

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
});
});
}

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 4427ad1

Please sign in to comment.