Skip to content

Commit

Permalink
bug fix in discovery of existing schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
niels committed Aug 30, 2020
1 parent 5266989 commit d16593b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dist/scheduler-card.js
100755 → 100644

Large diffs are not rendered by default.

33 changes: 28 additions & 5 deletions src/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,23 @@ export class Config {
}

AddEntityInfo(entity_id: string, cfg: IEntityConfigEntry) {
if (this.entities[entity_id]) Object.assign(this.entities[entity_id], { ...cfg });
else this.entities[entity_id] = Object.assign({ ...cfg }, { id: entity_id });
if (this.entities[entity_id]) {
Object.assign(this.entities[entity_id], _.omit({ ...cfg }, 'actions'));
if (_(cfg).has('actions')) {
_(cfg.actions).each(action => {
let match = _(this.entities[entity_id]['actions']).find(e => { return CreateSlug(_.pick(e, ['service', 'service_data'])) == CreateSlug(_.pick(action, ['service', 'service_data'])) });
if (match) return;
let actions = [... this.entities[entity_id]['actions']];
actions.push(action);
if (!match) this.entities[entity_id]['actions'] = actions;
});
}
}
else {
let entry = Object.assign({ ...cfg }, { id: entity_id });
_.defaults(entry, { actions: [] });
this.entities[entity_id] = entry;
}
if (!this.FindGroupForEntity(entity_id)) this.AddEntityToGroup(entity_id);
}

Expand Down Expand Up @@ -85,9 +100,17 @@ export class Config {
config['entity'] = getDomainFromEntityId(config['service']) + "." + config['entity'];
config['service'] = config['service'].split('.').pop();
}
this.AddEntityInfo(config['entity'], {
actions: [_.pick(config, ['service', 'service_data'])]
});
let service_data = _.omit(config, ['entity', 'service']);
if (service_data) {
this.AddEntityInfo(config['entity'], {
actions: [{ service: config['service'], service_data: service_data }]
});
}
else {
this.AddEntityInfo(config['entity'], {
actions: [_.pick(config, 'service')]
});
}
})
}

Expand Down
10 changes: 8 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export function getDomainFromEntityId(entity_id: string): string {


export function CreateSlug(input: IDictionary<any>) {
return slugify(JSON.stringify(_.values(input)).replace(/\W/g, ' '), '_');
let keys = _(input).keys();
keys = keys.sort();
let obj = {};
_(keys).each(key => obj[key] = input[key]);

return slugify(JSON.stringify(_.values(obj)).replace(/\W/g, ' '), '_');
}


Expand Down Expand Up @@ -121,14 +126,15 @@ export function ImportFromHass(hassData: any, configData: Config): IScheduleEntr
let actions: IScheduleAction[] = hassData.attributes['actions'].map(action => {
let entity_id = getDomainFromEntityId(action['entity']) ? action['entity'] : getDomainFromEntityId(action['service']) + "." + action['entity'];
let service = action['service'];
let service_data = _.omit(action, ['service', 'entity']);
if (getDomainFromEntityId(entity_id) == getDomainFromEntityId(service)) service = service.split(".").pop();

if (!configData.GetEntity(entity_id)) {
//console.log(`failed to find entity ${entity_id}!`);
return;
}

let action_id = CreateSlug(Object.assign({ service: service }, _.pick(action, 'service_data')));
let action_id = (service_data) ? CreateSlug(Object.assign({ service: service, service_data: service_data })) : CreateSlug(Object.assign({ service: service }));

if (!configData.GetAction(entity_id, action_id)) {
//console.log(`failed to find action ${action_id} for entity ${entity_id}!`);
Expand Down

0 comments on commit d16593b

Please sign in to comment.