forked from tuya/tuya-homebridge
-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'mal' SecuritySystem support. (#246)
* Add support for 'mal' type as SecuritySystem * 1.7.0-beta.30 * Cleanup * Add device name * Remove onDeviceStatusUpdate since it is not needed. Implementation on SecuritySystemCurrentState already checks the sos_state. * Add fix for night mode. * Unify SecuritySystemCurrentState and SecuritySystemTargetState into SecuritySystemState * Update Supported Devices list
- Loading branch information
Showing
6 changed files
with
128 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import BaseAccessory from './BaseAccessory'; | ||
import { configureSecuritySystemCurrentState, configureSecuritySystemTargetState } from './characteristic/SecuritySystemState'; | ||
import { configureName } from './characteristic/Name'; | ||
|
||
const SCHEMA_CODE = { | ||
MASTER_MODE: ['master_mode'], | ||
SOS_STATE: ['sos_state'], | ||
}; | ||
|
||
export default class SecuritySystemAccessory extends BaseAccessory { | ||
|
||
requiredSchema() { | ||
return [SCHEMA_CODE.MASTER_MODE, SCHEMA_CODE.SOS_STATE]; | ||
} | ||
|
||
isNightArm = false; | ||
|
||
configureServices() { | ||
const service = this.accessory.getService(this.Service.SecuritySystem) | ||
|| this.accessory.addService(this.Service.SecuritySystem); | ||
|
||
configureName(this, service, this.device.name); | ||
|
||
configureSecuritySystemCurrentState(this, service, this.getSchema(...SCHEMA_CODE.MASTER_MODE), | ||
this.getSchema(...SCHEMA_CODE.SOS_STATE)); | ||
configureSecuritySystemTargetState(this, service, this.getSchema(...SCHEMA_CODE.MASTER_MODE), | ||
this.getSchema(...SCHEMA_CODE.SOS_STATE)); | ||
} | ||
} |
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,91 @@ | ||
import { Service } from 'homebridge'; | ||
import { TuyaDeviceSchema } from '../../device/TuyaDevice'; | ||
import BaseAccessory from '../BaseAccessory'; | ||
import SecuritySystemAccessory from '../SecuritySystemAccessory'; | ||
|
||
const TUYA_CODES = { | ||
MASTER_MODE: { | ||
ARMED: 'arm', | ||
DISARMED: 'disarmed', | ||
HOME: 'home', | ||
}, | ||
}; | ||
|
||
function getTuyaHomebridgeMap(accessory: BaseAccessory) { | ||
const tuyaHomebridgeMap = new Map(); | ||
|
||
tuyaHomebridgeMap.set(TUYA_CODES.MASTER_MODE.ARMED, accessory.Characteristic.SecuritySystemCurrentState.AWAY_ARM); | ||
tuyaHomebridgeMap.set(TUYA_CODES.MASTER_MODE.DISARMED, accessory.Characteristic.SecuritySystemCurrentState.DISARMED); | ||
tuyaHomebridgeMap.set(TUYA_CODES.MASTER_MODE.HOME, accessory.Characteristic.SecuritySystemCurrentState.STAY_ARM); | ||
tuyaHomebridgeMap.set(accessory.Characteristic.SecuritySystemCurrentState.AWAY_ARM, TUYA_CODES.MASTER_MODE.ARMED); | ||
tuyaHomebridgeMap.set(accessory.Characteristic.SecuritySystemCurrentState.DISARMED, TUYA_CODES.MASTER_MODE.DISARMED); | ||
tuyaHomebridgeMap.set(accessory.Characteristic.SecuritySystemCurrentState.STAY_ARM, TUYA_CODES.MASTER_MODE.HOME); | ||
tuyaHomebridgeMap.set(accessory.Characteristic.SecuritySystemCurrentState.NIGHT_ARM, TUYA_CODES.MASTER_MODE.HOME); | ||
|
||
return tuyaHomebridgeMap; | ||
} | ||
|
||
export function configureSecuritySystemCurrentState(accessory: SecuritySystemAccessory, service: Service, | ||
masterModeSchema?: TuyaDeviceSchema, sosStateSchema?: TuyaDeviceSchema) { | ||
if (!masterModeSchema || !sosStateSchema) { | ||
return; | ||
} | ||
|
||
const tuyaHomebridgeMap = getTuyaHomebridgeMap(accessory); | ||
|
||
service.getCharacteristic(accessory.Characteristic.SecuritySystemCurrentState) | ||
.onGet(() => { | ||
const alarmTriggered = accessory.getStatus(sosStateSchema.code)!.value; | ||
|
||
if (alarmTriggered) { | ||
return accessory.Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED; | ||
} else { | ||
const currentState = accessory.getStatus(masterModeSchema.code)!.value; | ||
if (currentState === TUYA_CODES.MASTER_MODE.HOME) { | ||
return accessory.isNightArm ? accessory.Characteristic.SecuritySystemCurrentState.NIGHT_ARM : | ||
accessory.Characteristic.SecuritySystemCurrentState.STAY_ARM; | ||
} | ||
|
||
return tuyaHomebridgeMap.get(currentState); | ||
} | ||
}); | ||
} | ||
|
||
export function configureSecuritySystemTargetState(accessory: SecuritySystemAccessory, service: Service, | ||
masterModeSchema?: TuyaDeviceSchema, sosStateSchema?: TuyaDeviceSchema) { | ||
if (!masterModeSchema || !sosStateSchema) { | ||
return; | ||
} | ||
|
||
const tuyaHomebridgeMap = getTuyaHomebridgeMap(accessory); | ||
|
||
service.getCharacteristic(accessory.Characteristic.SecuritySystemTargetState) | ||
.onGet(() => { | ||
const currentState = accessory.getStatus(masterModeSchema.code)!.value; | ||
if (currentState === TUYA_CODES.MASTER_MODE.HOME) { | ||
return accessory.isNightArm ? accessory.Characteristic.SecuritySystemCurrentState.NIGHT_ARM : | ||
accessory.Characteristic.SecuritySystemCurrentState.STAY_ARM; | ||
} | ||
|
||
return tuyaHomebridgeMap.get(currentState); | ||
}) | ||
.onSet(value => { | ||
|
||
const sosState = accessory.getStatus(sosStateSchema.code)?.value; | ||
|
||
// If we received a request to disarm the alarm, we make sure sos_state is set to false | ||
if (sosState && value === accessory.Characteristic.SecuritySystemTargetState.DISARM) { | ||
accessory.sendCommands([{ | ||
code: sosStateSchema.code, | ||
value: false, | ||
}], true); | ||
} | ||
|
||
accessory.isNightArm = value === accessory.Characteristic.SecuritySystemTargetState.NIGHT_ARM; | ||
|
||
accessory.sendCommands([{ | ||
code: masterModeSchema.code, | ||
value: tuyaHomebridgeMap.get(value), | ||
}], true); | ||
}); | ||
} |