| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
- import { accessory } from './accessory'
- import { device } from './device'
- import { httpRequest, response } from './httpPromise'
- import { domoticaPlatform } from './platform'
- export class doorWindowSensorAccessory extends accessory {
- private service: Service
- private state = {
- ContactSensorState: this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED,
- LowBattery: false,
- Active: false
- }
- static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
- return new Promise((resolve, reject) => {
- let doorWindowSensorDevices: device[] = []
- httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/doorwindowsensor')
- .then((response) => {
- let devices = JSON.parse(response.body)
- for (const device of devices) {
- if (device.enabled == "true") {
- let object: device = {
- type: "DoorWindowSensor",
- typeId: device.id,
- uniqueId: "DoorWindowSensor." + device.id,
- deviceName: device.name,
- switchable: "false",
- dimmable: "false",
- detailedType: device.type
- }
- doorWindowSensorDevices.push(object)
- }
- }
- resolve(doorWindowSensorDevices)
- })
- .catch((error) => {
- reject('doorWindowSensorAccessory::discoverDevices Error ->' + error)
- })
- })
- }
- constructor(
- private readonly platform: domoticaPlatform,
- private readonly accessory: PlatformAccessory,
- ) {
- super("DoorWindowSensor", accessory.context.device.typeId)
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi')
- .setCharacteristic(this.platform.Characteristic.Model, 'MCCGQ01LM')
- .setCharacteristic(this.platform.Characteristic.SerialNumber, '04000' + accessory.context.device.typeId)
- if (accessory.context.device.detailedType == "door") {
- this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor)
- } else { // window
- this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor)
- }
- this.service.getCharacteristic(this.platform.Characteristic.ContactSensorState)
- .onGet(this.getContactSensorState.bind(this));
- this.service.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
- .onGet(this.getLowBattery.bind(this))
-
- this.service.getCharacteristic(this.platform.Characteristic.StatusActive)
- .onGet(this.getActive.bind(this))
- this.setName(accessory.context.device.deviceName)
- this.update()
- }
- setName(name) {
- this.service.setCharacteristic(this.platform.Characteristic.Name, name)
- }
- setValue(key, value) {
- if (key == "state" && value != "") {
- if (value == "Off") {
- this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
- } else {
- this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
- }
- this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState)
- }
- else if (key == "battery" && value != "") {
- if (value < 25) {
- this.state.LowBattery = true
- } else {
- this.state.LowBattery = false
- }
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
- }
- else if (key == "online" && value != "") {
- if (value == "false") {
- this.state.Active = false
- } else {
- this.state.Active = true
- }
- this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
- }
- }
- update() {
- const data = JSON.stringify([{
- type: 'doorwindow',
- query: 'state'
- }])
- httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
- .then((response) => {
- const devices = JSON.parse(response.body)
- if (devices.hasOwnProperty('doorwindow')) {
- for (const device of devices.doorwindow) {
- if (device.id == this.accessory.context.device.typeId) {
- if (device.state == 'closed') {
- this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED
- } else { // open
- this.state.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
- }
- if (device.battery < 25) {
- this.state.LowBattery = true
- } else {
- this.state.LowBattery = false
- }
- if (device.online == "false") {
- this.state.Active = false
- } else {
- this.state.Active = true
- }
- this.service.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.state.ContactSensorState)
- this.service.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.state.LowBattery)
- this.service.updateCharacteristic(this.platform.Characteristic.StatusActive, this.state.Active)
- break
- }
- }
- }
- })
- .catch((error) => {
- this.platform.log.debug('doorWindowSensorAccessory::update Error ->' + error)
- })
- }
- getContactSensorState(): CharacteristicValue {
- return this.state.ContactSensorState
- }
- getLowBattery(): CharacteristicValue {
- return this.state.LowBattery
- }
- getActive(): CharacteristicValue {
- return this.state.Active
- }
- }
|