Skip to content

Commit

Permalink
Added moveToHue and moveToSaturation
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Jun 24, 2024
1 parent bce491d commit 4dcf738
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 25 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
Endpoint,
SwitchCluster,
Switch,
ColorControlCluster,
} from 'matterbridge';
import { AnsiLogger, BLUE, CYAN, GREEN, TimestampFormat, YELLOW, db, debugStringify, dn, er, hk, idn, nf, or, rs, wr, zb } from 'node-ansi-logger';
import { NodeStorage, NodeStorageManager } from 'node-persist-manager';
Expand Down Expand Up @@ -185,6 +186,28 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
const state = child.getClusterServer(OnOffCluster)?.getOnOffAttribute();
this.shellyLightCommandHandler(mbDevice, endpoint.number, device, 'Level', state, request.level);
});
mbDevice.addCommandHandler('moveToHue', async ({ request, attributes, endpoint }) => {
attributes.colorMode.setLocal(ColorControl.ColorMode.CurrentHueAndCurrentSaturation);
const state = child.getClusterServer(OnOffCluster)?.getOnOffAttribute();
const level = child.getClusterServer(LevelControlCluster)?.getCurrentLevelAttribute();
const saturation = child.getClusterServer(ColorControlCluster.with(ColorControl.Feature.HueSaturation))?.getCurrentSaturationAttribute() ?? 0;
const rgb = hslColorToRgbColor((request.hue / 254) * 360, (saturation / 254) * 100, 50);
if (device.colorCommandTimeout) clearTimeout(device.colorCommandTimeout);
device.colorCommandTimeout = setTimeout(() => {
this.shellyLightCommandHandler(mbDevice, endpoint.number, device, 'ColorRGB', state, level, { r: rgb.r, g: rgb.g, b: rgb.b });
}, 200);
});
mbDevice.addCommandHandler('moveToSaturation', async ({ request, attributes, endpoint }) => {
attributes.colorMode.setLocal(ColorControl.ColorMode.CurrentHueAndCurrentSaturation);
const state = child.getClusterServer(OnOffCluster)?.getOnOffAttribute();
const level = child.getClusterServer(LevelControlCluster)?.getCurrentLevelAttribute();
const hue = child.getClusterServer(ColorControlCluster.with(ColorControl.Feature.HueSaturation))?.getCurrentHueAttribute() ?? 0;
const rgb = hslColorToRgbColor((hue / 254) * 360, (request.saturation / 254) * 100, 50);
if (device.colorCommandTimeout) clearTimeout(device.colorCommandTimeout);
device.colorCommandTimeout = setTimeout(() => {
this.shellyLightCommandHandler(mbDevice, endpoint.number, device, 'ColorRGB', state, level, { r: rgb.r, g: rgb.g, b: rgb.b });
}, 200);
});
mbDevice.addCommandHandler('moveToHueAndSaturation', async ({ request, attributes, endpoint }) => {
attributes.colorMode.setLocal(ColorControl.ColorMode.CurrentHueAndCurrentSaturation);
const state = child.getClusterServer(OnOffCluster)?.getOnOffAttribute();
Expand Down Expand Up @@ -655,8 +678,8 @@ export class ShellyPlatform extends MatterbridgeDynamicPlatform {
const cluster = endpoint.getClusterServer(ColorControl.Complete);
const hsl = rgbColorToHslColor({ r: red, g: green, b: blue });
this.log.warn(`Color: R:${red} G:${green} B:${blue} => H:${hsl.h} S:${hsl.s} L:${hsl.l}`);
if (shellyDevice.colorTimeout) clearTimeout(shellyDevice.colorTimeout);
shellyDevice.colorTimeout = setTimeout(() => {
if (shellyDevice.colorUpdateTimeout) clearTimeout(shellyDevice.colorUpdateTimeout);
shellyDevice.colorUpdateTimeout = setTimeout(() => {
const hue = Math.max(Math.min(Math.round((hsl.h / 360) * 254), 254), 0);
const saturation = Math.max(Math.min(Math.round((hsl.s / 100) * 254), 254), 0);
cluster?.setCurrentHueAttribute(hue);
Expand Down
9 changes: 6 additions & 3 deletions src/shellyDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class ShellyDevice extends EventEmitter {
gen = 0;
lastseen = 0;
hasUpdate = false;
colorTimeout?: NodeJS.Timeout;
colorUpdateTimeout?: NodeJS.Timeout;
colorCommandTimeout?: NodeJS.Timeout;
private lastseenInterval?: NodeJS.Timeout;
private startWsClientTimeout?: NodeJS.Timeout;

Expand All @@ -75,8 +76,10 @@ export class ShellyDevice extends EventEmitter {
}

destroy() {
if (this.colorTimeout) clearInterval(this.colorTimeout);
this.colorTimeout = undefined;
if (this.colorUpdateTimeout) clearInterval(this.colorUpdateTimeout);
this.colorUpdateTimeout = undefined;
if (this.colorCommandTimeout) clearInterval(this.colorCommandTimeout);
this.colorCommandTimeout = undefined;
this.lastseen = 0;
if (this.lastseenInterval) clearInterval(this.lastseenInterval);
this.lastseenInterval = undefined;
Expand Down

0 comments on commit 4dcf738

Please sign in to comment.