First v2 dev commit

This commit is contained in:
Serge Wagener committed 2022-02-08 17:01:36 +01:00
1 parent 0ab30244a6
commit b902603047
30 files changed
+7990 -805

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Fan implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private active = false;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Fanv2(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.Active)
.onGet(this.getActive.bind(this))
.onSet(this.setActive.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.active);
this.platform.shng.addMonitor(accessory.active, this.shngCallback.bind(this));
this.platform.log.info("Fan '%s' created!", accessory.name);
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getActive(): Nullable<CharacteristicValue> {
this.platform.log.info('getActive:', this.accessory.name, 'is currently', (this.active ? 'ON' : 'OFF'));
return this.active;
}
setActive(value: CharacteristicValue) {
this.active = value as boolean;
this.platform.log.info('setActive:', this.accessory.name, 'was set to', (this.active ? 'ON' : 'OFF'));
this.platform.shng.setItem(this.accessory.active, this.active);
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.active = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.Active, this.active);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}
+260
View File
@@ -0,0 +1,260 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
type RGBW = {
R: number;
G: number;
B: number;
W: number;
};
type RGB = {
R: number;
G: number;
B: number;
};
export class Lightbulb implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private switchOn = false;
private brightness = 0; brightnessMin = 0; private brightnessMax = 1000;
private R = 0; RMin = 0; RMax = 100;
private G = 0; GMin = 0; GMax = 100;
private B = 0; BMin = 0; BMax = 100;
private W = 0; WMin = 0; WMax = 100;
private hue = 0;
private saturation = 0;
private lightType = 0; // 0 = OnOff Light, 1 = Dimmable Light, 2 = RGB, 3 = RGBW
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory: any) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Lightbulb(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getOn.bind(this))
.onSet(this.setOn.bind(this));
this.platform.shng.addMonitor(accessory.on, this.shngOnCallback.bind(this));
if (accessory.brightness) {
this.deviceService.getCharacteristic(this.platform.Characteristic.Brightness)
.onGet(this.getBrightness.bind(this))
.onSet(this.setBrightness.bind(this));
this.platform.shng.addMonitor(accessory.brightness, this.shngBrightnessCallback.bind(this));
this.lightType = 1; // Dimmable light
}
// If RGB or RGBW light
if (accessory.r && accessory.g && accessory.b) {
this.deviceService.getCharacteristic(this.platform.Characteristic.Hue)
.onGet(this.getHue.bind(this))
.onSet(this.setHue.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.Saturation)
.onGet(this.getSaturation.bind(this))
.onSet(this.setSaturation.bind(this));
this.platform.shng.addMonitor(accessory.R, this.shngRCallback.bind(this));
this.platform.shng.addMonitor(accessory.G, this.shngGCallback.bind(this));
this.platform.shng.addMonitor(accessory.B, this.shngBCallback.bind(this));
this.lightType = 2; // RGB light
if (accessory.w) {
this.platform.shng.addMonitor(accessory.W, this.shngWCallback.bind(this));
this.lightType = 3; // RGBW light
}
}
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.on);
this.brightnessMax = accessory.brightnessmax ? accessory.brightnessmax : this.brightnessMax;
this.brightnessMin = accessory.brightnessmin ? accessory.brightnessmin : this.brightnessMin;
this.RMax = accessory.rmax ? accessory.rmax : this.RMax;
this.RMin = accessory.rmin ? accessory.rmin : this.RMin;
this.GMax = accessory.gmax ? accessory.gmax : this.GMax;
this.GMin = accessory.gmin ? accessory.gmin : this.GMin;
this.BMax = accessory.bmax ? accessory.bmax : this.BMax;
this.BMin = accessory.bmin ? accessory.bmin : this.BMin;
this.WMax = accessory.wmax ? accessory.wmax : this.WMax;
this.WMin = accessory.wmin ? accessory.wmin : this.WMin;
this.platform.log.info("Lightbulb '%s' created!", accessory.name);
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getOn(): Nullable<CharacteristicValue> {
this.platform.log.info('GetOn:', this.accessory.name, 'is currently', (this.switchOn ? 'ON' : 'OFF'));
return this.switchOn;
}
setOn(value: CharacteristicValue) {
this.switchOn = value as boolean;
this.platform.log.info('SetOn:', this.accessory.name, 'was set to', (this.switchOn ? 'ON' : 'OFF'));
this.platform.shng.setItem(this.accessory.on, this.switchOn);
}
getBrightness(): Nullable<CharacteristicValue> {
this.platform.log.info('getBrightness:', this.accessory.name, 'brightness is currently', this.brightness);
return this.brightness;
}
setBrightness(value: CharacteristicValue) {
this.brightness = value as number;
this.platform.log.info('setBrightness:', this.accessory.name, 'was set to', value);
this.platform.shng.setItem(
this.accessory.brightness,
this.convertRange(this.brightness, 0, 100, this.brightnessMin, this.brightnessMax),
);
this.updateColor();
}
getHue(): Nullable<CharacteristicValue> {
this.platform.log.info('getHue:', this.accessory.name, 'hue is currently', this.brightness);
return this.hue;
}
setHue(value: CharacteristicValue) {
this.hue = value as number;
this.platform.log.info('setHue:', this.accessory.name, 'was set to', value);
this.updateColor();
}
getSaturation(): Nullable<CharacteristicValue> {
this.platform.log.info('getSaturation:', this.accessory.name, 'saturation is currently', this.brightness);
return this.saturation;
}
setSaturation(value: CharacteristicValue) {
this.saturation = value as number;
this.platform.log.info('setSaturation:', this.accessory.name, 'was set to', value);
this.updateColor();
}
updateColor(): void {
if (this.lightType === 3) {
const rgbw: RGBW = this.hsb2rgbw(this.hue, this.saturation, this.brightness);
this.platform.shng.setItem(this.accessory.r, this.convertRange(rgbw.R, 0, 100, this.RMin, this.RMax));
this.platform.shng.setItem(this.accessory.g, this.convertRange(rgbw.G, 0, 100, this.GMin, this.GMax));
this.platform.shng.setItem(this.accessory.b, this.convertRange(rgbw.B, 0, 100, this.BMin, this.BMax));
this.platform.shng.setItem(this.accessory.w, this.convertRange(rgbw.W, 0, 100, this.WMin, this.WMax));
} else if (this.lightType === 2) {
const rgb: RGB = this.hsb2rgb(this.hue, this.saturation, this.brightness);
this.platform.shng.setItem(this.accessory.r, this.convertRange(rgb.R, 0, 100, this.RMin, this.RMax));
this.platform.shng.setItem(this.accessory.g, this.convertRange(rgb.G, 0, 100, this.GMin, this.GMax));
this.platform.shng.setItem(this.accessory.b, this.convertRange(rgb.B, 0, 100, this.BMin, this.BMax));
} else {
this.platform.log.warn('Cannot update color of', this.name, 'because RGB(W) items are missing');
}
}
shngOnCallback(value: unknown): void {
this.platform.log.debug('shngOnCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.switchOn = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.On, this.switchOn);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
shngBrightnessCallback(value: unknown): void {
this.platform.log.debug('shngBrightnessCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.brightness = this.convertRange(value as number, this.brightnessMin, this.brightnessMax, 0, 100);
this.deviceService.updateCharacteristic(this.platform.Characteristic.Brightness, this.brightness);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
shngRCallback(value: unknown): void {
// TODO
}
shngGCallback(value: unknown): void {
// TODO
}
shngBCallback(value: unknown): void {
// TODO
}
shngWCallback(value: unknown): void {
// TODO
}
// Credits to: https://github.com/Sunoo/homebridge-gpio-rgbw-ledstrip/blob/master/src/index.ts
hsb2rgbw(H: number, S: number, B: number): RGBW {
const rgbw = { R: 0, G: 0, B: 0, W: 0 };
if (H === 0 && S === 0) {
rgbw.W = B;
} else {
const segment = Math.floor(H / 60);
const offset = H % 60;
const mid = B * offset / 60;
rgbw.W = Math.round(B / 100 * (100 - S));
if (segment === 0) {
rgbw.R = Math.round(S / 100 * B);
rgbw.G = Math.round(S / 100 * mid);
} else if (segment === 1) {
rgbw.R = Math.round(S / 100 * (B - mid));
rgbw.G = Math.round(S / 100 * B);
} else if (segment === 2) {
rgbw.G = Math.round(S / 100 * B);
rgbw.B = Math.round(S / 100 * mid);
} else if (segment === 3) {
rgbw.G = Math.round(S / 100 * (B - mid));
rgbw.B = Math.round(S / 100 * B);
} else if (segment === 4) {
rgbw.R = Math.round(S / 100 * mid);
rgbw.B = Math.round(S / 100 * B);
} else if (segment === 5) {
rgbw.R = Math.round(S / 100 * B);
rgbw.B = Math.round(S / 100 * (B - mid));
}
}
return rgbw;
}
// Credits to: https://www.30secondsofcode.org/js/s/hsb-to-rgb
hsb2rgb(H: number, S: number, B: number): RGB {
const rgb = { R: 0, G: 0, B: 0 };
S /= 100;
B /= 100;
const k = (n) => (n + H / 60) % 6;
const f = (n) => B * (1 - S * Math.max(0, Math.min(k(n), 4 - k(n), 1)));
rgb.R = f(5) + 100;
rgb.G = f(3) + 100;
rgb.B = f(1) + 100;
return rgb;
}
convertRange(value: number, oldmin: number, oldmax: number, newmin: number, newmax: number): number {
return (((value - oldmin) * (newmax - newmin)) / (oldmax - oldmin)) + newmin;
}
}
+59
View File
@@ -0,0 +1,59 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class MotionSensor implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private motionDetected = false;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.MotionSensor(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.MotionDetected)
.onGet(this.handleMotionDetectedGet.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.motiondetected);
this.platform.shng.addMonitor(accessory.motiondetected, this.shngCallback.bind(this));
this.platform.log.info('MotionSensor', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
handleMotionDetectedGet(): Nullable<CharacteristicValue> {
this.platform.log.debug('handleMotionDetectedGet:', this.accessory.name, '=', this.motionDetected ? 'True' : 'False');
return this.motionDetected;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.motionDetected = value;
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.motionDetected);
}
}
+63
View File
@@ -0,0 +1,63 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class OccupancySensor implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private occupencyDetected = this.platform.Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.OccupancySensor(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.OccupancyDetected)
.onGet(this.handleOccupancyDetectedGet.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.occupancydetected);
this.platform.shng.addMonitor(accessory.occupancydetected, this.shngCallback.bind(this));
this.platform.log.info('OccupancySensor', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
handleOccupancyDetectedGet(): Nullable<CharacteristicValue> {
this.platform.log.debug('handleOccupancyDetectedGet:', this.accessory.name, '=', this.occupencyDetected ? 'True' : 'False');
return this.occupencyDetected;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
if (value) {
this.occupencyDetected = this.platform.Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
} else {
this.occupencyDetected = this.platform.Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
}
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, this.occupencyDetected);
}
}
+64
View File
@@ -0,0 +1,64 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Outlet implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private switchOn = false;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Outlet(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getOn.bind(this))
.onSet(this.setOn.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.on);
this.platform.shng.addMonitor(accessory.on, this.shngCallback.bind(this));
this.platform.log.info("Outlet '%s' created!", accessory.name);
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getOn(): Nullable<CharacteristicValue> {
this.platform.log.info('GetOn:', this.accessory.name, 'is currently', (this.switchOn ? 'ON' : 'OFF'));
return this.switchOn;
}
setOn(value: CharacteristicValue) {
this.switchOn = value as boolean;
this.platform.log.info('SetOn:', this.accessory.name, 'was set to', (this.switchOn ? 'ON' : 'OFF'));
this.platform.shng.setItem(this.accessory.on, this.switchOn);
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.switchOn = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.On, this.switchOn);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}
+64
View File
@@ -0,0 +1,64 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Switch implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private switchOn = false;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Switch(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getOn.bind(this))
.onSet(this.setOn.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.onoff);
this.platform.shng.addMonitor(accessory.onoff, this.shngCallback.bind(this));
this.platform.log.info("Switch '%s' created!", accessory.name);
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getOn(): Nullable<CharacteristicValue> {
this.platform.log.info('GetOn:', this.accessory.name, 'is currently', (this.switchOn ? 'ON' : 'OFF'));
return this.switchOn;
}
setOn(value: CharacteristicValue) {
this.switchOn = value as boolean;
this.platform.log.info('SetOn:', this.accessory.name, 'was set to', (this.switchOn ? 'ON' : 'OFF'));
this.platform.shng.setItem(this.accessory.onoff, this.switchOn);
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'boolean') {
this.switchOn = value;
this.deviceService.updateCharacteristic(this.platform.Characteristic.On, this.switchOn);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}
+59
View File
@@ -0,0 +1,59 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class TemperatureSensor implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private currentTemperature = 0;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.TemperatureSensor(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.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.currenttemperature);
this.platform.shng.addMonitor(accessory.currenttemperature, this.shngCallback.bind(this));
this.platform.log.info('TemperatureSensor', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getCurrentTemperature(): Nullable<CharacteristicValue> {
this.platform.log.debug('getCurrentTemperature:', this.accessory.name, '=', this.currentTemperature);
return this.currentTemperature;
}
shngCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.currentTemperature = value;
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.currentTemperature);
}
}
+153
View File
@@ -0,0 +1,153 @@
import {
AccessoryPlugin,
CharacteristicValue,
Service,
Nullable,
} from 'homebridge';
import { SmartHomeNGPlatform } from '../platform';
export class Thermostat implements AccessoryPlugin {
private readonly deviceService: Service;
private readonly informationService: Service;
public name: string;
private currentTemperature = 0;
private targetTemperature = 0;
private targetTemperatureDisplayUnit = this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS;
private currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF;
private targetHeatingCoolingState = this.platform.Characteristic.TargetHeatingCoolingState.AUTO;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
this.name = accessory.name;
this.deviceService = new this.platform.Service.Thermostat(accessory.name);
// create handlers for required characteristics
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onGet(this.getTargetTemperature.bind(this))
.onSet(this.setTargetTemperature.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits)
.onGet(this.getTemperatureDisplayUnits.bind(this))
.onSet(this.setTemperatureDisplayUnits.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState)
.onGet(this.getCurrentHeatingCoolingState.bind(this));
this.deviceService.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
.onGet(this.getTargetHeatingCoolingState.bind(this))
.onSet(this.setTargetHeatingCoolingState.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.currenttemperature);
// Add items to SHNG WS monitor
this.platform.shng.addMonitor(accessory.currenttemperature, this.shngCurrentTemperatureCallback.bind(this));
this.platform.shng.addMonitor(accessory.targettemperature, this.shngTargetTemperatureCallback.bind(this));
this.platform.shng.addMonitor(accessory.currentheatingcoolingstate, this.shngCurrentHeatingCoolingStateCallback.bind(this));
// FUTURE: Add monitoring of target Heating / Cooling states amd maybe display units
this.platform.log.info('Thermostat', accessory.name, 'created!');
}
identify(): void {
this.platform.log.info('Identify!');
}
getServices(): Service[] {
return [this.informationService, this.deviceService];
}
getCurrentTemperature(): Nullable<CharacteristicValue> {
this.platform.log.debug('getCurrentTemperature:', this.accessory.name, '=', this.currentTemperature);
return this.currentTemperature;
}
getTargetTemperature(): Nullable<CharacteristicValue> {
this.platform.log.debug('getTargetTemperature:', this.accessory.name, '=', this.targetTemperature);
return this.targetTemperature;
}
setTargetTemperature(value: CharacteristicValue) {
this.platform.log.debug('setTargetTemperature:', this.accessory.name, 'to', value);
this.targetTemperature = value as number;
this.platform.shng.setItem(this.accessory.targettemperature, this.targetTemperature);
}
getTemperatureDisplayUnits(): Nullable<CharacteristicValue> {
this.platform.log.debug('getTemperatureDisplayUnits:', this.accessory.name, '=', this.targetTemperatureDisplayUnit);
return this.targetTemperatureDisplayUnit;
}
setTemperatureDisplayUnits(value: CharacteristicValue) {
this.platform.log.debug('setTemperatureDisplayUnits:', this.accessory.name, 'to', value);
this.targetTemperatureDisplayUnit = value as number;
// FUTURE: Bind this to SHNG if needed
}
getCurrentHeatingCoolingState(): Nullable<CharacteristicValue> {
this.platform.log.debug('getCurrentHeatingCoolingState:', this.accessory.name, '=', this.currentHeatingCoolingState);
return this.currentHeatingCoolingState;
}
getTargetHeatingCoolingState(): Nullable<CharacteristicValue> {
this.platform.log.debug('getTargetHeatingCoolingState:', this.accessory.name, '=', this.targetHeatingCoolingState);
return this.targetHeatingCoolingState;
}
setTargetHeatingCoolingState(value: CharacteristicValue) {
this.platform.log.debug('setTargetHeatingCoolingState:', this.accessory.name, 'to', value);
this.targetHeatingCoolingState = value as number;
// FUTURE: Bind this to SHNG if needed
}
shngCurrentTemperatureCallback(value: unknown): void {
this.platform.log.debug('shngCurrentTemperatureCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.currentTemperature = value;
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.currentTemperature);
}
shngTargetTemperatureCallback(value: unknown): void {
this.platform.log.debug('shngTargetTemperatureCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.targetTemperature = value;
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
this.deviceService.updateCharacteristic(this.platform.Characteristic.TargetTemperature, this.targetTemperature);
}
shngCurrentHeatingCoolingStateCallback(value: unknown): void {
this.platform.log.debug('shngCurrentHeatingCoolingStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
switch (value) {
case 1:
this.currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.HEAT;
break;
case 2:
this.currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.COOL;
break;
default:
this.currentHeatingCoolingState = this.platform.Characteristic.CurrentHeatingCoolingState.OFF;
break;
}
this.deviceService.updateCharacteristic(
this.platform.Characteristic.CurrentHeatingCoolingState,
this.currentHeatingCoolingState,
);
} else {
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value);
}
}
}