Skip to content

Commit

Permalink
Use the homey-lib as the source for device classes and capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Erikvl87 committed Sep 28, 2024
1 parent 8dd92fa commit b2d3161
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 620 deletions.
96 changes: 96 additions & 0 deletions @types/my-extensions/homey-lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
declare module 'homey-lib' {
interface ITranslationObject {
en: string;
nl: string;
de: string;
fr: string;
it: string;
sv: string;
no: string;
es: string;
da: string;
ru: string;
pl: string;
ko: string;
[key: string]: string;
}

interface IDeviceClass {
title: ITranslationObject;
description: ITranslationObject;
virtualTitle?: ITranslationObject;
minCompatibility?: string;
virtualTitle?: ITranslationObject;
allowedVirtual?: string[];
}

interface ICapability {
title: ITranslationObject;
desc?: ITranslationObject;
type: string;
gettable: boolean
settable: boolean;
insights?: boolean;
insightsTitleTrue?: ITranslationObject;
insightsTitleFalse?: ITranslationObject;
uiComponent: string;
minCompatibility?: string;
}

interface IDeviceClasses {
[key: string]: IDeviceClass;
}

interface ICapabilities {
[key: string]: ICapability;
}

export class HomeyLib {
static getLocales(): unknown;
static getCategories(): unknown;
static getPermissions(): unknown;
static getBrandColor(): unknown;
}

export class Device {
static getClasses(): IDeviceClasses;
static getClass(id: string): IDeviceClass;
static getCapabilities(): ICapability;
}

export class Capability {
static getCapabilities(): ICapabilities;
static getCapability(id: string): ICapability;
static hasCapability(id: string): boolean;
}

export class Signal {
// Define Signal class methods and properties if available
}

export class Media {
static getCodecs(): unknown;
}

export class Energy {
static getCurrencies(): unknown;
static getBatteries(): unknown;
}

export function getDeviceClasses(): Record<string, unknown>;
export function getDeviceClass(id: string): unknown;

export function getCapabilities(): unknown;
export function getCapability(id: string): unknown;
export function hasCapability(id: string): boolean;

export function getAppLocales(): unknown;
export function getAppCategories(): unknown;
export function getAppPermissions(): unknown;
export function getAppBrandColor(): unknown;

export function getMediaCodecs(): unknown;

export function getCurrencies(): unknown;
export function getBatteries(): unknown;
}
1 change: 1 addition & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ConditionCardEvaluateSensorCapabilities from './lib/ConditionCardEvaluate
import ConditionCardZoneActiveForMinutes from './lib/ConditionCardZoneActiveForMinutes';
import TriggerCardAnyDeviceTurnedOn from './lib/TriggerCardAnyDeviceOnOff';
import ZonesDb from './lib/ZonesDb';
import HomeyLib from 'homey-lib';

Check failure on line 10 in app.ts

View workflow job for this annotation

GitHub Actions / eslint

'HomeyLib' is defined but never used. Allowed unused vars must match /^_/u

class ZoneActivity extends Homey.App {
/**
Expand Down
16 changes: 9 additions & 7 deletions lib/ConditionCardAnyDeviceTurnedOn.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ExtendedHomeyAPIV3Local } from "homey-api";
import Homey from "homey/lib/Homey";
import { DeviceClassManager } from "./DeviceClassManager";
import { FlowCard, FlowCardCondition } from "homey";
import handleZoneAutocomplete from "../utils/handleZoneAutocomplete";
import ZonesDb from "./ZonesDb";
import HomeyLib from "homey-lib";

export default class ConditionCardAnyDeviceTurnedOn {

private static instance: ConditionCardAnyDeviceTurnedOn | null = null;

conditionCard: FlowCardCondition;
Expand All @@ -27,17 +27,19 @@ export default class ConditionCardAnyDeviceTurnedOn {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('deviceType',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {
const deviceClasses = DeviceClassManager.getAllDeviceClasses();
const deviceClasses = HomeyLib.Device.getClasses();

const results = [{
name: this.homey.__('any_type') ?? "Any type",
id: 'any_type',
}];

results.push(...Object.values(deviceClasses).map((deviceClass) => {
const languageCode = this.homey.i18n.getLanguage();
results.push(...Object.entries(deviceClasses).map(([key, deviceClass]) => {
return {
name: this.homey.__(deviceClass.id) ?? deviceClass.friendlyName,
id: deviceClass.id,
name: deviceClass.title[languageCode],
description: deviceClass.description?.[languageCode],
id: key,
};
}));

Expand All @@ -53,7 +55,7 @@ export default class ConditionCardAnyDeviceTurnedOn {
try {
this.conditionCard.registerRunListener(async (args, _state) => {
this.log(`Checking devices in zone '${args.zone.id}' with class '${args.deviceType.id}'.`);

const zone = await this.zonesDb.getZone(args.zone.id);

if (zone == null)
Expand Down
25 changes: 16 additions & 9 deletions lib/ConditionCardEvaluateSensorCapabilities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FlowCard, FlowCardCondition } from "homey";
import { ExtendedHomeyAPIV3Local } from "homey-api";
import Homey from "homey/lib/Homey";
import { SensorCapabilitiesManager } from "./SensorCapabilitiesManager";
import handleZoneAutocomplete from "../utils/handleZoneAutocomplete";
import ZonesDb from "./ZonesDb";
import HomeyLib from "homey-lib";

export default class ConditionCardEvaluateSensorCapabilities {
private static instance: ConditionCardEvaluateSensorCapabilities | null = null;
Expand All @@ -25,14 +25,21 @@ export default class ConditionCardEvaluateSensorCapabilities {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('capability',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {
const deviceClasses = SensorCapabilitiesManager.getAllSensorCapabilities();

const results = [...Object.values(deviceClasses).map((capability) => {
return {
name: this.homey.__(capability.id) ?? capability.friendlyName,
id: capability.id,
};
})];

const capabilities = HomeyLib.Capability.getCapabilities();

const languageCode = this.homey.i18n.getLanguage();
const results = [...Object.entries(capabilities)
.filter(([_key, capability]) =>
capability.uiComponent === 'sensor' && capability.type === 'number'
)
.map(([key, capability]) => {
return {
name: capability.title[languageCode],
description: capability.desc?.[languageCode],
id: key,
};
})];

return results.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
Expand Down
Loading

0 comments on commit b2d3161

Please sign in to comment.