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 fcb59d3..e1a1862 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "battery-state-card", - "version": "3.2.1", + "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 cc204de..864b6e2 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -269,6 +269,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