Skip to content

Commit

Permalink
Remove duplicate accessories and services. Now a single accessory w/ …
Browse files Browse the repository at this point in the history
…multiple services.
  • Loading branch information
luis-godinez committed Aug 13, 2024
1 parent 0499a65 commit 8f74f18
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/safety.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('OpenSaunaAccessory Safety Tests', () => {
addService: jest.fn().mockImplementation(() => mockSwitchService),
} as unknown as PlatformAccessory;

saunaAccessory = new OpenSaunaAccessory(platform, accessory, saunaConfig, 'sauna');
saunaAccessory = new OpenSaunaAccessory(platform, accessory, saunaConfig);
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/sauna.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('OpenSaunaAccessory Sauna Tests', () => {
})),
} as unknown as PlatformAccessory;

saunaAccessory = new OpenSaunaAccessory(platform, accessory, saunaConfig, 'sauna');
saunaAccessory = new OpenSaunaAccessory(platform, accessory, saunaConfig);

// Mocking methods if they do not exist
(saunaAccessory as any).handleDoorStateChange = (doorType: string, doorOpen: boolean) => {

Check failure on line 105 in src/__tests__/sauna.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

Check failure on line 105 in src/__tests__/sauna.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
Expand Down
35 changes: 8 additions & 27 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,13 @@ export class OpenSaunaPlatform implements DynamicPlatformPlugin {
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 with all configured services
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}`);
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,
);
Expand All @@ -95,14 +76,14 @@ export class OpenSaunaPlatform implements DynamicPlatformPlugin {
'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);
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
213 changes: 116 additions & 97 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class OpenSaunaAccessory {
private readonly platform: OpenSaunaPlatform,
private readonly accessory: PlatformAccessory,
private readonly config: OpenSaunaConfig,
private readonly accessoryType: 'sauna' | 'steam' | 'light' | 'fan',
) {
// Initialize RPIO with desired options
rpio.init({
Expand Down Expand Up @@ -65,7 +64,7 @@ export class OpenSaunaAccessory {
}
});

// Initialize all necessary services based on the type of accessory
// Initialize all necessary services based on the config
this.setupAccessory();

// Ensure GPIO pins are cleaned up on process exit
Expand Down Expand Up @@ -141,81 +140,95 @@ export class OpenSaunaAccessory {
this.initializeGpioPins();

// Setup switches
this.saunaPowerSwitch =
this.accessory.getService('Sauna Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Sauna Power',
'sauna-power',
);
this.saunaPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleSaunaPowerSet.bind(this));
if (this.config.hasSauna) {
this.saunaPowerSwitch =
this.accessory.getService('Sauna Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Sauna Power',
'sauna-power',
);
this.saunaPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleSaunaPowerSet.bind(this));
}

this.steamPowerSwitch =
this.accessory.getService('Steam Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Steam Power',
'steam-power',
);
this.steamPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleSteamPowerSet.bind(this));
if (this.config.hasSteam) {
this.steamPowerSwitch =
this.accessory.getService('Steam Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Steam Power',
'steam-power',
);
this.steamPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleSteamPowerSet.bind(this));
}

this.lightPowerSwitch =
this.accessory.getService('Light Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Light Power',
'light-power',
);
this.lightPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleLightPowerSet.bind(this));
if (this.config.hasLight) {
this.lightPowerSwitch =
this.accessory.getService('Light Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Light Power',
'light-power',
);
this.lightPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleLightPowerSet.bind(this));
}

this.fanPowerSwitch =
this.accessory.getService('Fan Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Fan Power',
'fan-power',
);
this.fanPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleFanPowerSet.bind(this));
if (this.config.hasFan) {
this.fanPowerSwitch =
this.accessory.getService('Fan Power') ||
this.accessory.addService(
this.platform.Service.Switch,
'Fan Power',
'fan-power',
);
this.fanPowerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(this.handleFanPowerSet.bind(this));
}

// Setup thermostats
this.saunaThermostat =
this.accessory.getService('Sauna Thermostat') ||
this.accessory.addService(
this.platform.Service.Thermostat,
'Sauna Thermostat',
'sauna-thermostat',
);
this.saunaThermostat
.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onSet(this.handleSaunaTargetTemperatureSet.bind(this));
if (this.config.hasSauna) {
this.saunaThermostat =
this.accessory.getService('Sauna Thermostat') ||
this.accessory.addService(
this.platform.Service.Thermostat,
'Sauna Thermostat',
'sauna-thermostat',
);
this.saunaThermostat
.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onSet(this.handleSaunaTargetTemperatureSet.bind(this));
}

this.steamThermostat =
this.accessory.getService('Steam Thermostat') ||
this.accessory.addService(
this.platform.Service.Thermostat,
'Steam Thermostat',
'steam-thermostat',
);
this.steamThermostat
.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onSet(this.handleSteamTargetTemperatureSet.bind(this));
if (this.config.hasSteam) {
this.steamThermostat =
this.accessory.getService('Steam Thermostat') ||
this.accessory.addService(
this.platform.Service.Thermostat,
'Steam Thermostat',
'steam-thermostat',
);
this.steamThermostat
.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onSet(this.handleSteamTargetTemperatureSet.bind(this));
}

// Setup temperature sensors
this.saunaTemperatureSensor =
this.accessory.getService('Sauna Temperature') ||
this.accessory.addService(
this.platform.Service.TemperatureSensor,
'Sauna Temperature',
'sauna-temperature',
);
if (this.config.hasSauna) {
this.saunaTemperatureSensor =
this.accessory.getService('Sauna Temperature') ||
this.accessory.addService(
this.platform.Service.TemperatureSensor,
'Sauna Temperature',
'sauna-temperature',
);
}

this.pcbTemperatureSensor =
this.accessory.getService('PCB Temperature') ||
Expand Down Expand Up @@ -243,38 +256,44 @@ export class OpenSaunaAccessory {
});

// Setup steam temperature and humidity sensors
this.steamTemperatureSensor =
this.accessory.getService('Steam Temperature') ||
this.accessory.addService(
this.platform.Service.TemperatureSensor,
'Steam Temperature',
'steam-temperature',
);
if (this.config.hasSteam) {
this.steamTemperatureSensor =
this.accessory.getService('Steam Temperature') ||
this.accessory.addService(
this.platform.Service.TemperatureSensor,
'Steam Temperature',
'steam-temperature',
);

this.steamHumiditySensor =
this.accessory.getService('Steam Humidity') ||
this.accessory.addService(
this.platform.Service.HumiditySensor,
'Steam Humidity',
'steam-humidity',
);
this.steamHumiditySensor =
this.accessory.getService('Steam Humidity') ||
this.accessory.addService(
this.platform.Service.HumiditySensor,
'Steam Humidity',
'steam-humidity',
);
}

// Setup door sensors
this.saunaDoorSensor =
this.accessory.getService('Sauna Door') ||
this.accessory.addService(
this.platform.Service.ContactSensor,
'Sauna Door',
'sauna-door',
);
if (this.config.hasSauna) {
this.saunaDoorSensor =
this.accessory.getService('Sauna Door') ||
this.accessory.addService(
this.platform.Service.ContactSensor,
'Sauna Door',
'sauna-door',
);
}

this.steamDoorSensor =
this.accessory.getService('Steam Door') ||
this.accessory.addService(
this.platform.Service.ContactSensor,
'Steam Door',
'steam-door',
);
if (this.config.hasSteam) {
this.steamDoorSensor =
this.accessory.getService('Steam Door') ||
this.accessory.addService(
this.platform.Service.ContactSensor,
'Steam Door',
'steam-door',
);
}

// Monitor temperatures, humidity, and door states
this.monitorTemperatures();
Expand Down

0 comments on commit 8f74f18

Please sign in to comment.