Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a setting to allow showing hidden entities #698

Merged
merged 9 commits into from
Feb 16, 2024
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ 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.
| 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 All @@ -116,7 +117,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

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.0",
"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
2 changes: 1 addition & 1 deletion src/custom-elements/lovelace-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const errorHtml = (cardName: string, message: string, content: string | undefine
<ol>
<li>
Please check if the problem was reported already<br />
Click <a target="_blank" href="https://github.com/maxwroc/battery-state-card/issues?q=is%3Aissue+is%3Aopen+${encodeURIComponent(message)}">here</a> to search
Click <a target="_blank" href="https://github.com/maxwroc/battery-state-card/issues?q=is%3Aissue+${encodeURIComponent(message)}">here</a> to search
</li>
<li>
If it wasn't please consider creating one<br />
Expand Down
5 changes: 5 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
Loading