Skip to content

Commit

Permalink
Remove unnecessary try catch blocks and let homey-log initially do th…
Browse files Browse the repository at this point in the history
…e handling
  • Loading branch information
Erikvl87 committed Sep 28, 2024
1 parent cb01970 commit 2d0869a
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 246 deletions.
111 changes: 52 additions & 59 deletions lib/ConditionCardAnyDeviceTurnedOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,61 @@ export default class ConditionCardAnyDeviceTurnedOn {
}

private async setup(): Promise<void> {
try {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('deviceType',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {
const deviceClasses = HomeyLib.Device.getClasses();

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

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

return results.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('deviceType',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {
const deviceClasses = HomeyLib.Device.getClasses();

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

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

return results.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
);


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)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const zonesToCheck = [zone];
if (args.includeDescendants === "1")
zonesToCheck.push(...await this.zonesDb.getAllChildren(zone.id));

const allDevices = await this.homeyApi.devices.getDevices();
const devicesToCheck = Object.values(allDevices).filter(device =>
zonesToCheck.some(zone => device.zone === zone.id) &&
(args.deviceType.id === 'any_type' || device.class === args.deviceType.id) &&
device.capabilities.includes('onoff') &&
device.capabilitiesObj['onoff'] !== null
);
} catch (error) {
this.log('Error updating condition card arguments:', error);
}

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)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const zonesToCheck = [zone];
if (args.includeDescendants === "1")
zonesToCheck.push(...await this.zonesDb.getAllChildren(zone.id));
this.log(`Checking ${devicesToCheck.length} devices in zone '${zone.name}' with class '${args.deviceType.id}' with the onoff capability.`, { devicesToCheck: devicesToCheck.map(device => device.name) });

const allDevices = await this.homeyApi.devices.getDevices();
const devicesToCheck = Object.values(allDevices).filter(device =>
zonesToCheck.some(zone => device.zone === zone.id) &&
(args.deviceType.id === 'any_type' || device.class === args.deviceType.id) &&
device.capabilities.includes('onoff') &&
device.capabilitiesObj['onoff'] !== null
);
if (devicesToCheck.length === 0) {
this.log(`No devices with class '${args.deviceType.id}' found in zone '${zone.name}'.`, { args, zone });
// Throwing this error will make sure that the flow card will not be triggered if there are no devices in the zone.
throw new Error(`No devices found in zone '${zone.name}' with class '${args.deviceType.id}'.`);
}

this.log(`Checking ${devicesToCheck.length} devices in zone '${zone.name}' with class '${args.deviceType.id}' with the onoff capability.`, { devicesToCheck: devicesToCheck.map(device => device.name) });

if (devicesToCheck.length === 0) {
this.log(`No devices with class '${args.deviceType.id}' found in zone '${zone.name}'.`, { args, zone });
// Throwing this error will make sure that the flow card will not be triggered if there are no devices in the zone.
throw new Error(`No devices found in zone '${zone.name}' with class '${args.deviceType.id}'.`);
}

return devicesToCheck.some(device => device.capabilitiesObj['onoff'].value === true);
});
} catch (error) {
this.log('Error registering run listener:', error);
}
return devicesToCheck.some(device => device.capabilitiesObj['onoff'].value === true);
});
}
}
160 changes: 75 additions & 85 deletions lib/ConditionCardEvaluateSensorCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,94 +21,84 @@ export default class ConditionCardEvaluateSensorCapabilities {
}

private async setup(): Promise<void> {
try {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('capability',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {

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());
});
}
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
this.conditionCard.registerArgumentAutocompleteListener('capability',
async (query: string): Promise<FlowCard.ArgumentAutocompleteResults> => {

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());
});
}
);

this.conditionCard.registerRunListener(async (args, _state) => {
const capability = args.capability.id;
this.log(`Checking sensors in zone '${args.zone.id}' with capability '${capability}'.`);
const zone = await this.zonesDb.getZone(args.zone.id);

if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const zonesToCheck = [zone];
if (args.includeDescendants === "1")
zonesToCheck.push(...await this.zonesDb.getAllChildren(zone.id));

const allDevices = await this.homeyApi.devices.getDevices();
const devicesToCheck = Object.values(allDevices).filter(device =>
zonesToCheck.some(zone => device.zone === zone.id) &&
device.capabilities.includes(capability) &&
device.capabilitiesObj[capability] !== null &&
typeof device.capabilitiesObj[capability].value === 'number'
);

} catch (error) {
this.log('Error updating condition card arguments:', error);
}

try {

this.conditionCard.registerRunListener(async (args, _state) => {
const capability = args.capability.id;
this.log(`Checking sensors in zone '${args.zone.id}' with capability '${capability}'.`);
const zone = await this.zonesDb.getZone(args.zone.id);

if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const zonesToCheck = [zone];
if (args.includeDescendants === "1")
zonesToCheck.push(...await this.zonesDb.getAllChildren(zone.id));

const allDevices = await this.homeyApi.devices.getDevices();
const devicesToCheck = Object.values(allDevices).filter(device =>
zonesToCheck.some(zone => device.zone === zone.id) &&
device.capabilities.includes(capability) &&
device.capabilitiesObj[capability] !== null &&
typeof device.capabilitiesObj[capability].value === 'number'
);

this.log(`Checking ${devicesToCheck.length} devices in zone '${zone.name}' with capability '${capability}'.`, { devicesToCheck: devicesToCheck.map(device => device.name) });

if (devicesToCheck.length === 0) {
this.log(`No devices with capability '${capability}' found in zone '${zone.name}'.`, { args, zone });
// Throwing this error will make sure that the flow card will not be triggered if there are no devices in the zone.
throw new Error(`No devices found in zone '${zone.name}' with capability '${capability}'.`);
this.log(`Checking ${devicesToCheck.length} devices in zone '${zone.name}' with capability '${capability}'.`, { devicesToCheck: devicesToCheck.map(device => device.name) });

if (devicesToCheck.length === 0) {
this.log(`No devices with capability '${capability}' found in zone '${zone.name}'.`, { args, zone });
// Throwing this error will make sure that the flow card will not be triggered if there are no devices in the zone.
throw new Error(`No devices found in zone '${zone.name}' with capability '${capability}'.`);
}

const equation = args.equation;
const otherValue = args.value;
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}'.`);
}

const equation = args.equation;
const otherValue = args.value;
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}'.`);
}
});

});
} catch (error) {
this.log('Error registering run listener:', error);
}

});
}
}
59 changes: 25 additions & 34 deletions lib/ConditionCardZoneActiveForMinutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,30 @@ export default class ConditionCardZoneActiveForMinutes {
}

private async setup(): Promise<void> {
try {
this.conditionCard.registerRunListener(async (args, _state) => {
const zone = await this.zonesDb.getZone(args.zone.id);
if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const now = new Date();
const checkIsActive = args.state === 'active';

if(checkIsActive) {
const isZoneActive = (zone.activeLastUpdated !== null && zone.active);
const activeForGivenMinutes = (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;
const isActive = isZoneActive && activeForGivenMinutes;
this.log(`Zone '${zone.name}' is considered ${isActive ? 'active' : 'inactive'}.`, { args, zone });
return isActive;

} else {
const isZoneInactive = !(zone.activeLastUpdated === null ? false : zone.active);
const lastUpdatedWithinGivenMinutes = (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;
const isInactive = isZoneInactive && lastUpdatedWithinGivenMinutes;
this.log(`Zone '${zone.name}' is considered ${isInactive ? 'inactive' : 'active'}.`, { args, zone });
return isInactive;
}
});
} catch (error) {
this.log('Error registering run listener:', error);
}

try {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
}
catch (error) {
this.log('Error updating condition card arguments:', error);
}
this.conditionCard.registerRunListener(async (args, _state) => {
const zone = await this.zonesDb.getZone(args.zone.id);
if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const now = new Date();
const checkIsActive = args.state === 'active';

if(checkIsActive) {
const isZoneActive = (zone.activeLastUpdated !== null && zone.active);
const activeForGivenMinutes = (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;
const isActive = isZoneActive && activeForGivenMinutes;
this.log(`Zone '${zone.name}' is considered ${isActive ? 'active' : 'inactive'}.`, { args, zone });
return isActive;

} else {
const isZoneInactive = !(zone.activeLastUpdated === null ? false : zone.active);
const lastUpdatedWithinGivenMinutes = (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;
const isInactive = isZoneInactive && lastUpdatedWithinGivenMinutes;
this.log(`Zone '${zone.name}' is considered ${isInactive ? 'inactive' : 'active'}.`, { args, zone });
return isInactive;
}
});

this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
}
}
31 changes: 11 additions & 20 deletions lib/ConditionCardZoneInactiveForMinutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,19 @@ export default class ConditionCardZoneInactiveForMinutes {
}

private async setup(): Promise<void> {
try {
this.conditionCard.registerRunListener(async (args, _state) => {
const zone = await this.zonesDb.getZone(args.zone.id);
if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);
this.conditionCard.registerRunListener(async (args, _state) => {
const zone = await this.zonesDb.getZone(args.zone.id);
if (zone == null)
throw new Error(`Zone with id '${args.zone.id}' not found.`);

const now = new Date();
const isInactive = zone.activeLastUpdated === null ? true
: zone.active ? false : (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;
const now = new Date();
const isInactive = zone.activeLastUpdated === null ? true
: zone.active ? false : (now.getTime() - new Date(zone.activeLastUpdated).getTime()) >= args.minutes * 60 * 1000;

this.log(`Zone '${zone.name}' is considered ${isInactive ? 'inactive' : 'active'}.`, { args, zone });
return isInactive;
});
} catch (error) {
this.log('Error registering run listener:', error);
}
this.log(`Zone '${zone.name}' is considered ${isInactive ? 'inactive' : 'active'}.`, { args, zone });
return isInactive;
});

try {
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
}
catch (error) {
this.log('Error updating condition card arguments:', error);
}
this.conditionCard.registerArgumentAutocompleteListener('zone', async (query: string) => await handleZoneAutocomplete(query, this.zonesDb));
}
}
Loading

0 comments on commit 2d0869a

Please sign in to comment.