Skip to content

Commit

Permalink
fix: for trying define property
Browse files Browse the repository at this point in the history
  • Loading branch information
mentalilll committed Jul 30, 2024
1 parent 131337e commit 0e71eaa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
75 changes: 39 additions & 36 deletions dist/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,46 @@ export const bar = {
</ha-card>
`;
this.content = this.querySelector("div.card-content");

this.config.sensors.forEach((sensor) => {
let humidity = this._hass.states[sensor.humidity].state;
let temperature = this._hass.states[sensor.temperature].state;
let leafTemperature = temperature - (this.config.leaf_temperature_offset || 2);
if (sensor.leaf_temperature !== undefined) {
leafTemperature = this._hass.states[sensor.leaf_temperature].state;
}
let vpd;
if (sensor.vpd !== undefined) {
vpd = this._hass.states[sensor.vpd].state;
} else {
vpd = this.calculateVPD(this.toFixedNumber(leafTemperature), this.toFixedNumber(temperature), this.toFixedNumber(humidity)).toFixed(2);
}

let card = this.content.querySelector(`ha-card[data-sensor="${sensor.name}"]`);
if (!card) {
card = document.createElement('ha-card');
card.dataset.sensor = sensor.name;
card.className = 'vpd-card';
}
// if sensor.name is not empty than show in the card
let html = `<div class="bar">`;
if (sensor.name !== "") {
html += `<span class="vpd-title">${sensor.name}</span>`;
}
html += `<span class="vpd-value">${vpd} ${this.kpa_text || ''}</span>`;
html += `<span class="vpd-rh">${humidity}%</span>`;
html += `<span class="vpd-temp">${temperature} ${this.unit_temperature}</span>`;
html += `<span style="background: ${this.getColorForVpd(vpd)}" class="vpd-state ${this.getPhaseClass(vpd)}"><span>${this.getPhaseClass(vpd)}</span></span>`;
html += `<span class="vpd-history" style="float:right;"><canvas></canvas></span>`;
html += `</div>`;
card.innerHTML = html;
this.content.appendChild(card);
});
if (this._hass) {
this.config.sensors.forEach((sensor) => {
let humidity = this._hass.states[sensor.humidity].state;
let temperature = this._hass.states[sensor.temperature].state;
let leafTemperature = temperature - (this.config.leaf_temperature_offset || 2);
if (sensor.leaf_temperature !== undefined) {
if (this._hass.states[sensor.leaf_temperature] !== undefined) {
leafTemperature = this._hass.states[sensor.leaf_temperature].state;
}
}
let vpd;
if (sensor.vpd !== undefined) {
vpd = this._hass.states[sensor.vpd].state;
} else {
vpd = this.calculateVPD(this.toFixedNumber(leafTemperature), this.toFixedNumber(temperature), this.toFixedNumber(humidity)).toFixed(2);
}

let card = this.content.querySelector(`ha-card[data-sensor="${sensor.name}"]`);
if (!card) {
card = document.createElement('ha-card');
card.dataset.sensor = sensor.name;
card.className = 'vpd-card';
}
// if sensor.name is not empty than show in the card
let html = `<div class="bar">`;
if (sensor.name !== "") {
html += `<span class="vpd-title">${sensor.name}</span>`;
}
html += `<span class="vpd-value">${vpd} ${this.kpa_text || ''}</span>`;
html += `<span class="vpd-rh">${humidity}%</span>`;
html += `<span class="vpd-temp">${temperature} ${this.unit_temperature}</span>`;
html += `<span style="background: ${this.getColorForVpd(vpd)}" class="vpd-state ${this.getPhaseClass(vpd)}"><span>${this.getPhaseClass(vpd)}</span></span>`;
html += `<span class="vpd-history" style="float:right;"><canvas></canvas></span>`;
html += `</div>`;
card.innerHTML = html;
this.content.appendChild(card);
});
}
}

console.log()
this.updateBars();
this.updateVPDLegend();

Expand Down
14 changes: 12 additions & 2 deletions dist/ha-vpd-chart-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ export class HaVpdChartEditor extends HTMLElement {
const index = target.getAttribute('data-index');
let value = target.value;
if (this._vpd_phases[index].className !== value) {
this._config.vpd_phases[index].className = value;
if (Object.isExtensible(this._config.vpd_phases[index])) {
this._config.vpd_phases[index].className = value;
} else {
console.warn('Cannot define property on a non-extensible object');
}
fireEvent(this, 'config-changed', {config: this._config});
}
}
Expand Down Expand Up @@ -356,7 +360,13 @@ export class HaVpdChartEditor extends HTMLElement {

configValues.forEach(({id, prop, type}) => {
const element = this.shadowRoot.querySelector(`#${id}`);
if (element) element[type] = this[prop];
if (element) {
if (Object.isExtensible(element)) {
element[type] = this[prop];
} else {
console.warn('Cannot define property on a non-extensible object');
}
}
});

const minVPD = this.toFixedNumber(this.calculateVPD(this._max_temperature - this._leaf_temperature_offset, this._max_temperature, this._max_humidity));
Expand Down
2 changes: 1 addition & 1 deletion dist/ha-vpd-chart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Set version for the card
window.vpdChartVersion = "1.3.8";
window.vpdChartVersion = "1.3.9";

import {methods} from './methods.js';
import {chart} from './chart.js';
Expand Down

0 comments on commit 0e71eaa

Please sign in to comment.