Skip to content

Commit

Permalink
refactor: AreaLightController vs INotify
Browse files Browse the repository at this point in the history
  • Loading branch information
Gh61 committed Jan 20, 2024
1 parent 1aba06f commit df05fb1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -846,5 +846,4 @@ description: false
```
## Coming soon features
- reactions on sliding event instead of on change (value will be changed in the moment of sliding, not after)
- faster reactions between multiple cards (instant change of value on other cards)
- ui editor?
25 changes: 18 additions & 7 deletions src/core/area-light-controller.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { HomeAssistant } from 'custom-card-helpers';
import { IHassTextTemplate, ILightContainer, ILightFeatures } from '../types/types-interface';
import { IHassTextTemplate, ILightContainer, ILightFeatures, INotifyGeneric } from '../types/types-interface';
import { Background } from './colors/background';
import { Color } from './colors/color';
import { GlobalLights } from './global-lights';
import { HassTextTemplate, StaticTextTemplate } from './hass-text-template';
import { LightController } from './light-controller';
import { LightFeaturesCombined } from './light-features';
import { NotifyBase } from './notify-base';
import { IconHelper } from './icon-helper';
import { localize } from '../localize/localize';
import { SceneData } from '../types/types-config';
import { Action2 } from '../types/functions';

/**
* Serves as a controller for lights in single area.
* This can contain multiple lights even some interactions can be different.
* (Instead of turnOn, activate scene).
*/
export class AreaLightController extends NotifyBase<AreaLightController> implements ILightContainer {
export class AreaLightController implements ILightContainer, INotifyGeneric<LightController> {
private _hass: HomeAssistant;
private _lightGroup?: LightController;
private _lights: LightController[];
private _lightsFeatures: LightFeaturesCombined;
private _defaultColor: Color;

public constructor(entity_ids: string[], defaultColor: Color, lightGroupEntityId?: string) {
super();

// we need at least one
if (!entity_ids.length)
throw new Error('No entity specified.');
Expand Down Expand Up @@ -59,13 +57,26 @@ export class AreaLightController extends NotifyBase<AreaLightController> impleme
return this._lights.map(l => l); // map will cause creation of new array
}

/**
* Will register for light changed events.
*/
public registerOnPropertyChanged(id: string, callback: Action2<(keyof LightController)[], LightController>): void {
this._lights.forEach(l => l.registerOnPropertyChanged(id, callback));
}

/**
* Will unregister light changed events.
*/
public unregisterOnPropertyChanged(id: string): void {
this._lights.forEach(l => l.unregisterOnPropertyChanged(id));
}

public set hass(hass: HomeAssistant) {
this._hass = hass;
this._lights.forEach(l => l.hass = hass);
if (this._lightGroup) {
this._lightGroup.hass = hass;
}
this.raisePropertyChanged('hass');
}
public get hass() {
return this._hass;
Expand Down Expand Up @@ -211,7 +222,7 @@ export class AreaLightController extends NotifyBase<AreaLightController> impleme
}
});

let result:string;
let result: string;

if (description != null) {
if (description) {
Expand Down
12 changes: 4 additions & 8 deletions src/core/light-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ export class LightController extends NotifyBase<LightController> implements ISin
this._lightState.brightnessValue = brightnessValue;
}

this.raisePropertyChanged('state', 'brightnessValue');
this.raisePropertyChanged('isOn', 'isOff', 'brightnessValue');
}

private notifyTurnOff(): void {
this._lightState.state = 'off';
this._lightState.brightnessValue = 0;

this.raisePropertyChanged('state', 'brightnessValue');
this.raisePropertyChanged('isOn', 'isOff', 'brightnessValue');
}

private notifyBrightnessValueChanged(value: number): void {
this._lightState.brightnessValue = value;
this._lightState.state = value > 0 ? 'on' : 'off';

this.raisePropertyChanged('state', 'brightnessValue');
this.raisePropertyChanged('isOn', 'isOff', 'brightnessValue');
}

private notifyColorTempChanged(value: number): void {
Expand All @@ -113,10 +113,6 @@ export class LightController extends NotifyBase<LightController> implements ISin

//#region State ON/OFF

public get state(): string {
return this._lightState.state;
}

public isUnavailable(): boolean {
return this._lightState.isUnavailable();
}
Expand All @@ -132,7 +128,7 @@ export class LightController extends NotifyBase<LightController> implements ISin
public turnOff(): void {
this.toggle(false);
}
public toggle(on: boolean, scene?: string | SceneData) {
private toggle(on: boolean, scene?: string | SceneData) {
if (this.isUnavailable())
return;

Expand Down
8 changes: 4 additions & 4 deletions src/core/notify-base.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Action1 } from '../types/functions';
import { Action2 } from '../types/functions';
import { INotifyGeneric } from '../types/types-interface';

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

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

protected raisePropertyChanged(...propertyNames: (keyof TThis)[]): void {
for (const callbackId in this._propertyChangedCallbacks) {
this._propertyChangedCallbacks[callbackId](propertyNames);
this._propertyChangedCallbacks[callbackId](propertyNames, <TThis><unknown>this);
}
}

Expand All @@ -19,7 +19,7 @@ export abstract class NotifyBase<TThis> implements INotifyGeneric<TThis> {
* @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).
*/
public registerOnPropertyChanged(id: string, callback: Action1<(keyof TThis)[]>) {
public registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>) {
this._propertyChangedCallbacks[id] = callback;
}

Expand Down
10 changes: 4 additions & 6 deletions src/types/types-interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { HomeAssistant } from 'custom-card-helpers';
import { Background } from '../core/colors/background';
import { Action1 } from './functions';
import { Action1, Action2 } from './functions';
import { Color } from '../core/colors/color';
import { HassLightColorMode } from './types-hass';

export type ValueFactory = () => unknown;

export interface INotify {
/**
* Will register callback on property change events.
Expand All @@ -27,7 +25,7 @@ export interface INotifyGeneric<TThis> extends INotify {
* @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).
*/
registerOnPropertyChanged(id: string, callback: Action1<(keyof TThis)[]>): void;
registerOnPropertyChanged(id: string, callback: Action2<(keyof TThis)[], TThis>): void;
}

export interface IHassTextTemplate {
Expand Down Expand Up @@ -70,7 +68,7 @@ export interface ILightConfig {
get features(): ILightFeatures;
}

export interface ILightContainer extends ILightConfig, INotify {
export interface ILightContainer extends ILightConfig {
/**
* @returns true if any light in this container is on.
*/
Expand Down Expand Up @@ -104,7 +102,7 @@ export interface ILightContainer extends ILightConfig, INotify {
brightnessValue: number;
}

export interface ISingleLightContainer extends ILightContainer {
export interface ISingleLightContainer extends ILightContainer, INotify {
/**
* @returns if light is in COLOR color mode.
*/
Expand Down

0 comments on commit df05fb1

Please sign in to comment.