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 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix/buffer-error * wip check power * cleanup/code * refactor/hub-child-commands * refactor/prepare-for-klap-protocol * feat/contact-sensor * docs/update * feat/p110-p115-power-usage * feat/add-low-battery-button * version
- Loading branch information
Nicolae-Rares Ailincai
authored
Sep 5, 2023
1 parent
6645f44
commit 703eda4
Showing
17 changed files
with
626 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,5 @@ | |
} | ||
} | ||
}, | ||
"cSpell.words": ["Tapo"] | ||
"cSpell.words": ["KLAP", "Tapo"] | ||
} |
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
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/Button/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
18 changes: 18 additions & 0 deletions
18
src/accessories/Contact/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,114 @@ | ||
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'; | ||
|
||
export enum Status { | ||
KeepOpen = 'keepOpen', | ||
Closed = 'close', | ||
Open = 'open' | ||
} | ||
|
||
export type AccessoryThisType = ThisType<{ | ||
readonly hub: HubAccessory; | ||
readonly getInfo: () => Promise<ChildInfo>; | ||
}>; | ||
|
||
export default class ContactAccessory 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.ContactSensor) || | ||
this.accessory.addService(this.platform.Service.ContactSensor); | ||
|
||
const characteristic = service.getCharacteristic( | ||
this.platform.Characteristic.ContactSensorState | ||
); | ||
|
||
service | ||
.getCharacteristic(this.platform.Characteristic.StatusLowBattery) | ||
.onGet(StatusLowBattery.get.bind(this)); | ||
|
||
const checkStatus = async (initStatus?: Status) => { | ||
try { | ||
if (initStatus) { | ||
characteristic.updateValue(this.statusToValue(initStatus)); | ||
} | ||
|
||
const response = await this.hub.getChildLogs(this.deviceInfo.device_id); | ||
const lastEvent = response?.logs?.[0]; | ||
if (this.lastEventUpdate < lastEvent?.timestamp) { | ||
this.lastEventUpdate = lastEvent?.timestamp ?? 0; | ||
characteristic.updateValue(this.statusToValue(lastEvent?.event)); | ||
} | ||
} catch (error) { | ||
this.log.error('Failed to check for updates', error); | ||
await delay(500); | ||
} | ||
|
||
checkStatus(); | ||
}; | ||
|
||
this.setup((x) => checkStatus(x)); | ||
} | ||
|
||
cleanup() { | ||
clearInterval(this.interval!); | ||
} | ||
|
||
private async setup(callback: (x: Status) => void) { | ||
const init = await this.hub.getChildLogs(this.deviceInfo.device_id); | ||
const initEvent = init?.logs?.[0]; | ||
this.lastEventUpdate = initEvent?.timestamp ?? 0; | ||
callback(initEvent?.event ?? Status.KeepOpen); | ||
} | ||
|
||
private statusToValue(status: Status) { | ||
switch (status) { | ||
case Status.Open: | ||
case Status.KeepOpen: | ||
return this.platform.Characteristic.ContactSensorState | ||
.CONTACT_NOT_DETECTED; | ||
default: | ||
return this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
CharacteristicGetHandler, | ||
CharacteristicValue, | ||
Nullable | ||
} from 'homebridge'; | ||
|
||
import { AccessoryThisType } from '..'; | ||
|
||
const characteristic: { | ||
get: CharacteristicGetHandler; | ||
} & AccessoryThisType = { | ||
get: async function (): Promise<Nullable<CharacteristicValue>> { | ||
const response = await this.tpLink.cacheSendCommand( | ||
this.mac, | ||
'getCurrentPower' | ||
); | ||
|
||
return response.current_power > 0; | ||
} | ||
}; | ||
|
||
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
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,50 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { Logger } from 'homebridge'; | ||
import crypto from 'crypto'; | ||
|
||
import TpLinkCipher from '../TpLinkCipher'; | ||
|
||
abstract class API { | ||
protected readonly terminalUUID: string; | ||
|
||
protected loginToken?: string; | ||
|
||
constructor( | ||
protected readonly ip: string, | ||
protected readonly email: string, | ||
protected readonly password: string, | ||
protected readonly log: Logger | ||
) { | ||
this.email = TpLinkCipher.toBase64(TpLinkCipher.encodeUsername(this.email)); | ||
this.password = TpLinkCipher.toBase64(this.password); | ||
this.terminalUUID = crypto.randomUUID(); | ||
} | ||
|
||
public abstract login(): Promise<void>; | ||
|
||
public abstract setup(): Promise<void>; | ||
|
||
public abstract sendRequest( | ||
method: string, | ||
params: { | ||
[key: string]: any; | ||
}, | ||
setCookie: boolean | ||
): Promise<AxiosResponse<any, any>>; | ||
|
||
public abstract sendSecureRequest( | ||
method: string, | ||
params: { | ||
[key: string]: any; | ||
}, | ||
useToken: boolean, | ||
forceHandshake: boolean | ||
): Promise<{ | ||
body: any; | ||
response: AxiosResponse<any, any>; | ||
}>; | ||
|
||
public abstract needsNewHandshake(): boolean; | ||
} | ||
|
||
export default API; |
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,6 @@ | ||
enum Protocol { | ||
Legacy, | ||
KLAP | ||
} | ||
|
||
export default Protocol; |
Oops, something went wrong.