Skip to content

Commit

Permalink
refactor(INotify): introduce 'includeHass' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gh61 committed Jan 21, 2024
1 parent 22bf71e commit 541e9b7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
30 changes: 10 additions & 20 deletions src/controls/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ export class HueDialog extends IdLitElement {
this._actionHandler = actionHandler;
}

//#region Hass changes

private onLightControllerChanged(propertyNames: (keyof LightController)[]) {
// when LightController changed - update this
if (propertyNames.includes('hass')) {
this.requestUpdate();
}
}

//#endregion

//#region Tile interactions

// lightDetail as history step
Expand Down Expand Up @@ -209,8 +198,8 @@ export class HueDialog extends IdLitElement {
document.body.appendChild(this);
}

// register update delegate
this._ctrl.registerOnPropertyChanged(this._elementId, (p) => this.onLightControllerChanged(p));
// register update delegate (include hass - we need to update the dialog)
this._ctrl.registerOnPropertyChanged(this._elementId, this.onChangeHandler, /* includeHass: */ true);
}

public close(): void {
Expand Down Expand Up @@ -608,6 +597,12 @@ export class HueDialog extends IdLitElement {
}
}

private onChangeHandler = () => this.onChangeCallback();
private onChangeCallback() {
this.requestUpdate();
this.updateStylesInner(false);
}

protected override render() {
this._isRendered = true;

Expand All @@ -616,11 +611,6 @@ export class HueDialog extends IdLitElement {
const cardTitle = this._config.getTitle(this._ctrl).resolveToString(this._ctrl.hass);
const mdiClose = 'mdi:close';

const onChangeCallback = () => {
this.requestUpdate();
this.updateStylesInner(false);
};

/*eslint-disable */
return html`
<ha-dialog
Expand Down Expand Up @@ -648,9 +638,9 @@ export class HueDialog extends IdLitElement {
${cardTitle}
</div>
<div slot="actionItems">
${ViewUtils.createSwitch(this._ctrl, onChangeCallback, this._config.switchOnScene)}
${ViewUtils.createSwitch(this._ctrl, this.onChangeHandler, this._config.switchOnScene)}
</div>
${ViewUtils.createSlider(this._ctrl, this._config, onChangeCallback)}
${ViewUtils.createSlider(this._ctrl, this._config, this.onChangeHandler)}
</ha-dialog-header>
<div class="${classMap({
'content': true,
Expand Down
2 changes: 1 addition & 1 deletion src/controls/light-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class HueLightDetail extends IdLitElement {
this.lightContainer.registerOnPropertyChanged(this._elementId, () => {
this.onLightContainerState();
this.requestUpdate();
});
}, /* includeHass: */ true);
this.onLightContainerChanged();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/area-light-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class AreaLightController implements ILightContainer, INotifyGeneric<Ligh
/**
* Will register for light changed events.
*/
public registerOnPropertyChanged(id: string, callback: Action2<(keyof LightController)[], LightController>): void {
this._lights.forEach(l => l.registerOnPropertyChanged(id, callback));
public registerOnPropertyChanged(id: string, callback: Action2<(keyof LightController)[], LightController>, includeHass = false): void {
this._lights.forEach(l => l.registerOnPropertyChanged(id, callback, includeHass));
}

/**
Expand Down
36 changes: 22 additions & 14 deletions src/core/notify-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@ import { Action2 } from '../types/functions';
import { INotifyGeneric } from '../types/types-interface';

export abstract class NotifyBase<TThis> implements INotifyGeneric<TThis> {
private _propertyChangedCallbacks: Record<string, Action2<(keyof TThis)[], TThis>>;
private _propertyChangedCallbacks: Record<string, {
invoke: Action2<(keyof TThis)[], TThis>,
includeHass: boolean
}>;

protected constructor() {
this._propertyChangedCallbacks = {};
}

/*
* !!!!!
* TODO: filter hass PropertyChanged?
*/

protected raisePropertyChanged(...propertyNames: (keyof TThis)[]): void {
this.log(`Firing ${this.constructor.name}.PropertyChanged changed [${propertyNames.join(', ')}].`);
const onlyHass = propertyNames.length == 1 && propertyNames[0] == 'hass';
this.log(`${this.constructor.name} changed [${propertyNames.join(', ')}] (onlyHass:${onlyHass})`);

for (const callbackId in this._propertyChangedCallbacks) {
this.log(`Firing ${this.constructor.name}.PropertyChanged changed [${propertyNames.join(', ')}] for ${callbackId}.`);
this._propertyChangedCallbacks[callbackId](propertyNames, <TThis><unknown>this);
const handler = this._propertyChangedCallbacks[callbackId];

if (handler.includeHass || !onlyHass) {
this.log(`${this.constructor.name} changed [${propertyNames.join(', ')}] for ${callbackId}`);
handler.invoke(propertyNames, <TThis><unknown>this);
}
}
}

/**
* Will register callback on property change events.
* @param id Id for this specific callback. If this id already exists, it's callback will be overwriten.
* @param callback Action that will be called when any supported property if changed (takes propertyName as parameter).
* @param includeHass Specifies, whether change only in 'hass' property should be included (set to false to ignore).
*/
public registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>) {
this._propertyChangedCallbacks[id] = callback;
this.log(`Registered ${this.constructor.name}.PropertyChanged by control ID: '${id}'`);
public registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>, includeHass = false) {
this._propertyChangedCallbacks[id] = {
invoke: callback,
includeHass: includeHass
};
this.log(`Registered change of ${this.constructor.name} by control: '${id}' (includeHass:${includeHass})`);
}

/**
Expand All @@ -38,12 +46,12 @@ export abstract class NotifyBase<TThis> implements INotifyGeneric<TThis> {
*/
public unregisterOnPropertyChanged(id: string) {
delete this._propertyChangedCallbacks[id];
this.log(`Unregistered ${this.constructor.name}.PropertyChanged by control ID: '${id}'`);
this.log(`Unregistered change of ${this.constructor.name} for control: '${id}'`);
}

private log(message: string) {
if (Consts.Dev) {
console.log(message);
console.log('[HueNotify] ' + message);
}
}
}
10 changes: 1 addition & 9 deletions src/hue-like-light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ export class HueLikeLightCard extends IdLitElement implements LovelaceCard {

private onChangeHandler = () => this.onChangeCallback();
private onChangeCallback() {
console.log(this._elementId + ' Requesting update');
this.requestUpdate();
this.updateStylesInner();
}
Expand Down Expand Up @@ -443,14 +442,7 @@ export class HueLikeLightCard extends IdLitElement implements LovelaceCard {
private setupListeners() {
if (!this._ctrlListenerRegistered && this._ctrl) {
this._ctrlListenerRegistered = true;
this._ctrl.registerOnPropertyChanged(this._elementId, (props) => {
if (props[0] == 'hass' && props.length == 1) {
// ignore
}
else {
this.onChangeCallback();
}
});
this._ctrl.registerOnPropertyChanged(this._elementId, this.onChangeHandler);
}

const tapArea = this.renderRoot.querySelector('.tap-area');
Expand Down
6 changes: 4 additions & 2 deletions src/types/types-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export interface INotify {
* Will register callback on property change events.
* @param id Id for this specific callback. If this id already exists, it's callback will be overwriten.
* @param callback Action that will be called when any supported property if changed (takes propertyName as parameter).
* @param includeHass Specifies, whether change only in 'hass' property should be included (set to false to ignore).
*/
registerOnPropertyChanged(id: string, callback: Action1<(string | number | symbol)[]>): void;
registerOnPropertyChanged(id: string, callback: Action1<(string | number | symbol)[]>, includeHass?: boolean): void;

/**
* Will unregister callback from property change events.
Expand All @@ -24,8 +25,9 @@ export interface INotifyGeneric<TThis> extends INotify {
* Will register callback on property change events.
* @param id Id for this specific callback. If this id already exists, it's callback will be overwriten.
* @param callback Action that will be called when any supported property if changed (takes propertyName as parameter).
* @param includeHass Specifies, whether change only in 'hass' property should be included (set to false to ignore).
*/
registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>): void;
registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>, includeHass?: boolean): void;
}

export interface IHassTextTemplate {
Expand Down

0 comments on commit 541e9b7

Please sign in to comment.