Skip to content

Commit

Permalink
Refactor card code (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 committed Mar 4, 2024
1 parent 1b055a4 commit fb4fec3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions js/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export const CODE_SENSOR_KEY = 'code';
export const CODE_EVENT_KEY = 'pin_used';
export const PIN_SYNCED_TO_LOCKS_KEY = 'pin_synced_to_locks';
export const CONDITION_KEYS = ['number_of_uses'];
export const DIVIDER_CARD = {
type: 'divider'
};
export const KEY_ORDER = [
'name',
'enabled',
Expand Down
3 changes: 2 additions & 1 deletion js/dashboard-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ReactiveElement } from 'lit';

import { DEFAULT_INCLUDE_CODE_SLOT_SENSORS } from './const';
import { generateView } from './generate-view';
import { HomeAssistant } from './ha_type_stubs';
import { generateView, slugify } from './helpers';
import { slugify } from './slugify';
import { GetConfigEntriesResponse, LockCodeManagerDashboardStrategyConfig } from './types';

export class LockCodeManagerDashboardStrategy extends ReactiveElement {
Expand Down
45 changes: 3 additions & 42 deletions js/helpers.ts → js/generate-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CODE_EVENT_KEY,
CODE_SENSOR_KEY,
CONDITION_KEYS,
DIVIDER_CARD,
FOLD_ENTITY_ROW_SEARCH_STRING,
KEY_ORDER,
PIN_SYNCED_TO_LOCKS_KEY
Expand All @@ -13,12 +14,9 @@ import {
LovelaceResource,
LovelaceViewConfig
} from './ha_type_stubs';
import { slugify } from './slugify';
import { LockCodeManagerConfigEntryData, LockCodeManagerEntityEntry, SlotMapping } from './types';

const DIVIDER_CARD = {
type: 'divider'
};

export async function generateView(
hass: HomeAssistant,
configEntryId: string,
Expand Down Expand Up @@ -51,9 +49,7 @@ export async function generateView(
.map((entity) => {
return {
entity: entity.entity_id,
name: (entity.name ? entity.name : entity.original_name)
.replace('PIN synced to locks', 'synced')
.replace('Code slot', 'Slot'),
name: `Slot ${entity.slotNum.toString()} synced`,
type: 'state-label'
};
})
Expand Down Expand Up @@ -223,38 +219,3 @@ function maybeGenerateFoldEntityRowCard(
...entityCards
];
}

// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
export function slugify(value: string, delimiter = '-'): string {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·';
const b = `aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}`;
const p = new RegExp(a.split('').join('|'), 'g');

let slugified;

if (value === '') {
slugified = '';
} else {
slugified = value
.toString()
.toLowerCase()
// Replace special characters
.replace(p, (c) => b.charAt(a.indexOf(c)))
// Remove Commas between numbers
.replace(/(\d),(?=\d)/g, '$1')
// Replace all non-word characters
.replace(/[^a-z0-9]+/g, delimiter)
// Replace multiple delimiters with single delimiter
.replace(new RegExp(`(${delimiter})\\1+`, 'g'), '$1')
// Trim delimiter from start of text
.replace(new RegExp(`^${delimiter}+`), '')
// Trim delimiter from end of text
.replace(new RegExp(`${delimiter}+$`), '');

if (slugified === '') {
slugified = 'unknown';
}
}

return slugified;
}
34 changes: 34 additions & 0 deletions js/slugify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
export function slugify(value: string, delimiter = '-'): string {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·';
const b = `aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}`;
const p = new RegExp(a.split('').join('|'), 'g');

let slugified;

if (value === '') {
slugified = '';
} else {
slugified = value
.toString()
.toLowerCase()
// Replace special characters
.replace(p, (c) => b.charAt(a.indexOf(c)))
// Remove Commas between numbers
.replace(/(\d),(?=\d)/g, '$1')
// Replace all non-word characters
.replace(/[^a-z0-9]+/g, delimiter)
// Replace multiple delimiters with single delimiter
.replace(new RegExp(`(${delimiter})\\1+`, 'g'), '$1')
// Trim delimiter from start of text
.replace(new RegExp(`^${delimiter}+`), '')
// Trim delimiter from end of text
.replace(new RegExp(`${delimiter}+$`), '');

if (slugified === '') {
slugified = 'unknown';
}
}

return slugified;
}
2 changes: 1 addition & 1 deletion js/view-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ReactiveElement } from 'lit';

import { DEFAULT_INCLUDE_CODE_SLOT_SENSORS } from './const';
import { generateView } from './generate-view';
import { HomeAssistant } from './ha_type_stubs';
import { generateView } from './helpers';
import { LockCodeManagerEntitiesResponse, LockCodeManagerViewStrategyConfig } from './types';

export class LockCodeManagerViewStrategy extends ReactiveElement {
Expand Down

1 comment on commit fb4fec3

@chelming
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a screenshot of this to the dashboard page on the wiki?

Please sign in to comment.