Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Class property for contact sensor #321

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
"lib/zigbee2mqtt/zigbee2mqtt-device.js",
"lib/zigbee2mqtt/zigbee2mqtt-driver.js",
"lib/zigbee2mqtt/zigbee2mqtt-property.js",
"lib/zigbee2mqtt/utils.js",
"lib/zigbee2mqtt/properties/brightnessProperty.js",
"lib/zigbee2mqtt/properties/colorProperty.js",
"lib/zigbee2mqtt/properties/colorTemperatureProperty.js",
"lib/zigbee2mqtt/properties/contactProperty.js",
"lib/zigbee2mqtt/properties/heatingCoolingProperty.js",
"lib/zigbee2mqtt/properties/onOffProperty.js",
"manifest.json",
"node_modules"
]
Expand Down
2 changes: 1 addition & 1 deletion package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ npm ci
npm run build
npm prune --production

shasum --algorithm 256 manifest.json package.json lib/*.js lib/driver/*.js lib/zigbee2mqtt/*.js LICENSE README.md > SHA256SUMS
shasum --algorithm 256 manifest.json package.json lib/*.js lib/driver/*.js lib/zigbee2mqtt/*.js lib/zigbee2mqtt/properties/*.js LICENSE README.md > SHA256SUMS

find node_modules \( -type f -o -type l \) -exec shasum --algorithm 256 {} \; >> SHA256SUMS

Expand Down
32 changes: 32 additions & 0 deletions src/zigbee2mqtt/properties/brightnessProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';
import mqtt from 'mqtt';

export class BrightnessProperty extends Zigbee2MqttProperty<number> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'BrightnessProperty',
title: 'Brightness',
minimum: 0,
maximum: 100,
type: 'number',
unit: 'percent',
});
}

update(value: number, update: Record<string, unknown>): void {
const percent = Math.round((value / (this.expose.value_max ?? 100)) * 100);
super.update(percent, update);
}

protected async sendValue(value: number): Promise<void> {
return super.sendValue(Math.round((value / 100) * (this.expose.value_max ?? 100)));
}
}
91 changes: 91 additions & 0 deletions src/zigbee2mqtt/properties/colorProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';
import mqtt from 'mqtt';
import Color from 'color';

function limit(value: number, min: number, max: number): number {
return Math.max(Math.min(value, max), min);
}

/*
I tried

Color({
x: value.x * 100,
y: value.y * 100,
z: ((update.brightness as number) ?? 255) * 100 / 255,
}).hex()

but it seems to calculate the wrong color.
If we send #00FF00 to zigbee2mqtt we get {"x":0.1721,"y":0.6905} as answer.
The Color class translates this to #00FFF5.
*/
// https://stackoverflow.com/questions/22894498/philips-hue-convert-xy-from-api-to-hex-or-rgb
function xyBriToRgb(x: number, y: number, bri: number): RGB {
const z = 1.0 - x - y;

const Y = bri / 255.0;
const X = (Y / y) * x;
const Z = (Y / y) * z;

let r = X * 1.612 - Y * 0.203 - Z * 0.302;
let g = -X * 0.509 + Y * 1.412 + Z * 0.066;
let b = X * 0.026 - Y * 0.072 + Z * 0.962;

r = r <= 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math.pow(r, 1.0 / 2.4) - 0.055;
g = g <= 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math.pow(g, 1.0 / 2.4) - 0.055;
b = b <= 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math.pow(b, 1.0 / 2.4) - 0.055;

const maxValue = Math.max(r, g, b);

r /= maxValue;
g /= maxValue;
b /= maxValue;

r = limit(r * 255, 0, 255);
g = limit(g * 255, 0, 255);
b = limit(b * 255, 0, 255);

return { r: Math.round(r), g: Math.round(g), b: Math.round(b) };
}

export interface RGB {
r: number;
g: number;
b: number;
}

export interface XY {
x: number;
y: number;
}

export class ColorProperty extends Zigbee2MqttProperty<string> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'ColorProperty',
title: 'Color',
type: 'string',
readOnly: false,
});
}

update(value: XY, update: Record<string, unknown>): void {
const rgb = xyBriToRgb(value.x, value.y, (update.brightness as number) ?? 255);
const color = new Color(rgb);
super.update(color.hex(), update);
}

protected async sendValue(value: string): Promise<void> {
const color = new Color(value);
const rgb = color.object();
return super.sendValue(rgb);
}
}
39 changes: 39 additions & 0 deletions src/zigbee2mqtt/properties/colorTemperatureProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';
import mqtt from 'mqtt';

function kelvinToMiredd(kelvin: number): number {
return Math.round(1_000_000 / kelvin);
}

function miredToKelvin(mired: number): number {
return Math.round(1_000_000 / mired);
}

export class ColorTemperatureProperty extends Zigbee2MqttProperty<number> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'ColorTemperatureProperty',
title: 'Color temperature',
type: 'number',
minimum: miredToKelvin(expose.value_max!),
maximum: miredToKelvin(expose.value_min!),
unit: 'kelvin',
});
}

update(value: number, update: Record<string, unknown>): void {
super.update(miredToKelvin(value), update);
}

protected async sendValue(value: number): Promise<void> {
return super.sendValue(kelvinToMiredd(value));
}
}
26 changes: 26 additions & 0 deletions src/zigbee2mqtt/properties/contactProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import mqtt from 'mqtt';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';

export class ContactProperty extends Zigbee2MqttProperty<boolean> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'OpenProperty',
title: 'Open',
type: 'boolean',
});

device['@type'].push('DoorSensor');
}

update(value: boolean, update: Record<string, unknown>): void {
super.update(!value, update);
}
}
46 changes: 46 additions & 0 deletions src/zigbee2mqtt/properties/heatingCoolingProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';
import mqtt from 'mqtt';

function convertHeatingCoolingValues(value?: string[]): string[] | undefined {
if (value) {
return value.map((x) => convertHeatingCoolingValue(x));
}

return value;
}

function convertHeatingCoolingValue(value: string): string {
switch (value) {
case 'idle':
return 'off';
case 'heat':
return 'heating';
case 'cool':
return 'cooling';
default:
throw new Error(`Invalid HeatingCoolingValue ${value}, expected idle, heat or cool`);
}
}

export class HeatingCoolingProperty extends Zigbee2MqttProperty<string> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'HeatingCoolingProperty',
title: 'Run Mode',
type: 'string',
enum: convertHeatingCoolingValues(expose.values),
});
}

update(value: string, update: Record<string, unknown>): void {
super.update(convertHeatingCoolingValue(value), update);
}
}
29 changes: 29 additions & 0 deletions src/zigbee2mqtt/properties/onOffProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Expos } from '../zigbee2mqtt-adapter';
import { Zigbee2MqttDevice } from '../zigbee2mqtt-device';
import mqtt from 'mqtt';
import { Zigbee2MqttProperty } from '../zigbee2mqtt-property';
import { parseType } from '../utils';

export class OnOffProperty extends Zigbee2MqttProperty<boolean> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
super(device, name, expose, client, deviceTopic, {
'@type': 'OnOffProperty',
title: 'On',
type: parseType(expose),
});
}

update(value: string, update: Record<string, unknown>): void {
super.update(value === 'ON', update);
}

protected async sendValue(value: boolean): Promise<void> {
return super.sendValue(value ? 'ON' : 'OFF');
}
}
28 changes: 28 additions & 0 deletions src/zigbee2mqtt/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PropertyValueType } from 'gateway-addon/lib/schema';
import { Expos } from './zigbee2mqtt-adapter';

export function parseType(expose: Expos): PropertyValueType {
switch (expose.type) {
case 'numeric':
if (expose.value_step === 1) {
return 'integer';
} else {
return 'number';
}
case 'enum':
return 'string';
case 'binary':
return 'boolean';
}

return 'string';
}

export function parseUnit(unit?: string): string | undefined {
switch (unit) {
case '°C':
return 'degree celsius';
}

return unit;
}
46 changes: 30 additions & 16 deletions src/zigbee2mqtt/zigbee2mqtt-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import { Action, Device, Event } from 'gateway-addon';
import { PropertyValue, Event as EventSchema } from 'gateway-addon/lib/schema';
import { Zigbee2MqttAdapter, DeviceDefinition, Expos } from './zigbee2mqtt-adapter';
import {
OnOffProperty,
BrightnessProperty,
ColorTemperatureProperty,
ColorProperty,
Zigbee2MqttProperty,
WRITE_BIT,
parseType,
parseUnit,
HeatingCoolingProperty,
} from './zigbee2mqtt-property';
import mqtt from 'mqtt';
import DEBUG_FLAG from '../zb-debug';
import { OnOffProperty } from './properties/onOffProperty';
import { BrightnessProperty } from './properties/brightnessProperty';
import { ColorTemperatureProperty } from './properties/colorTemperatureProperty';
import { ColorProperty } from './properties/colorProperty';
import { ContactProperty } from './properties/contactProperty';
import { HeatingCoolingProperty } from './properties/heatingCoolingProperty';
import { parseType, parseUnit } from './utils';

function debug(): boolean {
return DEBUG_FLAG.DEBUG_zigbee2mqtt;
Expand Down Expand Up @@ -342,15 +342,29 @@ export class Zigbee2MqttDevice extends Device {

console.log(`Creating property for ${expose.name}`);

const property = new Zigbee2MqttProperty<T>(
this,
expose.name,
expose,
this.client,
this.deviceTopic
);

this.addProperty(property);
switch (expose.name) {
case 'contact': {
const property = new ContactProperty(
this,
expose.name,
expose,
this.client,
this.deviceTopic);
this.addProperty(property);
}
break;
default: {
const property = new Zigbee2MqttProperty<T>(
this,
expose.name,
expose,
this.client,
this.deviceTopic
);
this.addProperty(property);
}
break;
}
} else {
console.log(`Ignoring property without name: ${JSON.stringify(expose, null, 0)}`);
}
Expand Down
Loading