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

Add Magic areas 2 #75

Closed
15 changes: 12 additions & 3 deletions src/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,17 @@ class Helper {
}

/**
* Get state entities, filtered by area and domain.
* Get state entities, filtered by area, domain and optionally deviceClass.
*
* The result excludes hidden and disabled entities.
*
* @param {areaEntity} area Area entity.
* @param {string} domain Domain of the entity-id.
* @param {string|null} deviceClass Device class attribute of the state.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deviceClass should be [deviceClass] if it's an optional parameter.

*
* @return {stateObject[]} Array of state entities.
*/
static getStateEntities(area, domain) {
static getStateEntities(area, domain, deviceClass = null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deviceClass = null or deviceClass = undefined?

if (!this.isInitialized()) {
console.warn("Helper class should be initialized before calling this method!");
}
Expand All @@ -355,7 +356,15 @@ class Helper {

// Get states whose entity-id starts with the given string.
const stateEntities = Object.values(this.#hassStates).filter(
state => state.entity_id.startsWith(`${domain}.`),
state => {
if (deviceClass
Copy link
Collaborator

@DigiLive DigiLive Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the return conditions rewritten to a ternary operator statement?
E.g.: return deviceClass && state.attributes.device_class === deviceClass ? state.entity_id.startsWith(`${domain}.`) : false;

&& state.attributes.device_class !== deviceClass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolved variable device_class.

It should be included in typedef stateObject in file src/typedefs.js

) {
return false;
}

return state.entity_id.startsWith(`${domain}.`)
},
);

for (const state of stateEntities) {
Expand Down
63 changes: 63 additions & 0 deletions src/cards/AreaCard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AbstractCard} from "./AbstractCard";
import {Helper} from "../Helper";

/**
* Area Card Class
Expand Down Expand Up @@ -57,6 +58,68 @@ class AreaCard extends AbstractCard {
this.options.primary = options.name;
}
}

/**
* @inheritdoc
*/
getCard() {
let card = super.getCard();

if (!card.secondary) {
// Get or determine the relevant sensor entity IDs based on options or default behavior
const temperatureSensorId = this.options?.temperature || Helper.getStateEntities(this.entity, "sensor", "temperature")[0]?.entity_id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of field options in AbstractClass should change to {abstractOptions & Object<string, any>} to allow unknown additional properties.

Applies to lines #L70, #L71, #L72, #L92, #L93, #L94.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument type  hassEntity | areaEntity is not assignable to parameter type areaEntity.

Maybe it's more proper to declare private field #entity in AreaCard as type areaEntity and assign in the value of parameter area in the constructor. And then use this.#entity in the function call of Helper.getStateEntities

Applies to lines #L70, #L71, #L72, #L92, #L93, #L94.

const humiditySensorId = this.options?.humidity || Helper.getStateEntities(this.entity, "sensor", "humidity")[0]?.entity_id;
const illuminanceSensorId = this.options?.illuminance || Helper.getStateEntities(this.entity, "sensor", "illuminance")[0]?.entity_id;

// Collect secondary pieces of information for the card, based on available sensors
let secondaries = [];
if (temperatureSensorId) {
secondaries.push(`🌡️{{ states('${temperatureSensorId}') | int }}°`);
}
if (humiditySensorId) {
secondaries.push(`💧{{ states('${humiditySensorId}') | int }}%`);
}
if (illuminanceSensorId) {
secondaries.push(`☀️{{ states('${illuminanceSensorId}')}}lx`);
}

// Set the secondary information on the card as a combined string
card.secondary = secondaries.join(" ");
}

if (!card.badge_icon) {
// Get or determine the relevant lock or binary sensor entity IDs
const lockId = this.options?.lock || Helper.getStateEntities(this.entity, "lock")[0]?.entity_id;
const windowBinarySensorId = this.options?.window || Helper.getStateEntities(this.entity, "binary_sensor", "window")[0]?.entity_id;
const doorBinarySensorId = this.options?.door || Helper.getStateEntities(this.entity, "binary_sensor", "door")[0]?.entity_id;

// Construct badge conditions based on the existence and states of the entities
let badgeConditions = []
if (lockId) {
badgeConditions.push({entity: lockId, state: 'unlocked', icon: 'mdi:lock-open'})
}
if (windowBinarySensorId) {
badgeConditions.push({entity: windowBinarySensorId, state: 'on', icon: 'mdi:window-open-variant'})
}
if (doorBinarySensorId) {
badgeConditions.push({entity: doorBinarySensorId, state: 'on', icon: 'mdi:door-open'})
}

// If there are badge conditions, construct a template for the badge icon based on conditions
if (badgeConditions.length) {
let badge = badgeConditions
.map(condition => `is_state('${condition.entity}', '${condition.state}') %}${condition.icon}{%`)
.join(' elif ')
badge = `{% if ${badge} endif %}`

// Set badge properties on the card
card.badge_color = "red";
card.badge_icon = badge;
}
}

return card;
}
}

export {AreaCard};