Skip to content

Commit

Permalink
Improve motion sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed Apr 15, 2024
1 parent fc03449 commit 9566380
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/EltakoMotionAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { IUpdatableAccessory } from './IUpdatableAccessory';

export class EltakoMotionAccessory implements IUpdatableAccessory {
private service: Service;
private hasOnOffState: boolean;

constructor(
private readonly platform: EltakoMiniSafe2Platform,
public readonly accessory: PlatformAccessory,
) {
const deviceType = accessory.context.device.info.data;
this.hasOnOffState = deviceType === 'eltako_motion' || deviceType === 'eltako_motion2'; // not eltako_tf_motion

this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.context.device.info.vendor)
.setCharacteristic(this.platform.Characteristic.Model, accessory.context.device.info.data)
Expand All @@ -24,29 +20,17 @@ export class EltakoMotionAccessory implements IUpdatableAccessory {

this.service.getCharacteristic(this.platform.Characteristic.MotionDetected)
.onGet(this.getMotionDetected.bind(this));

if (this.hasOnOffState) {
this.service.getCharacteristic(this.platform.Characteristic.StatusActive)
.onGet(this.getStatusActive.bind(this));
}
}

getMotionDetected(): CharacteristicValue {
const state = this.platform.deviceStateCache.find(s => s.sid === this.accessory.context.device.info.sid);
return state?.state?.motion === 'true';
}

getStatusActive(): CharacteristicValue {
const state = this.platform.deviceStateCache.find(s => s.sid === this.accessory.context.device.info.sid);
return state?.state?.state === 'on';
// eltako_tf_motion uses motion=true
// eltako_motion and eltako_motion2 use state=on
return state?.state?.motion === 'true' || state?.state?.state === 'on';
}

update() {

this.service.getCharacteristic(this.platform.Characteristic.MotionDetected).updateValue(this.getMotionDetected());

if (this.hasOnOffState) {
this.service.getCharacteristic(this.platform.Characteristic.StatusActive).updateValue(this.getStatusActive());
}
}
}

0 comments on commit 9566380

Please sign in to comment.