From e01aeede5988a383000958c29385866bf12ce512 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Mon, 12 Feb 2024 15:50:31 +0000 Subject: [PATCH 1/4] Search for all reported exceptions - not only open ones --- src/custom-elements/lovelace-card.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/custom-elements/lovelace-card.ts b/src/custom-elements/lovelace-card.ts index b0841d0..6faa6e4 100644 --- a/src/custom-elements/lovelace-card.ts +++ b/src/custom-elements/lovelace-card.ts @@ -153,7 +153,7 @@ const errorHtml = (cardName: string, message: string, content: string | undefine
  1. Please check if the problem was reported already
    - Click here to search + Click here to search
  2. If it wasn't please consider creating one
    From 03e0ecd661a7ccd595525977e54b46d23cb48b0f Mon Sep 17 00:00:00 2001 From: Daniel <49846893+danielbrunt57@users.noreply.github.com> Date: Tue, 13 Feb 2024 00:51:26 -0800 Subject: [PATCH 2/4] Update README.md Fixed 2 typos in Common options --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae1cad9..8a2d8f4 100644 --- a/README.md +++ b/README.md @@ -106,8 +106,8 @@ These options can be specified both per-entity and at the top level (affecting a | unit | string | `"%"` | v2.1.0 | Override for unit displayed next to the state/level value ([example](#other-use-cases)) | value_override | [KString](#keyword-string-kstring) | | v3.0.0 | Allows to override the battery level value. Note: when used the `multiplier`, `round`, `state_map` setting is ignored | non_battery_entity | boolean | `false` | v3.0.0 | Disables default battery state sources e.g. "battery_level" attribute -| default_state_formatting | bollean | `true` | v3.1.0 | Can be used to disable default state formatting e.g. entity display precission setting -| debug | boolean \| string | `false` | v3.2.0 | Whether to show debug otuput (all available entity data). You can use entity_id if you want to debug specific one. +| default_state_formatting | boolean | `true` | v3.1.0 | Can be used to disable default state formatting e.g. entity display precission setting +| debug | boolean \| string | `false` | v3.2.0 | Whether to show debug output (all available entity data). You can use entity_id if you want to debug specific one. ### Keyword string (KString) From 31cd4dc97a609b484b5816c225eef756ff08a122 Mon Sep 17 00:00:00 2001 From: Daniel <49846893+danielbrunt57@users.noreply.github.com> Date: Tue, 13 Feb 2024 06:06:44 -0800 Subject: [PATCH 3/4] Update README.md Changed KSstring Entity property description to reflect ability to use reltime() function for relative time. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a2d8f4..4234cb8 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ This is a string value containing dynamic values. Data for dynamic values can be | Type | Example | Description | |:-----|:-----|:-----| | Charging state | `"{charging}"` | Shows text specified in [ChargingState](#charging-state-object) -| Entity property | `"{last_updated}"` | Current entity property. To show relative time there cannot be any additional string befor/after the keyword otherwise it will show full date. +| Entity property | `"{last_updated}"` | Current entity property. To ensure relative time, use the reltime() function via "\|" (see below). E.g.: `"Changed: {last_updated\|reltime()}"` | Entity attributes | `"Remaining time: {attributes.remaining_time}"` | Current entity attribute value. | Other entity data | `"Since last charge: {sensor.tesla.attributes.distance}"` | You can use full "path" to the other entity data From 91d10c13bfa9dbdb6f829ca496fd30043da4fea7 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Fri, 16 Feb 2024 13:45:59 +0000 Subject: [PATCH 4/4] Added setting to allow showing hidden entities --- README.md | 1 + package.json | 2 +- src/battery-provider.ts | 15 +++++-------- src/custom-elements/battery-state-entity.ts | 24 ++++++++++++++++++++- src/typings.d.ts | 5 +++++ test/card/filters.test.ts | 15 ++++++++----- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 4234cb8..0a863ab 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ These options can be specified both per-entity and at the top level (affecting a | non_battery_entity | boolean | `false` | v3.0.0 | Disables default battery state sources e.g. "battery_level" attribute | default_state_formatting | boolean | `true` | v3.1.0 | Can be used to disable default state formatting e.g. entity display precission setting | debug | boolean \| string | `false` | v3.2.0 | Whether to show debug output (all available entity data). You can use entity_id if you want to debug specific one. +| respect_visibility_setting | boolean | `true` | v3.3.0 | Whether to hide entities which are marked in the UI as hidden on dashboards. ### Keyword string (KString) diff --git a/package.json b/package.json index ec9f9c6..e1a1862 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "3.2.0", + "version": "3.3.0", "description": "Battery State card for Home Assistant", "main": "dist/battery-state-card.js", "author": "Max Chodorowski", diff --git a/src/battery-provider.ts b/src/battery-provider.ts index 4edafaf..af0e0d9 100644 --- a/src/battery-provider.ts +++ b/src/battery-provider.ts @@ -15,8 +15,9 @@ const entititesGlobalProps: (keyof IBatteryEntityConfig)[] = [ "default_state_formatting", "extend_entity_data", "icon", - "non_battery_entity", - "round", + "non_battery_entity", + "respect_visibility_setting", + "round", "secondary_info", "state_map", "tap_action", @@ -217,11 +218,6 @@ export class BatteryProvider { */ private processExcludes() { if (this.exclude == undefined) { - Object.keys(this.batteries).forEach((entityId) => { - const battery = this.batteries[entityId]; - battery.isHidden = (battery.entityData?.display)?.hidden; - }); - return; } @@ -248,8 +244,8 @@ export class BatteryProvider { } // we keep the view model to keep updating it - // it might be shown/not-hidden next time - battery.isHidden = isHidden || (battery.entityData?.display)?.hidden; + // it might be shown/not-hidden after next update + isHidden? battery.hideEntity() : battery.showEntity(); }); toBeRemoved.forEach(entityId => delete this.batteries[entityId]); @@ -262,5 +258,4 @@ export interface IBatteryCollection { export interface IBatteryCollectionItem extends BatteryStateEntity { entityId?: string; - isHidden?: boolean; } \ No newline at end of file diff --git a/src/custom-elements/battery-state-entity.ts b/src/custom-elements/battery-state-entity.ts index c9e239d..210b83d 100644 --- a/src/custom-elements/battery-state-entity.ts +++ b/src/custom-elements/battery-state-entity.ts @@ -12,7 +12,7 @@ import { getChargingState } from "../entity-fields/charging-state"; import { getBatteryLevel } from "../entity-fields/battery-level"; import { getName } from "../entity-fields/get-name"; import { getIcon } from "../entity-fields/get-icon"; -import { DeviceRegistryEntry } from "../type-extensions"; +import { DeviceRegistryEntry, EntityRegistryDisplayEntry } from "../type-extensions"; /** * Battery entity element @@ -61,6 +61,11 @@ export class BatteryStateEntity extends LovelaceCard { @property({ attribute: false }) public action: IAction | undefined; + /** + * Whether entity should not be shown + */ + public isHidden: boolean | undefined; + /** * Raw entity data */ @@ -85,6 +90,9 @@ export class BatteryStateEntity extends LovelaceCard { if (this.config.extend_entity_data !== false) { this.extendEntityData(); + + // make sure entity is visible when it should be shown + this.showEntity(); } if (this.config.debug === true || this.config.debug === this.config.entity) { @@ -127,6 +135,20 @@ export class BatteryStateEntity extends LovelaceCard { onError(): void { } + hideEntity(): void { + this.isHidden = true; + } + + showEntity(): void { + if (this.config.respect_visibility_setting !== false && (this.entityData?.display)?.hidden) { + // When entity is marked as hidden in the UI we should respect it + this.isHidden = true; + return; + } + + this.isHidden = false; + } + /** * Adding or removing action * @param enable Whether to enable/add the tap action diff --git a/src/typings.d.ts b/src/typings.d.ts index c88aac5..aab2e1e 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -264,6 +264,11 @@ interface IBatteryEntityConfig { * Whether to print the debug output */ debug?: string | boolean, + + /** + * Whether to respect HA entity visibility setting + */ + respect_visibility_setting?: boolean, } interface IBatteryCardConfig { diff --git a/test/card/filters.test.ts b/test/card/filters.test.ts index 17fa546..6643e32 100644 --- a/test/card/filters.test.ts +++ b/test/card/filters.test.ts @@ -67,15 +67,19 @@ test("Include via entity_id and exclude via state - empty result", async () => { test.each([ - [false, 1], - [true, 0], -])("Entity filtered based on hidden state", async (isHidden: boolean, numOfRenderedEntities: number) => { + [false, undefined, 1], + [true, undefined, 0], + [false, true, 1], + [true, true, 0], + [false, false, 1], + [true, false, 1], +])("Entity filtered based on hidden state", async (isHidden: boolean, respectVisibilitySetting: boolean | undefined, numOfRenderedEntities: number) => { const hass = new HomeAssistantMock(); const entity = hass.addEntity("Bedroom motion battery level", "90"); entity.setProperty("display", { entity_id: "", hidden: isHidden }) - const cardElem = hass.addCard("battery-state-card", { + const cardElem = hass.addCard("battery-state-card", { title: "Header", filter: { include: [ @@ -86,7 +90,8 @@ test.each([ ], exclude: [], }, - entities: [] + entities: [], + respect_visibility_setting: respectVisibilitySetting, }); // waiting for card to be updated/rendered