Skip to content

Commit

Permalink
Update entity-progress-card.js
Browse files Browse the repository at this point in the history
  • Loading branch information
francois-le-ko4la authored Feb 1, 2025
1 parent b25437e commit ffc2407
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions entity-progress-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* More informations here: https://github.com/francois-le-ko4la/lovelace-entity-progress-card/
*
* @author ko4la
* @version 1.0.29
* @version 1.0.30
*
*/

/** --------------------------------------------------------------------------
* PARAMETERS
*/

const VERSION='1.0.29';
const VERSION='1.0.30';
const CARD = {
typeName: 'entity-progress-card',
name: 'Entity progress card',
Expand All @@ -50,7 +50,7 @@ const CARD = {
language: "en",
minValue: 0,
maxPercent: 100,
unit: '%',
unit: {default: '%', fahrenheit: '°F' },
color: 'var(--state-icon-color)',
icon: 'mdi:alert',
alert_icon: 'mdi:alert-circle-outline',
Expand Down Expand Up @@ -818,10 +818,10 @@ class PercentHelper {
* @param {number} [currentValue=0] - The current value. Defaults to 0.
* @param {number} [minValue=CARD.config.minValue] - The minimum value. Defaults to CARD.config.minValue.
* @param {number} [maxValue=CARD.config.maxPercent] - The maximum percentage value. Defaults to CARD.config.maxPercent.
* @param {string} [unit=CARD.config.unit] - The unit of measurement. Defaults to CARD.config.unit.
* @param {string} [unit=CARD.config.unit.default] - The unit of measurement. Defaults to CARD.config.unit.default.
* @param {number} [decimal=CARD.config.decimal.percentage] - The number of decimal places to use. Defaults to CARD.config.decimal.percentage.
*/
constructor(currentValue = 0, minValue = CARD.config.minValue, maxValue = CARD.config.maxPercent, unit = CARD.config.unit, decimal = CARD.config.decimal.percentage) {
constructor(currentValue = 0, minValue = CARD.config.minValue, maxValue = CARD.config.maxPercent, unit = CARD.config.unit.default, decimal = CARD.config.decimal.percentage) {
/**
* @type {Value}
* @private
Expand Down Expand Up @@ -902,25 +902,25 @@ class PercentHelper {
/**
* Sets the unit of measurement.
*
* @param {string} [newUnit] - The new unit of measurement. If not provided, defaults to CARD.config.unit.
* @param {string} [newUnit] - The new unit of measurement. If not provided, defaults to CARD.config.unit.default.
*/
set unit(newUnit) {
if (!newUnit) {
newUnit = CARD.config.unit;
newUnit = CARD.config.unit.default;
}
this._unit.value = newUnit;
}

/**
* Sets the number of decimal places to use.
* If no value is provided, it defaults to CARD.config.decimal.percentage if the unit is CARD.config.unit,
* If no value is provided, it defaults to CARD.config.decimal.percentage if the unit is CARD.config.unit.default,
* otherwise it defaults to CARD.config.decimal.other.
*
* @param {number} [newDecimal] - The new number of decimal places.
*/
set decimal(newDecimal) {
if (newDecimal === undefined || newDecimal === null) {
if (this._unit.value === CARD.config.unit) {
if (this._unit.value === CARD.config.unit.default) {
newDecimal = CARD.config.decimal.percentage;
} else {
newDecimal = CARD.config.decimal.other;
Expand All @@ -929,6 +929,14 @@ class PercentHelper {
this._decimal.value = newDecimal;
}

valueForThemes(themeIsLinear) {
let value = this._current.value;
if (this._unit.value === CARD.config.unit.fahrenheit) {
value = (value - 32) * 5 / 9;
}
return themeIsLinear || this._unit.value === CARD.config.unit.default ? this._percent : value;
}

/**
* Checks if all internal values are valid.
*
Expand Down Expand Up @@ -973,7 +981,7 @@ class PercentHelper {
*/
get label() {
let value = this._percent;
if (this._unit.value !== CARD.config.unit) {
if (this._unit.value !== CARD.config.unit.default) {
value = this._current.value;
}
return `${value.toFixed(this._decimal.value)}${this._unit.value}`;
Expand Down Expand Up @@ -1456,14 +1464,14 @@ class ConfigHelper {

/**
* Sets the number of decimal places.
* Defaults to `CARD.config.decimal.percentage` if the unit is `CARD.config.unit`,
* Defaults to `CARD.config.decimal.percentage` if the unit is `CARD.config.unit.default`,
* otherwise defaults to `CARD.config.decimal.other`.
*
* @param {number} [newDecimal] - The new number of decimal places.
*/
set decimal(newDecimal) {
if (newDecimal === undefined || newDecimal === null) {
if (this._config.unit === CARD.config.unit) {
if (this._config.unit === CARD.config.unit.default) {
newDecimal = CARD.config.decimal.percentage;
} else {
newDecimal = CARD.config.decimal.other;
Expand Down Expand Up @@ -1634,7 +1642,7 @@ class CardView {
this._percentHelper.min = this._configHelper.config.min_value;
this._percentHelper.max = this._max_value.value;
this._percentHelper.refresh();
this._theme.value = this._theme.isLinear ? this._percentHelper.percent : this._currentValue.value;
this._theme.value = this._percentHelper.valueForThemes(this._theme.isLinear);
}

/**
Expand Down Expand Up @@ -2424,6 +2432,16 @@ class EntityProgressCardEditor extends HTMLElement {
});
}


_updateUnitFromEntity(unitAvailable) {
if(unitAvailable) {
this.configManager.updateProperty(EDITOR_INPUT_FIELDS.unit.name, unitAvailable);
this._elements[EDITOR_INPUT_FIELDS.unit.name].value = unitAvailable;
} else {
this.configManager.updateProperty(EDITOR_INPUT_FIELDS.unit.name, '');
this._elements[EDITOR_INPUT_FIELDS.unit.name].value = '';
}
}
/**
* Updates a specific property in the configuration object, and handles additional logic
* related to `theme` and field visibility.
Expand All @@ -2439,9 +2457,12 @@ class EntityProgressCardEditor extends HTMLElement {
}
if (key === EDITOR_INPUT_FIELDS.entity.type) {
const attributeAvailable = this._isEntityWithAttribute(value);
const unitAvailable = this._getUnitFromEntity(value);
if(attributeAvailable) {
this._refreshAttributeOption(this._hass.states[value] ?? null)
}
this._updateUnitFromEntity(unitAvailable);

this._toggleFieldDisable(EDITOR_INPUT_FIELDS.attribute.isInGroup, !attributeAvailable);
}

Expand Down Expand Up @@ -2508,6 +2529,11 @@ class EntityProgressCardEditor extends HTMLElement {
return !!ATTRIBUTE_MAPPING[entity?.split(".")[0]];
}

_getUnitFromEntity(curEntity = null) {
const entity = curEntity || this._hass.states[this.config.entity];
return this._hass.states[entity]?.attributes?.unit_of_measurement ?? null;
}

_getAttributeOption(curEntity=null) {
const entity = curEntity ?? this._hass.states[this.config.entity] ?? null;
if (!entity) {
Expand Down

0 comments on commit ffc2407

Please sign in to comment.