Skip to content

Commit

Permalink
fix: TS refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 5, 2023
1 parent 9b361c8 commit e1f8b3c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib/extend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as exposes from './exposes';
import tz from '../converters/toZigbee';
import fz from '../converters/fromZigbee';
import light from './light';
import * as light from './light';
const e = exposes.presets;

const extend = {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/kelvinToXy.js → src/lib/kelvinToXy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const lookup = {
const lookup: {[k: number]: {x: number, y: number}} = {
1000: {x: 0.652750055750174, y: 0.344462227197370},
1010: {x: 0.651338820005714, y: 0.345710187476526},
1020: {x: 0.649930067872860, y: 0.346948777634801},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fromZigbeeConverters from '../converters/fromZigbee';
const fromZigbeeStore: KeyValueAny = {};
import * as exposes from './exposes';
import constants from './constants';
import light from './light';
import * as light from './light';

interface KeyValueAny {[s: string]: any}

Expand Down
27 changes: 13 additions & 14 deletions src/lib/light.js → src/lib/light.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const utils = require('./utils');
import utils from './utils';
import {isGroup} from './utils2';

async function readColorCapabilities(endpoint) {
async function readColorCapabilities(endpoint: zh.Endpoint) {
await endpoint.read('lightingColorCtrl', ['colorCapabilities']);
}

async function readColorTempMinMax(endpoint) {
async function readColorTempMinMax(endpoint: zh.Endpoint) {
await endpoint.read('lightingColorCtrl', ['colorTempPhysicalMin', 'colorTempPhysicalMax']);
}

function readColorAttributes(entity, meta, additionalAttributes=[]) {
function readColorAttributes(entity: zh.Endpoint | zh.Group, meta: tz.Meta, additionalAttributes: string[]=[]) {
/**
* Not all bulbs support the same features, we need to take care we read what is supported.
* `supportsHueAndSaturation` indicates support for currentHue and currentSaturation
Expand Down Expand Up @@ -44,18 +45,18 @@ function readColorAttributes(entity, meta, additionalAttributes=[]) {
return [...attributes, ...additionalAttributes];
}

function findColorTempRange(entity, logger) {
function findColorTempRange(entity: zh.Endpoint | zh.Group, logger: Logger) {
let colorTempMin;
let colorTempMax;
if (entity.constructor.name === 'Group') {
if (isGroup(entity)) {
const minCandidates = entity.members.map(
(m) => m.getClusterAttributeValue('lightingColorCtrl', 'colorTempPhysicalMin'),
(m) => Number(m.getClusterAttributeValue('lightingColorCtrl', 'colorTempPhysicalMin')),
).filter((v) => v != null);
if (minCandidates.length > 0) {
colorTempMin = Math.max(...minCandidates);
}
const maxCandidates = entity.members.map(
(m) => m.getClusterAttributeValue('lightingColorCtrl', 'colorTempPhysicalMax'),
(m) => Number(m.getClusterAttributeValue('lightingColorCtrl', 'colorTempPhysicalMax')),
).filter((v) => v != null);
if (maxCandidates.length > 0) {
colorTempMax = Math.min(...maxCandidates);
Expand All @@ -65,14 +66,13 @@ function findColorTempRange(entity, logger) {
colorTempMax = entity.getClusterAttributeValue('lightingColorCtrl', 'colorTempPhysicalMax');
}
if ((colorTempMin == null) || (colorTempMax == null)) {
const entityType = entity.constructor.name.toLowerCase();
const entityId = (entityType === 'group') ? entity.groupID : entity.deviceIeeeAddress;
logger.debug(`Missing colorTempPhysicalMin and/or colorTempPhysicalMax for ${entityType} ${entityId}!`);
const entityId = isGroup(entity) ? entity.groupID : entity.deviceIeeeAddress;
logger.debug(`Missing colorTempPhysicalMin and/or colorTempPhysicalMax for ${isGroup(entity) ? 'group' : 'endpoint'} ${entityId}!`);
}
return [colorTempMin, colorTempMax];
}

function clampColorTemp(colorTemp, colorTempMin, colorTempMax, logger) {
export function clampColorTemp(colorTemp: number, colorTempMin: number, colorTempMax: number, logger: Logger) {
if ((colorTempMin != null) && (colorTemp < colorTempMin)) {
logger.debug(`Requested color_temp ${colorTemp} is lower than minimum supported ${colorTempMin}, using minimum!`);
return colorTempMin;
Expand All @@ -83,8 +83,7 @@ function clampColorTemp(colorTemp, colorTempMin, colorTempMax, logger) {
}
return colorTemp;
}

async function configure(device, coordinatorEndpoint, logger, readColorTempMinMaxAttribute) {
export async function configure(device: zh.Device, coordinatorEndpoint: zh.Endpoint, logger: Logger, readColorTempMinMaxAttribute: boolean) {
if (device.powerSource === 'Unknown') {
device.powerSource = 'Mains (single phase)';
device.save();
Expand Down
8 changes: 8 additions & 0 deletions src/lib/utils2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export function getFromLookup<V>(value: unknown, lookup: {[s: number | string]:
export function assertEndpoint(obj: unknown): asserts obj is zh.Endpoint {
if (obj?.constructor?.name?.toLowerCase() !== 'endpoint') throw new Error('Not an endpoint');
}

export function isEndpoint(obj: zh.Endpoint | zh.Group): obj is zh.Endpoint {
return obj.constructor.name.toLowerCase() === 'endpoint';
}

export function isGroup(obj: zh.Endpoint | zh.Group): obj is zh.Group {
return obj.constructor.name.toLowerCase() === 'group';
}

0 comments on commit e1f8b3c

Please sign in to comment.