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

Workaround IKEA bulbs freezing during a brightness & color transition #8637

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions src/converters/toZigbee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Zcl} from 'zigbee-herdsman';
import * as libColor from '../lib/color';
import * as constants from '../lib/constants';
import * as exposes from '../lib/exposes';
import {unfreezeMechanisms, UnfreezeSupport} from '../lib/ikea';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like vendor-specific imports shouldn't be here and the logic from ../lib/ikea should be inlined into the ikea_bulb_unfreeze converter below

import * as legacy from '../lib/legacy';
import * as light from '../lib/light';
import {logger} from '../lib/logger';
Expand Down Expand Up @@ -4509,6 +4510,38 @@ const converters3 = {
return await converters2.light_onoff_brightness.convertSet(entity, key, value, meta);
},
} satisfies Tz.Converter,
ikea_bulb_unfreeze: {
key: ['transition', 'brightness', 'brightness_percent', 'color_temp', 'color_temp_percent'],
convertSet: async (entity, key, value, meta) => {
const {message} = meta;

// attempts to transition a colour and brightness at the same time fail
if ('transition' in message && typeof message.transition === 'number' && 'brightness' in message && 'color_temp' in message) {
const appliedFix = 'hasIkeaFix' in entity;

if (!appliedFix) {
logger.info('Applied workaround for IKEA brightness + temperature change', NS);

const unfreeze = new UnfreezeSupport(
entity,
unfreezeMechanisms.genLevelCtrl, // hardcoded
);

const newEntity = Object.create(entity);
newEntity.hasIkeaFix = true;

newEntity.command = async (clusterKey: string, commandKey: string, payload: KeyValue, options: KeyValue) => {
return await unfreeze.command(clusterKey, commandKey, payload, options);
};

entity = newEntity;
// rob: return await converters.convertSet(newEntity, key, value, meta);
}
}

await entity.command('genOnOff', 'on', {}, utils.getOptions(meta.mapped, entity));
},
} satisfies Tz.Converter,
};

const converters = {...converters1, ...converters2, ...converters3};
Expand Down
2 changes: 2 additions & 0 deletions src/devices/ikea.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Zcl} from 'zigbee-herdsman';

import tz from '../converters/toZigbee';
import {repInterval} from '../lib/constants';
import {
addCustomClusterManuSpecificIkeaAirPurifier,
Expand Down Expand Up @@ -210,6 +211,7 @@ const definitions: DefinitionWithExtend[] = [
vendor: 'IKEA',
description: 'TRADFRI bulb B22, white spectrum, globe, opal, 1055 lm',
extend: [addCustomClusterManuSpecificIkeaUnknown(), ikeaLight({colorTemp: true}), m.identify()],
toZigbee: [tz.ikea_bulb_unfreeze],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could specify different unfreeze mechanisms in the future (as in the original code), but I only have this one bulb type to test with, so I'm just changing this for now

},
// #endregion E26/E27/B22
{
Expand Down
64 changes: 64 additions & 0 deletions src/lib/ikea.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as semver from 'semver';

import {Zcl} from 'zigbee-herdsman';
import {Endpoint, Group} from 'zigbee-herdsman/dist/controller/model';

import tz from '../converters/toZigbee';
import * as constants from '../lib/constants';
Expand Down Expand Up @@ -812,3 +813,66 @@ export function addCustomClusterManuSpecificIkeaUnknown(): ModernExtend {
commandsResponse: {},
});
}

type UnfreezeMechanism = () => Promise<void>;

export const unfreezeMechanisms: {
[key: string]: UnfreezeMechanism;
} = {
// WS lights:
// Aborts the color transition midway: light will stay at the intermediary
// state it was when it received the command.
// Color lights:
// Do not support this command.
moveColorTemp: async function (this: {entity: Endpoint | Group}) {
await this.entity.command('lightingColorCtrl', 'moveColorTemp', {rate: 1, movemode: 0, minimum: 0, maximum: 600}, {});
},

// WS lights:
// Same as 'moveColorTemp'.
// Color lights:
// Finishes the color transition instantly: light will instantly
// "fast forward" to the final state, post-transition.
genLevelCtrl: async function (this: {entity: Endpoint | Group}) {
await this.entity.command('genLevelCtrl', 'stop', {}, {});
},
};

const willFreeze = (clusterKey: string, payload: KeyValue): payload is KeyValue & {transtime: number} =>
payload &&
// any color command with a transition will freeze the light...
typeof payload.transtime === 'number' &&
payload.transtime > 0 &&
clusterKey == 'lightingColorCtrl' &&
// ...except for 'stop' commands:
payload.rate != 1 &&
payload.movemode != 0;

export class UnfreezeSupport {
readonly entity: Endpoint | Group;
readonly unfreeze: UnfreezeMechanism;

constructor(entity: Endpoint | Group, mechanism: UnfreezeMechanism) {
this.entity = entity;
this.unfreeze = mechanism;
}

async command(clusterKey: string, commandKey: string, payload: KeyValue, options: KeyValue) {
const frozenUtil = globalStore.getValue(this.entity, 'frozenUntil');
if (frozenUtil != null) {
if (Date.now() <= frozenUtil) {
console.log('Light is frozen, will attempt to unfreeze');
Copy link
Contributor Author

@bobrippling bobrippling Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and below) will be changed to a proper logger call

await this.unfreeze();
}
globalStore.clearValue(this.entity, 'frozenUntil');
}
const returnValue = await this.entity.command(clusterKey, commandKey, payload, options);
if (willFreeze(clusterKey, payload)) {
const millis = payload.transtime * 100;
const frozenUntil = Date.now() + millis;
globalStore.putValue(this.entity, 'frozenUntil', frozenUntil);
console.log('Command', clusterKey + '.' + commandKey, payload, options, 'will freeze the light until', frozenUntil);
}
return returnValue;
}
}
Loading