Skip to content

Commit

Permalink
fix: code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyberends committed Nov 14, 2024
1 parent 62aca1d commit b1ce2bf
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/loxone/items/NfcCodeTouch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ import { MotionSensor } from '../../homekit/services/MotionSensor';

/**
* NfcCodeTouch Item
*/
*/
type NfcCodeTouchMapping = 'DoorBell' | 'MotionSensor';

export class NfcCodeTouch extends LoxoneAccessory {

configureServices(): void {
const mapping = this.platform.config.Advanced.NfcCodeTouchMapping;
const mapping: NfcCodeTouchMapping = this.platform.config.Advanced.NfcCodeTouchMapping;
this.platform.log.debug('Configuring NfcCodeTouch with mapping:', mapping);

if (mapping === 'DoorBell') {
this.initializeDoorbellService();
} else {
this.initializeMotionSensorService();
}
}

private initializeDoorbellService(): void {
this.ItemStates = {
[this.device.states.events]: { 'service': 'PrimaryService', 'state': 'bell' },
};
this.Service.PrimaryService = new Doorbell(this.platform, this.Accessory!);
this.platform.log.debug('Doorbell service initialized.');
// Dynamically initialize the service based on the mapping
this.initializeService(mapping);
}

private initializeMotionSensorService(): void {
private initializeService(mapping: NfcCodeTouchMapping): void {
const serviceClass = mapping === 'DoorBell' ? Doorbell : MotionSensor;
this.ItemStates = {
[this.device.states.events]: { 'service': 'PrimaryService', 'state': 'MotionDetected' },
[this.device.states.events]: { 'service': 'PrimaryService', 'state': 'events' },
};
this.Service.PrimaryService = new MotionSensor(this.platform, this.Accessory!);
this.platform.log.debug('MotionSensor service initialized.');
this.Service.PrimaryService = new serviceClass(this.platform, this.Accessory!);
this.platform.log.debug(`${serviceClass.name} service initialized.`);
}
}

0 comments on commit b1ce2bf

Please sign in to comment.