Skip to content

Commit

Permalink
add more sauna tests.
Browse files Browse the repository at this point in the history
luis-godinez committed Aug 14, 2024
1 parent 3e2f092 commit cca8239
Showing 3 changed files with 63 additions and 7 deletions.
32 changes: 31 additions & 1 deletion src/__tests__/sauna.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTestPlatformAndAccessory, saunaConfig } from './setup';
import { mockDigitalWrite } from '../jest.setup';
import { mockDigitalWrite, mockRead } from '../jest.setup';

import { OpenSaunaAccessory } from '../platformAccessory';
import { OpenSaunaPlatform } from '../platform';
@@ -23,6 +23,36 @@ describe('OpenSaunaAccessory Sauna Test', () => {
jest.clearAllTimers();
});

it('should turn on sauna when in HEAT mode and sauna is not running', () => {
mockRead.mockReturnValue(0); // Mock sauna not running

(saunaAccessory as any).handleSaunaTargetTemperatureSet(75);

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

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

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

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

saunaConfig.gpioPins.saunaPowerPins.forEach((pin: number) => {
expect(mockDigitalWrite).toHaveBeenCalledWith(pin, 1);
});
});

it('should not turn on sauna if already running', () => {
mockRead.mockReturnValue(1); // Mock sauna already running

(saunaAccessory as any).handleSaunaTargetTemperatureSet(75);

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

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

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

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

expect(mockDigitalWrite).not.toHaveBeenCalled();
});

it('should not turn off sauna if already off', () => {
mockRead.mockReturnValue(0); // Mock sauna already off
const thermostatService = accessory.getService('sauna-thermostat');
if (thermostatService) {
(thermostatService.getCharacteristic(platform.Characteristic.TargetHeatingCoolingState).value as number) = 0; // Set mode to OFF

(saunaAccessory as any).handleSaunaTargetTemperatureSet(0);

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

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

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

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

expect(mockDigitalWrite).not.toHaveBeenCalled();
}
});

test('should keep sauna heater on when door opens if saunaOnWhileDoorOpen is true', () => {
// Update configuration to ensure sauna stays on when the door is open
saunaConfig.saunaOnWhileDoorOpen = true;
1 change: 1 addition & 0 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ const mockCharacteristic = {
onSet: jest.fn(),
updateCharacteristic: jest.fn(),
updateValue: jest.fn(),
setValue: jest.fn().mockReturnThis(),
};

const mockTemperatureSensorService = {
37 changes: 31 additions & 6 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
@@ -417,13 +417,25 @@ export class OpenSaunaAccessory {
.value;

if (currentMode === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
// Keep the target temperature consistent and toggle sauna power
this.handleSaunaPowerSet(true);
if (!this.isSaunaRunning()) {
this.platform.log.info('Turning sauna ON due to HEAT state.');
this.handleSaunaPowerSet(true);
}
} else if (currentMode === this.platform.Characteristic.TargetHeatingCoolingState.OFF) {
this.handleSaunaPowerSet(false);
if (this.isSaunaRunning()) {
this.platform.log.info('Turning sauna OFF due to OFF state.');
this.handleSaunaPowerSet(false);
}
} else {
this.platform.log.warn('Unexpected sauna mode:', currentMode);
}
}

// Ensure this helper method correctly checks the state of the sauna
private isSaunaRunning(): boolean {
return rpio.read(this.config.gpioPins.saunaPowerPins[0]) === rpio.HIGH;
}

private handleSteamTargetTemperatureSet(value: CharacteristicValue) {
this.platform.log.info('Steam Target Temperature set to:', value);

@@ -432,14 +444,27 @@ export class OpenSaunaAccessory {
?.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
.value;

// Only attempt to turn on if not already running and set to HEAT
if (currentMode === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
// Keep the target temperature consistent and toggle steam power
this.handleSteamPowerSet(true);
if (!this.isSteamRunning()) {
this.platform.log.info('Turning steam ON due to HEAT state.');
this.handleSteamPowerSet(true);
}
} else if (currentMode === this.platform.Characteristic.TargetHeatingCoolingState.OFF) {
this.handleSteamPowerSet(false);
if (this.isSteamRunning()) {
this.platform.log.info('Turning steam OFF due to OFF state.');
this.handleSteamPowerSet(false);
}
}else {
this.platform.log.warn('Unexpected sauna mode:', currentMode);
}
}

// Helper method to determine if the steam system is currently running
private isSteamRunning(): boolean {
return rpio.read(this.config.gpioPins.steamPowerPins[0]) === rpio.HIGH;
}

// Start a system with timeout logic
private startSystem(
system: 'sauna' | 'steam',

0 comments on commit cca8239

Please sign in to comment.