Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Erikvl87 committed Sep 28, 2024
1 parent 84ca023 commit ff9a218
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 599 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
15 changes: 8 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,18 @@ 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],
id: key,
};
}));

Expand All @@ -53,7 +54,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
57 changes: 31 additions & 26 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 @@ -16,7 +16,7 @@ export default class ConditionCardEvaluateSensorCapabilities {
public static async initialize(homey: Homey, homeyApi: ExtendedHomeyAPIV3Local, zonesDb: ZonesDb, log: (...args: unknown[]) => void): Promise<void> {
if (ConditionCardEvaluateSensorCapabilities.instance === null) {
ConditionCardEvaluateSensorCapabilities.instance = new ConditionCardEvaluateSensorCapabilities(homey, homeyApi, zonesDb, log);
await ConditionCardEvaluateSensorCapabilities.instance;
await ConditionCardEvaluateSensorCapabilities.instance.setup();
}
}

Expand All @@ -25,14 +25,19 @@ 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')
.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 Expand Up @@ -79,23 +84,23 @@ export default class ConditionCardEvaluateSensorCapabilities {
return devicesToCheck.some(device => {
const value = device.capabilitiesObj[capability].value as number;
switch (equation) {
case 'equals': {
const result = value === otherValue;
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'equal' : 'not equal'} to '${otherValue}'.`, { device: device.name, args });
return result;
}
case 'is-greater-than': {
const result = value > otherValue;
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'greater' : 'not greater'} than '${otherValue}'.`, { device: device.name, args });
return result;
}
case 'is-less-than': {
const result = value < otherValue;
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'less' : 'not less'} than '${otherValue}'.`, { device: device.name, args });
return result;
}
default:
throw new Error(`Unknown equation '${equation}'.`);
case 'equals': {

Check failure on line 87 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 5 tabs but found 6
const result = value === otherValue;

Check failure on line 88 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'equal' : 'not equal'} to '${otherValue}'.`, { device: device.name, args });

Check failure on line 89 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
return result;

Check failure on line 90 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
}

Check failure on line 91 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 5 tabs but found 6
case 'is-greater-than': {

Check failure on line 92 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 5 tabs but found 6
const result = value > otherValue;

Check failure on line 93 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'greater' : 'not greater'} than '${otherValue}'.`, { device: device.name, args });

Check failure on line 94 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
return result;

Check failure on line 95 in lib/ConditionCardEvaluateSensorCapabilities.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 tabs but found 7
}
case 'is-less-than': {
const result = value < otherValue;
this.log(`Device '${device.name}' has value '${value}' for capability '${capability}' which is ${result ? 'less' : 'not less'} than '${otherValue}'.`, { device: device.name, args });
return result;
}
default:
throw new Error(`Unknown equation '${equation}'.`);
}
});

Expand Down
Loading

0 comments on commit ff9a218

Please sign in to comment.