Skip to content

Commit

Permalink
Merge latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Feb 17, 2024
2 parents 35344fc + b187ce1 commit a89e108
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 5 additions & 10 deletions src/battery-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = (<EntityRegistryDisplayEntry>battery.entityData?.display)?.hidden;
});

return;
}

Expand All @@ -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 || (<EntityRegistryDisplayEntry>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]);
Expand All @@ -262,5 +258,4 @@ export interface IBatteryCollection {

export interface IBatteryCollectionItem extends BatteryStateEntity {
entityId?: string;
isHidden?: boolean;
}
24 changes: 23 additions & 1 deletion src/custom-elements/battery-state-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +61,11 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {
@property({ attribute: false })
public action: IAction | undefined;

/**
* Whether entity should not be shown
*/
public isHidden: boolean | undefined;

/**
* Raw entity data
*/
Expand All @@ -85,6 +90,9 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {

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) {
Expand Down Expand Up @@ -127,6 +135,20 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {
onError(): void {
}

hideEntity(): void {
this.isHidden = true;
}

showEntity(): void {
if (this.config.respect_visibility_setting !== false && (<EntityRegistryDisplayEntry>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
Expand Down
5 changes: 5 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 10 additions & 5 deletions test/card/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BatteryStateCard>();
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", <any>{
title: "Header",
filter: {
include: [
Expand All @@ -86,7 +90,8 @@ test.each([
],
exclude: [],
},
entities: []
entities: [],
respect_visibility_setting: respectVisibilitySetting,
});

// waiting for card to be updated/rendered
Expand Down

0 comments on commit a89e108

Please sign in to comment.