Skip to content

Commit

Permalink
Add hide functionatilty (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored Apr 8, 2023
1 parent 333ad58 commit fd5e186
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 194 deletions.
426 changes: 254 additions & 172 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mini-climate-card",
"version": "v2.4.5",
"version": "v2.5.0",
"description": "a/c card for Home Assistant Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
6 changes: 6 additions & 0 deletions release_notes/v2.5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## v2.5.0
[![Downloads](https://img.shields.io/github/downloads/artem-sedykh/mini-climate-card/v2.5.0/total.svg)](https://github.com/artem-sedykh/mini-climate-card/releases/tag/v2.5.0)

### Added
- feature: add hide functionality to indicators by @regevbr and @adi90x
- feature: hide can now be a function by @regevbr
7 changes: 6 additions & 1 deletion src/components/indicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ export default class ClimateIndicators extends ScopedRegistryHost(LitElement) {
if (!ClimateIndicators.elementDefinitionsLoaded) {
return html``;
}

const indicatorsToShow = Object.entries(this.indicators)
.map(entry => entry[1])
.filter(indicator => !indicator.hide);

const context = this;

return html`
<div class='mc-indicators__container'>
${Object.entries(this.indicators).map(i => context.renderIndicator(i[1]))}
${indicatorsToShow.map(i => context.renderIndicator(i))}
</div>
`;
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default class ClimateTemperature extends ScopedRegistryHost(LitElement) {
changing: Boolean,
target: Number,
swapTemperatures: Boolean,
hideCurrentTemperature: Boolean,
};
}

Expand All @@ -33,7 +32,7 @@ export default class ClimateTemperature extends ScopedRegistryHost(LitElement) {
}

renderTemperature() {
if (this.temperature.value === undefined || this.hideCurrentTemperature)
if (this.temperature.value === undefined || this.temperature.hide)
return '';

if (this.swapTemperatures) {
Expand Down
89 changes: 75 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
this.temperature = {};
this.targetTemperature = {};
this.swapTemperatures = false;
this.hideCurrentTemperature = false;
this.buttons = {};
this.indicators = {};
this.hvacMode = {};
Expand Down Expand Up @@ -152,7 +151,7 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
const targetTemperatureEntity = this.hass.states[targetTemperatureEntityId];

const temperature = new TemperatureObject(temperatureEntity, targetTemperatureEntity,
this.config);
this.config, this.climate);

if (this.temperature.rawValue !== temperature.rawValue
|| this.temperature.target !== temperature.target || force) {
Expand Down Expand Up @@ -273,6 +272,14 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
if (item.style)
item.functions.style = compileTemplate(item.style, context);

if (item.hide) {
if (typeof item.hide === 'boolean') {
item.functions.hide = () => true;
} else {
item.functions.hide = compileTemplate(item.hide, context);
}
}

return item;
}

Expand Down Expand Up @@ -330,13 +337,58 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
item.functions.icon.style = compileTemplate(item.icon.style, context);
}

if (item.hide) {
if (typeof item.hide === 'boolean') {
item.functions.hide = () => true;
} else {
item.functions.hide = compileTemplate(item.hide, context);
}
}

return item;
}

getSecondaryInfoConfig(config) {
const item = {
...config,
};

item.functions = item.functions || {};
const context = { ...config };

if (item.hide) {
if (typeof item.hide === 'boolean') {
item.functions.hide = () => true;
} else {
item.functions.hide = compileTemplate(item.hide, context);
}
}

return item;
}

getToggleConfig(config) {
const item = {
...config,
};

item.functions = item.functions || {};
const context = { ...config };

if (item.hide) {
if (typeof item.hide === 'boolean') {
item.functions.hide = () => true;
} else {
item.functions.hide = compileTemplate(item.hide, context);
}
}

return item;
}

getIndicatorsConfig(config) {
return Object.entries(config.indicators || {})
.map(i => this.getIndicatorConfig(i[0], i[1] || {}, config))
.filter(i => !i.hide);
.map(i => this.getIndicatorConfig(i[0], i[1] || {}, config));
}

getTargetTemperatureConfig(config) {
Expand Down Expand Up @@ -425,12 +477,12 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {

this.config.hvac_mode = this.getHvacModeConfig(this.config);

this.config.toggle = {
this.config.toggle = this.getToggleConfig({
icon: ICON.TOGGLE,
hide: false,
default: false,
...config.toggle || {},
};
});

if (typeof config.secondary_info === 'string') {
this.config.secondary_info = { type: config.secondary_info };
Expand All @@ -440,11 +492,11 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
...config.secondary_info || {},
};
}
this.config.secondary_info = this.getSecondaryInfoConfig(this.config.secondary_info);

this.toggle = this.config.toggle.default;

this.swapTemperatures = !!this.config.swap_temperatures;
this.hideCurrentTemperature = !!this.config.hide_current_temperature;
}

renderCtlWrap() {
Expand All @@ -471,8 +523,7 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
.temperature=${this.temperature}
.target=${this.targetTemperatureValue}
.changing=${this.targetTemperatureChanging}
.swapTemperatures=${this.swapTemperatures}
.hideCurrentTemperature=${this.hideCurrentTemperature}>
.swapTemperatures=${this.swapTemperatures}>
</mc-temperature>
`;
}
Expand Down Expand Up @@ -583,11 +634,16 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
}

renderToggleButton() {
if (this.config.buttons.filter(b => !b.hide && b.location !== 'main').length === 0)
return '';
if (Object.entries(this.buttons)
.map(entry => entry[1])
.filter(button => !button.hide && button.location !== 'main')
.length === 0)
return html``;

if (this.config.toggle.hide)
return '';
if (this.config.toggle.functions.hide
&& this.config.toggle.functions.hide(this.climate.entity, this.climate.mode)) {
return html``;
}

return html`
<ha-icon-button class='toggle-button ${this.toggleButtonCls()}'
Expand All @@ -608,9 +664,14 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
}

renderSecondaryInfo() {
if (this.climate.isUnavailable || this.config.secondary_info.hide)
if (this.climate.isUnavailable)
return html``;

if (this.config.secondary_info.functions.hide
&& this.config.secondary_info.functions.hide(this.climate.entity, this.climate.mode)) {
return html``;
}

return html`
<div class='entity__secondary_info ellipsis'>
<mc-secondary-info
Expand Down
7 changes: 6 additions & 1 deletion src/models/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export default class ButtonObject {
}

get hide() {
return this.config.hide;
if (this.config.functions.hide) {
return this.config.functions.hide(this.state, this.entity,
this.climate.entity, this.climate.mode);
}

return false;
}

get icon() {
Expand Down
7 changes: 6 additions & 1 deletion src/models/hvac-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export default class HvacModeObject {
}

get hide() {
return this.config.hide;
if (this.config.functions.hide) {
return this.config.functions.hide(this.state, this.entity,
this.climate.entity, this.climate.mode);
}

return false;
}

get originalState() {
Expand Down
9 changes: 9 additions & 0 deletions src/models/indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ export default class IndicatorObject {

return {};
}

get hide() {
if (this.config.functions.hide) {
return this.config.functions.hide(this.value, this.entity,
this.climate.entity, this.climate.mode);
}

return false;
}
}
19 changes: 17 additions & 2 deletions src/models/temperature.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { getEntityValue, round } from '../utils/utils';
import { compileTemplate, getEntityValue, round } from '../utils/utils';

export default class TemperatureObject {
constructor(temperatureEntity, targetTemperatureEntity, config) {
constructor(temperatureEntity, targetTemperatureEntity, config, climate) {
this.climate = climate || {};
this.temperatureEntity = temperatureEntity || {};
this.targetTemperatureEntity = targetTemperatureEntity || {};
this.config = config;
if (this.config.hide_current_temperature) {
if (typeof this.config.hide_current_temperature === 'boolean') {
this.shouldHideCurrentTemperature = () => true;
} else {
this.shouldHideCurrentTemperature = compileTemplate(this.config.hide_current_temperature);
}
} else {
this.shouldHideCurrentTemperature = () => false;
}
}

get unit() {
Expand Down Expand Up @@ -40,4 +50,9 @@ export default class TemperatureObject {
get rawValue() {
return getEntityValue(this.temperatureEntity, this.config.temperature.source);
}

get hide() {
return this.shouldHideCurrentTemperature(this.value, this.temperatureEntity,
this.targetTemperatureEntity, this.climate.entity, this.climate.mode);
}
}

0 comments on commit fd5e186

Please sign in to comment.