Skip to content

Commit

Permalink
remove redundant accessories
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-godinez committed Aug 12, 2024
1 parent 0fcb428 commit e3b5936
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
45 changes: 14 additions & 31 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class OpenSaunaPlatform implements DynamicPlatformPlugin {
this.accessories.push(accessory);
}

// Modify the discoverDevices method to add a single accessory
discoverDevices() {
if (!isOpenSaunaConfig(this.config)) {
this.log.error(
Expand All @@ -51,58 +52,40 @@ export class OpenSaunaPlatform implements DynamicPlatformPlugin {
devices.saunaTimeout = devices.saunaTimeout ?? 60; // in minutes
devices.steamTimeout = devices.steamTimeout ?? 60; // in minutes
devices.saunaMaxTemperature =
devices.saunaMaxTemperature ?? (devices.temperatureUnitFahrenheit ? 212 : 100);
devices.saunaMaxTemperature ?? (devices.temperatureUnitFahrenheit ? 212 : 100);
devices.steamMaxTemperature =
devices.steamMaxTemperature ?? (devices.temperatureUnitFahrenheit ? 140 : 60);
devices.steamMaxTemperature ?? (devices.temperatureUnitFahrenheit ? 140 : 60);
devices.steamMaxHumidity = devices.steamMaxHumidity ?? 60; // in percent
devices.saunaSafetyTemperature = devices.saunaSafetyTemperature ?? (devices.temperatureUnitFahrenheit ? 248 : 120);
devices.steamSafetyTemperature = devices.steamSafetyTemperature ?? (devices.temperatureUnitFahrenheit ? 140 : 60);
devices.controllerSafetyTemperature = devices.controllerSafetyTemperature ?? (devices.temperatureUnitFahrenheit ? 194 : 90);

// Conditionally add Sauna accessory
if (devices.hasSauna) {
this.addAccessory(devices, 'sauna');
}

// Conditionally add Steam accessory
if (devices.hasSteam) {
this.addAccessory(devices, 'steam');
}

// Add Light and Fan accessories if they exist
if (devices.hasLight) {
this.addAccessory(devices, 'light');
}

if (devices.hasFan) {
this.addAccessory(devices, 'fan');
}
// Add a single accessory for all configured devices
this.addAccessory(devices);
}

private addAccessory(
devices: OpenSaunaConfig,
type: 'sauna' | 'steam' | 'light' | 'fan',
) {
// Generate a unique UUID for each accessory
const uuid = this.api.hap.uuid.generate(`${devices.name}-${type}`);
// Adjust addAccessory to handle a single accessory with all features
private addAccessory(devices: OpenSaunaConfig) {
// Generate a unique UUID for the combined accessory
const uuid = this.api.hap.uuid.generate(devices.name);
const existingAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid,
);

if (existingAccessory) {
// The accessory already exists, update it
// The accessory already exists, update it
this.log.info(
'Restoring existing accessory from cache:',
existingAccessory.displayName,
);
new OpenSaunaAccessory(this, existingAccessory, devices, type);
new OpenSaunaAccessory(this, existingAccessory, devices);
} else {
// Create a new accessory
this.log.info('Adding new accessory:', devices.name, type);
// Create a new accessory
this.log.info('Adding new accessory:', devices.name);
const accessory = new this.api.platformAccessory(devices.name, uuid);

// Create the accessory handler
new OpenSaunaAccessory(this, accessory, devices, type);
new OpenSaunaAccessory(this, accessory, devices);

// Register the accessory
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
Expand Down
10 changes: 0 additions & 10 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class OpenSaunaAccessory {
private readonly platform: OpenSaunaPlatform,
private readonly accessory: PlatformAccessory,
private readonly config: OpenSaunaConfig,
private readonly accessoryType: 'sauna' | 'steam' | 'light' | 'fan',
) {
// Set the accessory information
this.accessory
Expand All @@ -52,15 +51,6 @@ export class OpenSaunaAccessory {
'58008',
);

// Set the accessory's name
this.accessory.displayName = `${config.name} ${accessoryType}`;
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(
this.platform.Characteristic.Name,
this.accessory.displayName,
);

// Initialize RPIO with desired options
rpio.init({
mapping: 'gpio', // Use GPIO pin numbering
Expand Down

0 comments on commit e3b5936

Please sign in to comment.