Skip to content

Commit

Permalink
Add support for motion sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed Apr 15, 2024
1 parent 38387b3 commit 5b85366
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/EltakoMotionAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
import { EltakoMiniSafe2Platform } from './platform';
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)
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.context.device.info.address);

// https://developers.homebridge.io/#/service/MotionSensor
const serviceType = this.platform.Service.MotionSensor;
this.service = this.accessory.getService(serviceType) || this.accessory.addService(serviceType);

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';
}

update() {

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

if (this.hasOnOffState) {
this.service.getCharacteristic(this.platform.Characteristic.StatusActive).updateValue(this.getStatusActive());
}
}
}
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class DeviceState {
state?: string;
operation_mode?: string;
contact?: string;
motion?: string;
}

export interface Device {
Expand Down
13 changes: 13 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig,
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
import { EltakoBlindsAccessory } from './EltakoBlindsAccessory';
import { EltakoContactAccessory } from './EltakoContactAccessory';
import { EltakoMotionAccessory } from './EltakoMotionAccessory';
import { EltakoDimmerAccessory } from './EltakoDimmerAccessory';
import { EltakoLightSensorAccessory } from './EltakoLightSensorAccessory';
import { EltakoSwitchAccessory } from './EltakoSwitchAccessory';
Expand Down Expand Up @@ -185,6 +186,12 @@ export class EltakoMiniSafe2Platform implements DynamicPlatformPlugin {
instance = new EltakoContactAccessory(this, existingAccessory);
break;
}
case 'eltako_motion':
case 'eltako_tf_motion':
case 'eltako_motion2': {
instance = new EltakoMotionAccessory(this, existingAccessory);
break;
}
case 'eltako_switch':
case 'eltako_tf_switch':
case 'eltako_fsr14': {
Expand Down Expand Up @@ -250,6 +257,12 @@ export class EltakoMiniSafe2Platform implements DynamicPlatformPlugin {
instance = new EltakoContactAccessory(this, accessory);
break;
}
case 'eltako_motion':
case 'eltako_tf_motion':
case 'eltako_motion2': {
instance = new EltakoMotionAccessory(this, accessory);
break;
}
case 'eltako_switch':
case 'eltako_tf_switch':
case 'eltako_fsr14': {
Expand Down

0 comments on commit 5b85366

Please sign in to comment.