This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for motion sensor T100
- Loading branch information
1 parent
4a9e220
commit 80bae06
Showing
6 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/accessories/MotionSensor/characteristics/StatusActive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
CharacteristicGetHandler, | ||
CharacteristicValue, | ||
Nullable | ||
} from 'homebridge'; | ||
|
||
import { AccessoryThisType } from '..'; | ||
|
||
const characteristic: { | ||
get: CharacteristicGetHandler; | ||
} & AccessoryThisType = { | ||
get: async function (): Promise<Nullable<CharacteristicValue>> { | ||
const deviceInfo = await this.getInfo(); | ||
return deviceInfo.status === 'online'; | ||
} | ||
}; | ||
|
||
export default characteristic; |
18 changes: 18 additions & 0 deletions
18
src/accessories/MotionSensor/characteristics/StatusLowBattery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
CharacteristicGetHandler, | ||
CharacteristicValue, | ||
Nullable | ||
} from 'homebridge'; | ||
|
||
import { AccessoryThisType } from '..'; | ||
|
||
const characteristic: { | ||
get: CharacteristicGetHandler; | ||
} & AccessoryThisType = { | ||
get: async function (): Promise<Nullable<CharacteristicValue>> { | ||
const deviceInfo = await this.getInfo(); | ||
return deviceInfo.at_low_battery; | ||
} | ||
}; | ||
|
||
export default characteristic; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { PlatformAccessory, Logger } from 'homebridge'; | ||
|
||
import { ChildInfo } from '../../api/@types/ChildListInfo'; | ||
import HubAccessory, { HubContext } from '../Hub'; | ||
import Accessory from '../../@types/Accessory'; | ||
import Context from '../../@types/Context'; | ||
import Platform from '../../platform'; | ||
import delay from '../../utils/delay'; | ||
|
||
import StatusLowBattery from './characteristics/StatusLowBattery'; | ||
import StatusActive from './characteristics/StatusActive'; | ||
|
||
export type AccessoryThisType = ThisType<{ | ||
readonly hub: HubAccessory; | ||
readonly getInfo: () => Promise<ChildInfo>; | ||
}>; | ||
|
||
type State = { | ||
detected: boolean; | ||
active: boolean; | ||
}; | ||
|
||
export default class MotionSensorAccessory extends Accessory { | ||
private interval?: NodeJS.Timeout; | ||
private lastEventUpdate = 0; | ||
|
||
public get UUID() { | ||
return this.accessory.UUID.toString(); | ||
} | ||
|
||
private getInfo() { | ||
return this.hub.getChildInfo(this.deviceInfo.device_id); | ||
} | ||
|
||
constructor( | ||
private readonly hub: HubAccessory, | ||
platform: Platform, | ||
accessory: PlatformAccessory<HubContext>, | ||
log: Logger, | ||
deviceInfo: ChildInfo | ||
) { | ||
super( | ||
platform, | ||
accessory as unknown as PlatformAccessory<Context>, | ||
log, | ||
deviceInfo | ||
); | ||
|
||
this.accessory | ||
.getService(this.platform.Service.AccessoryInformation)! | ||
.setCharacteristic( | ||
this.platform.Characteristic.Manufacturer, | ||
'TP-Link Technologies' | ||
) | ||
.setCharacteristic(this.platform.Characteristic.Model, this.model) | ||
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.mac); | ||
|
||
const service = | ||
this.accessory.getService(this.platform.Service.MotionSensor) || | ||
this.accessory.addService(this.platform.Service.MotionSensor); | ||
|
||
const motionDetected = service.getCharacteristic( | ||
this.platform.Characteristic.MotionDetected | ||
); | ||
const isActive = service.getCharacteristic( | ||
this.platform.Characteristic.StatusActive) | ||
.onGet(StatusActive.get.bind(this)); | ||
|
||
service | ||
.getCharacteristic(this.platform.Characteristic.StatusLowBattery) | ||
.onGet(StatusLowBattery.get.bind(this)); | ||
|
||
const checkStatus = async (initStatus?: State) => { | ||
if (initStatus) { | ||
motionDetected.updateValue(initStatus.detected); | ||
isActive.updateValue(initStatus.active); | ||
} | ||
|
||
try { | ||
const response = await this.getInfo(); | ||
if (!response) { | ||
this.log.warn('Failed to check for updates, delaying 500ms'); | ||
await delay(500); | ||
} | ||
|
||
motionDetected.updateValue(response.detected); | ||
} catch (error) { | ||
this.log.error('Failed to check for updates', error); | ||
await delay(500); | ||
} | ||
}; | ||
|
||
this.setup(checkStatus.bind(this)); | ||
} | ||
|
||
cleanup() { | ||
clearInterval(this.interval!); | ||
} | ||
|
||
private async setup(callback: (x?: State) => Promise<void>) { | ||
const init = await this.getInfo(); | ||
|
||
await callback({ | ||
detected: init.detected, | ||
active: init.status === 'online' | ||
}); | ||
this.interval = setInterval(() => { | ||
callback(); | ||
}, 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters