Skip to content

Commit

Permalink
feat(RelativeRotary): added support for the relative_rotary resource
Browse files Browse the repository at this point in the history
Signed-off-by: s222em <sem.roeten@gmail.com>
  • Loading branch information
S222em committed Aug 28, 2023
1 parent 9366a0f commit 4ae6103
Show file tree
Hide file tree
Showing 28 changed files with 458 additions and 329 deletions.
29 changes: 29 additions & 0 deletions src/actions/RelativeRotary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Hue } from '../hue/Hue';
import { Events } from '../hue/HueEvents';
import { SSEResource } from '../types/sse';

export function relativeRotaryAdd(data: SSEResource, hue: Hue) {
const relativeRotary = hue.relativeRotaries._add(data);

return () => hue.emit(Events.RelativeRotaryAdd, relativeRotary);
}

export function relativeRotaryUpdate(data: SSEResource, hue: Hue) {
const relativeRotary = hue.relativeRotaries.cache.get(data.id);
if (!relativeRotary) return;

const clone = relativeRotary._update(data);

return () => hue.emit(Events.RelativeRotaryUpdate, relativeRotary, clone);
}

export function relativeRotaryDelete(data: SSEResource, hue: Hue) {
const relativeRotary = hue.relativeRotaries.cache.get(data.id);
if (!relativeRotary) return;

const clone = relativeRotary._clone();

hue.relativeRotaries.cache.delete(data.id);

return () => hue.emit(Events.RelativeRotaryDelete, clone);
}
4 changes: 4 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { temperatureAdd, temperatureDelete, temperatureUpdate } from './Temperat
import { lightLevelAdd, lightLevelDelete, lightLevelUpdate } from './LightLevel';
import { zgpConnectivityAdd, zgpConnectivityDelete, zgpConnectivityUpdate } from './ZgpConnectivity';
import { geofenceClientAdd, geofenceClientDelete, geofenceClientUpdate } from './GeofenceClient';
import { relativeRotaryAdd, relativeRotaryDelete, relativeRotaryUpdate } from './RelativeRotary';

export const RESOURCE_ADD_ACTION: { [key: string]: (data: SSEResource, hue: Hue) => (() => boolean) | undefined } = {
[APIResourceType.Device]: deviceAdd,
Expand All @@ -43,6 +44,7 @@ export const RESOURCE_ADD_ACTION: { [key: string]: (data: SSEResource, hue: Hue)
[APIResourceType.LightLevel]: lightLevelAdd,
[APIResourceType.ZgpConnectivity]: zgpConnectivityAdd,
[APIResourceType.GeofenceClient]: geofenceClientAdd,
[APIResourceType.RelativeRotary]: relativeRotaryAdd,
};

export const RESOURCE_DELETE_ACTION: { [key: string]: (data: SSEResource, hue: Hue) => (() => boolean) | undefined } = {
Expand All @@ -64,6 +66,7 @@ export const RESOURCE_DELETE_ACTION: { [key: string]: (data: SSEResource, hue: H
[APIResourceType.LightLevel]: lightLevelDelete,
[APIResourceType.ZgpConnectivity]: zgpConnectivityDelete,
[APIResourceType.GeofenceClient]: geofenceClientDelete,
[APIResourceType.RelativeRotary]: relativeRotaryDelete,
};

export const RESOURCE_UPDATE_ACTION: { [key: string]: (data: SSEResource, hue: Hue) => (() => boolean) | undefined } = {
Expand All @@ -85,4 +88,5 @@ export const RESOURCE_UPDATE_ACTION: { [key: string]: (data: SSEResource, hue: H
[APIResourceType.LightLevel]: lightLevelUpdate,
[APIResourceType.ZgpConnectivity]: zgpConnectivityUpdate,
[APIResourceType.GeofenceClient]: geofenceClientUpdate,
[APIResourceType.RelativeRotary]: relativeRotaryUpdate,
};
6 changes: 6 additions & 0 deletions src/hue/Hue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { TemperatureManager } from '../managers/TemperatureManager';
import { LightLevelManager } from '../managers/LightLevelManager';
import { ZgpConnectivityManager } from '../managers/ZgpConnectivityManager';
import { GeofenceClientManager } from '../managers/GeofenceClientManager';
import { RelativeRotaryManager } from '../managers/RelativeRotaryManager';

export const CA =
'-----BEGIN CERTIFICATE-----\n' +
Expand Down Expand Up @@ -162,6 +163,11 @@ export class Hue extends EventEmitter {
*/
public readonly geofenceClients = new GeofenceClientManager(this);

/**
* All of the {@link RelativeRotary} objects that have been cached, mapped by their ids
*/
public readonly relativeRotaries = new RelativeRotaryManager(this);

/**
* The REST manager
* @private
Expand Down
7 changes: 7 additions & 0 deletions src/hue/HueEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Temperature } from '../structures/Temperature';
import { LightLevel } from '../structures/LightLevel';
import { ZgpConnectivity } from '../structures/ZgpConnectivity';
import { GeofenceClient } from '../structures/GeofenceClient';
import { RelativeRotary } from '../structures/RelativeRotary';

export const Events = {
Ready: 'ready',
Expand Down Expand Up @@ -80,6 +81,9 @@ export const Events = {
GeofenceClientAdd: 'geofenceClientAdd',
GeofenceClientUpdate: 'geofenceClientUpdate',
GeofenceClientDelete: 'geofenceClientDelete',
RelativeRotaryAdd: 'relativeRotaryAdd',
RelativeRotaryUpdate: 'relativeRotaryUpdate',
RelativeRotaryDelete: 'relativeRotaryDelete',
} as const;

export interface HueEvents {
Expand Down Expand Up @@ -149,4 +153,7 @@ export interface HueEvents {
[Events.GeofenceClientAdd]: [geofenceClient: GeofenceClient];
[Events.GeofenceClientUpdate]: [newGeofenceClient: GeofenceClient, oldGeofenceClient: GeofenceClient];
[Events.GeofenceClientDelete]: [geofenceClient: GeofenceClient];
[Events.RelativeRotaryAdd]: [relativeRotary: RelativeRotary];
[Events.RelativeRotaryUpdate]: [newRelativeRotary: RelativeRotary, oldRelativeRotary: RelativeRotary];
[Events.RelativeRotaryDelete]: [geofenceClient: RelativeRotary];
}
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ export * from './color/xy';
export * from './color/gamut';
export * from './color/rgb';
export * from './color/hex';

//todo entertainment_configuration
//todo entertainment
//todo homekit
//todo matter
//todo matter_fabric
//todo smart_scene
//todo relative_rotary
//todo behaviour_script
//todo behaviour_instance
11 changes: 11 additions & 0 deletions src/managers/RelativeRotaryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ResourceManager } from './ResourceManager';
import { APIResourceType } from '../types/api';
import { RelativeRotary } from '../structures/RelativeRotary';

/**
* Manages the relative_rotary resource
*/
export class RelativeRotaryManager extends ResourceManager<RelativeRotary> {
type = APIResourceType.RelativeRotary;
holds = RelativeRotary;
}
14 changes: 7 additions & 7 deletions src/structures/ArcheTypeResource.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { APINamedResource, NamedResource, NamedResourceEditOptions } from './NamedResource';
import { APIArcheType, APIResourceType } from '../types/api';

export interface APIArcheTypeResource<T extends APIResourceType = APIResourceType> extends APINamedResource<T> {
metadata: {
name: string;
archetype: APIArcheType;
};
}

export interface ArcheTypeResourceEditOptions extends NamedResourceEditOptions {
archeType?: APIArcheType;
}
Expand Down Expand Up @@ -42,3 +35,10 @@ export abstract class ArcheTypeResource<

public abstract edit(options: ArcheTypeResourceEditOptions): Promise<string>;
}

export interface APIArcheTypeResource<T extends APIResourceType = APIResourceType> extends APINamedResource<T> {
metadata: {
name: string;
archetype: APIArcheType;
};
}
16 changes: 8 additions & 8 deletions src/structures/Bridge.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Resource } from './Resource';
import { APIResource, APIResourceIdentifier, APIResourceType } from '../types/api';

export interface APIBridge extends APIResource<APIResourceType.Bridge> {
owner: APIResourceIdentifier;
bridge_id: string;
time_zone: {
time_zone: string;
};
}

/**
* Represents the bridge resource from the hue API
*/
Expand Down Expand Up @@ -37,3 +29,11 @@ export class Bridge extends Resource<APIBridge> {
return this.data.time_zone.time_zone;
}
}

export interface APIBridge extends APIResource<APIResourceType.Bridge> {
owner: APIResourceIdentifier;
bridge_id: string;
time_zone: {
time_zone: string;
};
}
10 changes: 5 additions & 5 deletions src/structures/BridgeHome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { Resource } from './Resource';
import { APIResource, APIResourceIdentifier, APIResourceType } from '../types/api';
import { BridgeHomeManager } from '../managers/BridgeHomeManager';

export interface APIBridgeHome extends APIResource<APIResourceType.BridgeHome> {
children: APIResourceIdentifier[];
services: APIResourceIdentifier[];
}

/**
* Represents the bridge_home resource from the hue API
*/
Expand All @@ -31,3 +26,8 @@ export class BridgeHome extends Resource<APIBridgeHome> {
return this.data.services.map((service) => service.rid);
}
}

export interface APIBridgeHome extends APIResource<APIResourceType.BridgeHome> {
children: APIResourceIdentifier[];
services: APIResourceIdentifier[];
}
40 changes: 20 additions & 20 deletions src/structures/Button.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
import { APIResource, APIResourceIdentifier, APIResourceType } from '../types/api';
import { Resource } from './Resource';

export enum ButtonLastEvent {
InitialPress = 'initial_press',
Repeat = 'repeat',
ShortRelease = 'short_release',
LongRelease = 'long_release',
DoubleShortRelease = 'double_short_release',
LongPress = 'long_press',
}

export interface APIButton extends APIResource<APIResourceType.Button> {
onwer: APIResourceIdentifier;
metadata: {
control_id: number;
};
button: {
last_event: ButtonLastEvent;
};
}

/**
* Represents the button resource from the hue API
*/
Expand All @@ -34,7 +15,26 @@ export class Button extends Resource<APIButton> {
/**
* Last event that took place
*/
get lastEvent(): ButtonLastEvent {
get lastEvent(): APIButtonLastEvent {
return this.data.button.last_event;
}
}

export enum APIButtonLastEvent {
InitialPress = 'initial_press',
Repeat = 'repeat',
ShortRelease = 'short_release',
LongRelease = 'long_release',
DoubleShortRelease = 'double_short_release',
LongPress = 'long_press',
}

export interface APIButton extends APIResource<APIResourceType.Button> {
onwer: APIResourceIdentifier;
metadata: {
control_id: number;
};
button: {
last_event: APIButtonLastEvent;
};
}
38 changes: 19 additions & 19 deletions src/structures/Device.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import { APIArcheType, APIResourceIdentifier, APIResourceType } from '../types/api';
import { APIArcheTypeResource, ArcheTypeResource, ArcheTypeResourceEditOptions } from './ArcheTypeResource';

export interface APIDevice extends APIArcheTypeResource<APIResourceType.Device> {
product_data: {
model_id: string;
manufacturer_name: string;
product_name: string;
product_archetype: APIArcheType;
certified: boolean;
software_version: string;
hardware_platform_type?: string;
};
services: APIResourceIdentifier[];
}

export enum DeviceAction {
Identify = 'identify',
}

export interface DeviceEditOptions extends ArcheTypeResourceEditOptions {
action: DeviceAction;
action: APIDeviceAction;
}

/**
Expand Down Expand Up @@ -82,7 +65,7 @@ export class Device extends ArcheTypeResource<APIDevice> {
* This causes the device (light) to blink
*/
public async identify(): Promise<string> {
return await this.edit({ action: DeviceAction.Identify });
return await this.edit({ action: APIDeviceAction.Identify });
}

/**
Expand All @@ -100,3 +83,20 @@ export class Device extends ArcheTypeResource<APIDevice> {
return await this.hue.devices.delete(this.id);
}
}

export enum APIDeviceAction {
Identify = 'identify',
}

export interface APIDevice extends APIArcheTypeResource<APIResourceType.Device> {
product_data: {
model_id: string;
manufacturer_name: string;
product_name: string;
product_archetype: APIArcheType;
certified: boolean;
software_version: string;
hardware_platform_type?: string;
};
services: APIResourceIdentifier[];
}
32 changes: 16 additions & 16 deletions src/structures/DevicePower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ import { Resource } from './Resource';
import { APIResource, APIResourceIdentifier, APIResourceType } from '../types/api';
import { DevicePowerManager } from '../managers/DevicePowerManager';

export interface APIDevicePower extends APIResource<APIResourceType.DevicePower> {
owner: APIResourceIdentifier;
power_state: {
battery_state: 'normal' | 'low' | 'critical';
battery_level: number;
};
}

export enum DevicePowerBatteryState {
Normal = 'normal',
Low = 'low',
Critical = 'critical',
}

/**
* Represents the device_power resource from the hue API
*/
Expand All @@ -36,8 +22,8 @@ export class DevicePower extends Resource<APIDevicePower> {
/**
* This device power's battery state
*/
get batteryState(): DevicePowerBatteryState {
return this.data.power_state.battery_state as DevicePowerBatteryState;
get batteryState(): APIDevicePowerBatteryState {
return this.data.power_state.battery_state as APIDevicePowerBatteryState;
}

/**
Expand All @@ -47,3 +33,17 @@ export class DevicePower extends Resource<APIDevicePower> {
return this.data.power_state.battery_level;
}
}

export enum APIDevicePowerBatteryState {
Normal = 'normal',
Low = 'low',
Critical = 'critical',
}

export interface APIDevicePower extends APIResource<APIResourceType.DevicePower> {
owner: APIResourceIdentifier;
power_state: {
battery_state: 'normal' | 'low' | 'critical';
battery_level: number;
};
}
8 changes: 4 additions & 4 deletions src/structures/GeofenceClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Resource } from './Resource';
import { APIResource, APIResourceType } from '../types/api';

export interface APIGeofenceClient extends APIResource<APIResourceType.GeofenceClient> {
name: string;
}

export interface GeofenceEditOptions {
isAtHome?: boolean;
name?: string;
Expand Down Expand Up @@ -57,3 +53,7 @@ export class GeofenceClient extends Resource<APIGeofenceClient> {
return this.hue.geofenceClients.delete(this.id);
}
}

export interface APIGeofenceClient extends APIResource<APIResourceType.GeofenceClient> {
name: string;
}
Loading

0 comments on commit 4ae6103

Please sign in to comment.