ha-nunjucks 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -24,12 +24,50 @@ if (version(packageInfo.version).compare(window.haNunjucks.version || '0.0.0') >
24
24
  // Setup on first import
25
25
  handleWhenReady(() => {
26
26
  const ha = document.querySelector('home-assistant');
27
- // Label registry and states object
28
- window.haNunjucks.hass = ha.hass;
29
- fetchLabelRegistry(ha.hass);
30
- fetchEntityRegistry(ha.hass);
31
- fetchConfigEntries(ha.hass);
32
- fetchRepairsIssues(ha.hass);
27
+ // Initialize window object
28
+ window.haNunjucks = {
29
+ ...window.haNunjucks,
30
+ hass: ha.hass,
31
+ labelRegistry: {
32
+ event: 'label_registry_updated',
33
+ fetchRegistry: fetchLabelRegistry,
34
+ labelId: {},
35
+ name2LabelId: {},
36
+ },
37
+ entityRegistry: {
38
+ event: 'entity_registry_updated',
39
+ fetchRegistry: fetchEntityRegistry,
40
+ entityId2ConfigEntryId: {},
41
+ configEntryId2EntityIds: {},
42
+ },
43
+ configEntries: {
44
+ event: 'config_entries/subscribe',
45
+ fetchRegistry: fetchConfigEntries,
46
+ entryId: {},
47
+ title2EntryId: {},
48
+ },
49
+ repairsIssues: {
50
+ event: 'repairs/list_issues',
51
+ fetchRegistry: fetchRepairsIssues,
52
+ issues: {},
53
+ },
54
+ };
55
+ const registries = [
56
+ 'labelRegistry',
57
+ 'entityRegistry',
58
+ 'configEntries',
59
+ 'repairsIssues',
60
+ ];
61
+ for (const registry of registries) {
62
+ window.haNunjucks[registry].fetchRegistry(ha.hass);
63
+ ha.hass.connection.subscribeEvents(() => {
64
+ clearTimeout(window.haNunjucks[registry].timeout);
65
+ window.haNunjucks[registry].timeout = setTimeout(() => {
66
+ window.haNunjucks[registry].fetchRegistry(ha.hass);
67
+ }, 500);
68
+ }, window.haNunjucks[registry].event);
69
+ }
70
+ // States object
33
71
  buildStatesObject();
34
72
  // Number and datetime translators
35
73
  window.haNunjucks.numberFormat = new Intl.NumberFormat(ha.hass.language);
@@ -45,7 +83,7 @@ if (version(packageInfo.version).compare(window.haNunjucks.version || '0.0.0') >
45
83
  }, () => {
46
84
  const ha = document.querySelector('home-assistant');
47
85
  return ha?.hass?.connected && ha?.hass?.connection?.connected;
48
- }, 10000, 1, 'ha-nunjucks failed to initialize - Home Assistant connection timeout');
86
+ }, 10000, 10, 'ha-nunjucks failed to initialize - Home Assistant connection timeout');
49
87
  // Initialize global ha-nunjucks environment
50
88
  nunjucks.installJinjaCompat();
51
89
  window.haNunjucks.env = addTests(addFilters(addGlobals(nunjucks.configure(`${window.location.origin}/local`))));
@@ -1,6 +1,6 @@
1
1
  import { HomeAssistant } from '../models/interfaces/hass';
2
2
  export declare function fetchConfigEntries(hass: HomeAssistant): Promise<void>;
3
- export declare function config_entry_id(entity_id: string): string | undefined;
3
+ export declare function config_entry_id(entity_id: string): string;
4
4
  declare const ConfigEntryAttributes: readonly ["domain", "title", "state", "source", "disabled_by"];
5
5
  type ConfigEntryAttribute = (typeof ConfigEntryAttributes)[number];
6
6
  export declare function config_entry_attr(config_entry_id: string, attr: ConfigEntryAttribute): string | undefined;
@@ -1,10 +1,22 @@
1
1
  export async function fetchConfigEntries(hass) {
2
- window.haNunjucks.configEntries = await hass.callWS({
2
+ const entries = await hass.callWS({
3
3
  type: 'config_entries/get',
4
4
  });
5
+ const entryId = {};
6
+ const title2EntryId = {};
7
+ for (const entry of entries) {
8
+ entryId[entry.entry_id] = entry;
9
+ title2EntryId[entry.title] ??= [];
10
+ title2EntryId[entry.title].push(entry.entry_id);
11
+ }
12
+ window.haNunjucks.configEntries = {
13
+ ...window.haNunjucks.configEntries,
14
+ entryId,
15
+ title2EntryId,
16
+ };
5
17
  }
6
18
  export function config_entry_id(entity_id) {
7
- return window.haNunjucks.entityRegistry.find((entry) => entry.entity_id == entity_id)?.config_entry_id;
19
+ return window.haNunjucks.entityRegistry.entityId2ConfigEntryId[entity_id];
8
20
  }
9
21
  const ConfigEntryAttributes = [
10
22
  'domain',
@@ -17,5 +29,5 @@ export function config_entry_attr(config_entry_id, attr) {
17
29
  if (!ConfigEntryAttributes.includes(attr)) {
18
30
  throw Error('Invalid config entry attribute');
19
31
  }
20
- return window.haNunjucks.configEntries.find((entry) => entry.entry_id == config_entry_id)?.[attr];
32
+ return window.haNunjucks.configEntries.entryId[config_entry_id]?.[attr];
21
33
  }
@@ -1,7 +1,21 @@
1
1
  export async function fetchEntityRegistry(hass) {
2
- window.haNunjucks.entityRegistry = await hass.connection.sendMessagePromise({
2
+ const entities = await hass.connection.sendMessagePromise({
3
3
  type: 'config/entity_registry/list',
4
4
  });
5
+ const entityId2ConfigEntryId = {};
6
+ const configEntryId2EntityIds = {};
7
+ for (const entity of entities) {
8
+ if (entity.config_entry_id) {
9
+ entityId2ConfigEntryId[entity.entity_id] = entity.config_entry_id;
10
+ configEntryId2EntityIds[entity.config_entry_id] ??= [];
11
+ configEntryId2EntityIds[entity.config_entry_id].push(entity.entity_id);
12
+ }
13
+ }
14
+ window.haNunjucks.entityRegistry = {
15
+ ...window.haNunjucks.entityRegistry,
16
+ entityId2ConfigEntryId,
17
+ configEntryId2EntityIds,
18
+ };
5
19
  }
6
20
  export function is_hidden_entity(hass, entity_id) {
7
21
  try {
package/dist/utils/iif.js CHANGED
@@ -11,12 +11,12 @@ export function iif(hass, condition, if_true, if_false, if_none) {
11
11
  return if_none;
12
12
  }
13
13
  }
14
- const template = `
15
- {% if ${condition} %}
16
- ${if_true ?? true}
17
- {% else %}
18
- ${if_false ?? false}
19
- {% endif %}
14
+ const template = `
15
+ {% if ${condition} %}
16
+ ${if_true ?? true}
17
+ {% else %}
18
+ ${if_false ?? false}
19
+ {% endif %}
20
20
  `;
21
21
  return renderTemplate(hass, template);
22
22
  }
@@ -1,2 +1,2 @@
1
1
  import { HomeAssistant } from '../models/interfaces/hass';
2
- export declare function integration_entities(hass: HomeAssistant, integration: string): (string | undefined)[];
2
+ export declare function integration_entities(hass: HomeAssistant, integration: string): string[];
@@ -15,11 +15,9 @@ export function integration_entities(hass, integration) {
15
15
  return entityIds;
16
16
  }
17
17
  // Config entry title
18
- const configEntryIds = window.haNunjucks.configEntries
19
- .filter((entry) => entry.title == integration)
20
- .map((entry) => entry.entry_id);
18
+ const configEntryIds = window.haNunjucks.configEntries.title2EntryId[integration];
21
19
  for (const entryId of configEntryIds) {
22
- entityIds.push(window.haNunjucks.entityRegistry.find((entity) => entity.config_entry_id == entryId)?.entity_id);
20
+ entityIds.push(...window.haNunjucks.entityRegistry.configEntryId2EntityIds[entryId]);
23
21
  }
24
22
  entityIds.sort();
25
23
  return entityIds.filter(Boolean);
@@ -2,13 +2,15 @@ export async function fetchRepairsIssues(hass) {
2
2
  const repairsIssues = (await hass.connection.sendMessagePromise({
3
3
  type: 'repairs/list_issues',
4
4
  })).issues;
5
+ const issues = {};
5
6
  for (const issue of repairsIssues) {
6
- window.haNunjucks.repairsIssues[`${issue.issue_domain || issue.domain},${issue.issue_id}`] = issue;
7
+ issues[`${issue.issue_domain || issue.domain},${issue.issue_id}`] = issue;
7
8
  }
9
+ window.haNunjucks.repairsIssues.issues = issues;
8
10
  }
9
11
  export function issues() {
10
- return window.haNunjucks.repairsIssues;
12
+ return window.haNunjucks.repairsIssues.issues;
11
13
  }
12
14
  export function issue(domain, issue_id) {
13
- return window.haNunjucks.repairsIssues[`${domain},${issue_id}`];
15
+ return window.haNunjucks.repairsIssues.issues[`${domain},${issue_id}`];
14
16
  }
@@ -1,8 +1,8 @@
1
1
  import { HomeAssistant } from '../models/interfaces/hass';
2
2
  export declare function fetchLabelRegistry(hass: HomeAssistant): Promise<void>;
3
3
  export declare function labels(hass: HomeAssistant, lookup_value?: string): string[];
4
- export declare function label_id(lookup_value: string): string | undefined;
5
- export declare function label_name(lookup_value: string): string | undefined;
4
+ export declare function label_id(lookup_value: string): string;
5
+ export declare function label_name(lookup_value: string): string;
6
6
  export declare function label_description(lookup_value: string): string | undefined;
7
7
  export declare function label_areas(hass: HomeAssistant, label_name_or_id: string): string[];
8
8
  export declare function label_devices(hass: HomeAssistant, label_name_or_id: string): string[];
@@ -3,12 +3,22 @@ export async function fetchLabelRegistry(hass) {
3
3
  type: 'config/label_registry/list',
4
4
  });
5
5
  labels.sort((ent1, ent2) => ent1.name.localeCompare(ent2.name));
6
- window.haNunjucks.labelRegistry = labels;
6
+ const labelId = {};
7
+ const name2LabelId = {};
8
+ for (const label of labels) {
9
+ labelId[label.label_id] = label;
10
+ name2LabelId[label.name] = label.label_id;
11
+ }
12
+ window.haNunjucks.labelRegistry = {
13
+ ...window.haNunjucks.labelRegistry,
14
+ labelId,
15
+ name2LabelId,
16
+ };
7
17
  }
8
18
  export function labels(hass, lookup_value) {
9
19
  try {
10
20
  if (!lookup_value) {
11
- return window.haNunjucks.labelRegistry.map((entry) => entry.label_id);
21
+ return Object.keys(window.haNunjucks.labelRegistry.labelId);
12
22
  }
13
23
  return (hass.entities[lookup_value]?.labels ??
14
24
  hass.devices[lookup_value]?.labels ??
@@ -20,20 +30,20 @@ export function labels(hass, lookup_value) {
20
30
  }
21
31
  }
22
32
  export function label_id(lookup_value) {
23
- return window.haNunjucks.labelRegistry.find((entry) => entry.name == lookup_value)?.label_id;
33
+ return window.haNunjucks.labelRegistry.name2LabelId[lookup_value];
24
34
  }
25
35
  export function label_name(lookup_value) {
26
- return window.haNunjucks.labelRegistry.find((entry) => entry.label_id == lookup_value)?.name;
36
+ return window.haNunjucks.labelRegistry.labelId[lookup_value]?.name;
27
37
  }
28
38
  export function label_description(lookup_value) {
29
- return window.haNunjucks.labelRegistry.find((entry) => entry.label_id == lookup_value)?.description;
39
+ return window.haNunjucks.labelRegistry.labelId[lookup_value]?.description;
30
40
  }
31
41
  export function label_areas(hass, label_name_or_id) {
32
42
  try {
33
43
  const areaIds = [];
34
44
  let labelId = undefined;
35
45
  if (label_name_or_id) {
36
- if (window.haNunjucks.labelRegistry.find((entry) => entry.label_id == label_name_or_id)) {
46
+ if (window.haNunjucks.labelRegistry.labelId[label_name_or_id]) {
37
47
  labelId = label_name_or_id;
38
48
  }
39
49
  else {
@@ -60,7 +70,7 @@ export function label_devices(hass, label_name_or_id) {
60
70
  const deviceIds = [];
61
71
  if (label_name_or_id) {
62
72
  let labelId = undefined;
63
- if (window.haNunjucks.labelRegistry.find((entry) => entry.label_id == label_name_or_id)) {
73
+ if (window.haNunjucks.labelRegistry.labelId[label_name_or_id]) {
64
74
  labelId = label_name_or_id;
65
75
  }
66
76
  else {
@@ -87,7 +97,7 @@ export function label_entities(hass, label_name_or_id) {
87
97
  const entityIds = [];
88
98
  if (label_name_or_id) {
89
99
  let labelId = undefined;
90
- if (window.haNunjucks.labelRegistry.find((entry) => entry.label_id == label_name_or_id)) {
100
+ if (window.haNunjucks.labelRegistry.labelId[label_name_or_id]) {
91
101
  labelId = label_name_or_id;
92
102
  }
93
103
  else {
package/package.json CHANGED
@@ -1,63 +1,63 @@
1
- {
2
- "name": "ha-nunjucks",
3
- "version": "1.7.0",
4
- "description": "Wrapper for nunjucks for use with Home Assistant frontend custom components to render templates",
5
- "main": "./dist/index.js",
6
- "files": [
7
- "dist/**"
8
- ],
9
- "type": "module",
10
- "scripts": {
11
- "test": "ts-mocha tests/**/*.test.ts --require tests/fixtures.mjs",
12
- "build": "tsc",
13
- "prelint": "tsc --noemit",
14
- "lint": "eslint --config ./eslint.config.js",
15
- "setup": "git config --add core.hooksPath githooks && chmod +x githooks/pre-commit && npm i"
16
- },
17
- "lint-staged": {
18
- "*": "prettier -w"
19
- },
20
- "author": "Nerwyn",
21
- "license": "Apache-2.0",
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/Nerwyn/ha-nunjucks.git"
25
- },
26
- "bugs": {
27
- "url": "https://github.com/Nerwyn/ha-nunjucks/issues"
28
- },
29
- "homepage": "https://github.com/Nerwyn/ha-nunjucks#readme",
30
- "dependencies": {
31
- "buffer": "latest",
32
- "create-hash": "latest",
33
- "home-assistant-js-websocket": "latest",
34
- "mersenne-twister": "latest",
35
- "nunjucks": "latest",
36
- "python-struct": "latest",
37
- "slugify": "latest",
38
- "stream": "npm:stream-browserify@latest",
39
- "ts-py-datetime": "latest"
40
- },
41
- "devDependencies": {
42
- "@types/create-hash": "latest",
43
- "@types/mersenne-twister": "latest",
44
- "@types/mocha": "latest",
45
- "@types/nunjucks": "latest",
46
- "@types/python-struct": "latest",
47
- "@typescript-eslint/eslint-plugin": "latest",
48
- "@typescript-eslint/parser": "latest",
49
- "eslint": "latest",
50
- "eslint-config-prettier": "latest",
51
- "eslint-plugin-prettier": "latest",
52
- "global-jsdom": "latest",
53
- "lint-staged": "latest",
54
- "mocha": "latest",
55
- "prettier": "latest",
56
- "prettier-plugin-organize-imports": "latest",
57
- "ts-loader": "latest",
58
- "ts-mocha": "latest",
59
- "ts-node": "latest",
60
- "tsx": "latest",
61
- "typescript": "latest"
62
- }
63
- }
1
+ {
2
+ "name": "ha-nunjucks",
3
+ "version": "1.7.1",
4
+ "description": "Wrapper for nunjucks for use with Home Assistant frontend custom components to render templates",
5
+ "main": "./dist/index.js",
6
+ "files": [
7
+ "dist/**"
8
+ ],
9
+ "type": "module",
10
+ "scripts": {
11
+ "test": "ts-mocha tests/**/*.test.ts --require tests/fixtures.mjs",
12
+ "build": "tsc",
13
+ "prelint": "tsc --noemit",
14
+ "lint": "eslint --config ./eslint.config.js",
15
+ "setup": "git config --add core.hooksPath githooks && chmod +x githooks/pre-commit && npm i"
16
+ },
17
+ "lint-staged": {
18
+ "*": "prettier -w"
19
+ },
20
+ "author": "Nerwyn",
21
+ "license": "Apache-2.0",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/Nerwyn/ha-nunjucks.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/Nerwyn/ha-nunjucks/issues"
28
+ },
29
+ "homepage": "https://github.com/Nerwyn/ha-nunjucks#readme",
30
+ "dependencies": {
31
+ "buffer": "latest",
32
+ "create-hash": "latest",
33
+ "home-assistant-js-websocket": "latest",
34
+ "mersenne-twister": "latest",
35
+ "nunjucks": "latest",
36
+ "python-struct": "latest",
37
+ "slugify": "latest",
38
+ "stream": "npm:stream-browserify@latest",
39
+ "ts-py-datetime": "latest"
40
+ },
41
+ "devDependencies": {
42
+ "@types/create-hash": "latest",
43
+ "@types/mersenne-twister": "latest",
44
+ "@types/mocha": "latest",
45
+ "@types/nunjucks": "latest",
46
+ "@types/python-struct": "latest",
47
+ "@typescript-eslint/eslint-plugin": "latest",
48
+ "@typescript-eslint/parser": "latest",
49
+ "eslint": "latest",
50
+ "eslint-config-prettier": "latest",
51
+ "eslint-plugin-prettier": "latest",
52
+ "global-jsdom": "latest",
53
+ "lint-staged": "latest",
54
+ "mocha": "^11.3.0",
55
+ "prettier": "latest",
56
+ "prettier-plugin-organize-imports": "latest",
57
+ "ts-loader": "^9.2.1",
58
+ "ts-mocha": "latest",
59
+ "ts-node": "latest",
60
+ "tsx": "latest",
61
+ "typescript": "latest"
62
+ }
63
+ }