added humidity sensor

This commit is contained in:
Chester 2022-03-08 15:13:53 +01:00
parent 6b46c9cdb8
commit 3561e0c3d6
3 changed files with 122 additions and 18 deletions

View File

@ -12,23 +12,15 @@ export class GarageDoor implements AccessoryPlugin {
private readonly informationService: Service; private readonly informationService: Service;
public name: string; public name: string;
//private currentPosition = 0; private currentPositionMin = 0; private currentPositionMax = 100; private currentPositionInverted = false;
//private = this.platform.Characteristic.PositionState.STOPPED;
private targetDoorState = this.platform.Characteristic.CurrentDoorState.CLOSED; private targetDoorState = this.platform.Characteristic.CurrentDoorState.CLOSED;
private currentDoorState = this.platform.Characteristic.CurrentDoorState.STOPPED; private currentDoorState = this.platform.Characteristic.CurrentDoorState.STOPPED;
private obstructionDetected = false;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name; this.name = accessory.name;
this.deviceService = new this.platform.Service.GarageDoorOpener(accessory.name); this.deviceService = new this.platform.Service.GarageDoorOpener(accessory.name);
// create handlers for required characteristics // create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentDoorState)
.onGet(this.getCurrentDoorState.bind(this))
.onSet(this.setCurrentDoorState.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.TargetDoorState)
.onGet(this.getTargetDoorState.bind(this))
.onSet(this.setTargetDoorState.bind(this));
this.informationService = this.informationService =
new this.platform.Service.AccessoryInformation() new this.platform.Service.AccessoryInformation()
@ -36,13 +28,32 @@ export class GarageDoor implements AccessoryPlugin {
.setCharacteristic(this.platform.Characteristic.Model, accessory.model) .setCharacteristic(this.platform.Characteristic.Model, accessory.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currentdoorstate); // FIXME .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currentdoorstate); // FIXME
this.platform.shng.addMonitor(accessory.currentdoorstate, this.shngCurrentDoorStateCallback.bind(this)); if (accessory.currentdoorstate) {
this.platform.shng.addMonitor(accessory.targetdoorstate, this.shngTargetDoorStateCallback.bind(this)); this.platform.shng.addMonitor(accessory.currentdoorstate, this.shngCurrentDoorStateCallback.bind(this));
/* this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentDoorState)
.onGet(this.getCurrentDoorState.bind(this))
.onSet(this.setCurrentDoorState.bind(this));
} else {
this.platform.log.error('GarageDoor: missing \"currentdoorstate\" in config.json!');
}
if (accessory.targetdoorstate) {
this.platform.shng.addMonitor(accessory.targetdoorstate, this.shngTargetDoorStateCallback.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.TargetDoorState)
.onGet(this.getTargetDoorState.bind(this))
.onSet(this.setTargetDoorState.bind(this));
} else {
this.platform.log.error('GarageDoor: missing \"targetdoorstate\" in config.json!');
}
if (accessory.obstructiondetected) { if (accessory.obstructiondetected) {
this.platform.shng.addMonitor(accessory.obstructiondetected, this.shngObstructionDetectedCallback.bind(this)); this.platform.shng.addMonitor(accessory.obstructiondetected, this.shngObstructionDetectedCallback.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.ObstructionDetected)
.onGet(this.getObstructionDetected.bind(this))
.onSet(this.setObstructionDetected.bind(this));
} }
*/
this.platform.log.info("GarageDoor '%s' created!", accessory.name); this.platform.log.info("GarageDoor '%s' created!", accessory.name);
} }
@ -55,6 +66,8 @@ export class GarageDoor implements AccessoryPlugin {
} }
getCurrentDoorState(): Nullable<CharacteristicValue> { getCurrentDoorState(): Nullable<CharacteristicValue> {
//getCurrentDoorState(value: CharacteristicValue) {
// this.currentDoorState = value as number;
this.platform.log.info('getCurrentDoorState:', this.accessory.name, 'is currently', this.currentDoorState); this.platform.log.info('getCurrentDoorState:', this.accessory.name, 'is currently', this.currentDoorState);
return this.currentDoorState; return this.currentDoorState;
} }
@ -62,10 +75,12 @@ export class GarageDoor implements AccessoryPlugin {
setCurrentDoorState(value: CharacteristicValue) { setCurrentDoorState(value: CharacteristicValue) {
this.currentDoorState = value as number; this.currentDoorState = value as number;
this.platform.log.info('setCurrentDoorState:', this.accessory.name, 'was set to', this.currentDoorState); this.platform.log.info('setCurrentDoorState:', this.accessory.name, 'was set to', this.currentDoorState);
this.platform.shng.setItem(this.accessory.currentDoorState, this.currentDoorState); this.platform.shng.setItem(this.accessory.currentdoorstate, this.currentDoorState);
} }
getTargetDoorState(): Nullable<CharacteristicValue> { getTargetDoorState(): Nullable<CharacteristicValue> {
//getTargetDoorState(value: CharacteristicValue) {
// this.targetDoorState = value as number;
this.platform.log.info('getTargetDoorState:', this.accessory.name, 'is currently', this.targetDoorState); this.platform.log.info('getTargetDoorState:', this.accessory.name, 'is currently', this.targetDoorState);
return this.targetDoorState; return this.targetDoorState;
} }
@ -73,13 +88,26 @@ export class GarageDoor implements AccessoryPlugin {
setTargetDoorState(value: CharacteristicValue) { setTargetDoorState(value: CharacteristicValue) {
this.targetDoorState = value as number; this.targetDoorState = value as number;
this.platform.log.info('setTargetDoorState:', this.accessory.name, 'was set to', this.targetDoorState); this.platform.log.info('setTargetDoorState:', this.accessory.name, 'was set to', this.targetDoorState);
this.platform.shng.setItem(this.accessory.targetDoorState, this.targetDoorState); this.platform.shng.setItem(this.accessory.targetdoorstate, this.targetDoorState);
}
getObstructionDetected(): Nullable<CharacteristicValue> {
this.platform.log.info('getObstructionDetected:', this.accessory.name, 'is currently', this.obstructionDetected);
return this.obstructionDetected;
}
setObstructionDetected(value: CharacteristicValue) {
this.obstructionDetected = value as boolean;
this.platform.log.info('setObstructionDetected:', this.accessory.name, 'was set to', this.obstructionDetected);
this.platform.shng.setItem(this.accessory.obstructiondetected, this.obstructionDetected);
} }
shngCurrentDoorStateCallback(value: unknown): void { shngCurrentDoorStateCallback(value: unknown): void {
this.platform.log.debug('shngCurrentDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); this.platform.log.debug('shngCurrentDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') { if (typeof value === 'number') {
this.currentDoorState = value; this.currentDoorState = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, this.currentDoorState);
} else { } else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
} }
@ -89,6 +117,17 @@ export class GarageDoor implements AccessoryPlugin {
this.platform.log.debug('shngTargetDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); this.platform.log.debug('shngTargetDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') { if (typeof value === 'number') {
this.targetDoorState = value; this.targetDoorState = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.TargetDoorState, this.targetDoorState);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
shngObstructionDetectedCallback(value: unknown): void {
this.platform.log.debug('shngObstructionDetectedCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.obstructionDetected = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, this.obstructionDetected);
} else { } else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
} }

View File

@ -0,0 +1,59 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class HumiditySensor implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private currentHumidity = 0;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.HumiditySensor(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
.onGet(this.getCurrentHumidity.bind(this));
this.informationService =
new this.platform.Service.AccessoryInformation()
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer)
.setCharacteristic(this.platform.Characteristic.Model, accessory.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currenthumidity);
this.platform.shng.addMonitor(accessory.currenthumidity, this.shngCallback.bind(this));
this.platform.log.info('HumiditySensor', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getCurrentHumidity(): Nullable<CharacteristicValue> {
this.platform.log.debug('getCurrentHumidity:', this.accessory.name, '=', this.currentHumidity);
return this.currentHumidity;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.currentHumidity = value;
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.currentHumidity);
}
}

View File

@ -22,6 +22,7 @@ import { SecuritySystem } from './Accessories/SecuritySystem';
import { OccupancySensor } from './Accessories/OccupancySensor'; import { OccupancySensor } from './Accessories/OccupancySensor';
import { MotionSensor } from './Accessories/MotionSensor'; import { MotionSensor } from './Accessories/MotionSensor';
import { GarageDoor } from './Accessories/GarageDoor'; import { GarageDoor } from './Accessories/GarageDoor';
import { HumiditySensor } from './Accessories/HumiditySensor';
function uncapitalizeKeys(obj): Record<string, unknown> { function uncapitalizeKeys(obj): Record<string, unknown> {
function isObject(o: unknown): boolean { function isObject(o: unknown): boolean {
@ -107,7 +108,7 @@ export class SmartHomeNGPlatform implements StaticPlatformPlugin {
devices.push(new Doorbell(this, accessory)); devices.push(new Doorbell(this, accessory));
break; break;
// Doorbell // Security system
case 'securitysystem': case 'securitysystem':
devices.push(new SecuritySystem(this, accessory)); devices.push(new SecuritySystem(this, accessory));
break; break;
@ -132,7 +133,7 @@ export class SmartHomeNGPlatform implements StaticPlatformPlugin {
devices.push(new Fan(this, accessory)); devices.push(new Fan(this, accessory));
break; break;
// TemperatureSensor // Temperature sensor
case 'temperaturesensor': case 'temperaturesensor':
devices.push(new TemperatureSensor(this, accessory)); devices.push(new TemperatureSensor(this, accessory));
break; break;
@ -147,11 +148,16 @@ export class SmartHomeNGPlatform implements StaticPlatformPlugin {
devices.push(new WindowCovering(this, accessory)); devices.push(new WindowCovering(this, accessory));
break; break;
// GarageDoor // Garage door
case 'garagedoor': case 'garagedoor':
devices.push(new GarageDoor(this, accessory)); devices.push(new GarageDoor(this, accessory));
break; break;
// Humidity sensor
case 'humiditysensor':
devices.push(new HumiditySensor(this, accessory));
break;
// Show error for (yet ?) unsupported device // Show error for (yet ?) unsupported device
default: default:
this.log.warn('Accessory type', accessory.type, 'for', accessory.name, 'not recognized !'); this.log.warn('Accessory type', accessory.type, 'for', accessory.name, 'not recognized !');