Skip to content

Commit

Permalink
feature: add custom styales to indicators values (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr authored May 9, 2023
1 parent fd5e186 commit b509ea8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ v2 is only compatible from version 2022.11 onwards
| indicators: `name:icon` | object | optional | v1.0.1 | Icon object |
| indicators: `name:icon:template` | function | optional | v1.0.1 | Icon template function |
| indicators: `name:icon:style` | function | optional | v1.0.1 | Styles |
| indicators: `name:value` | object | optional | v1.0.1 | Value object |
| indicators: `name:value:style` | function | optional | v1.0.1 | Styles |
| indicators: `name:unit` | string | optional | v1.0.1 | Display unit |
| indicators: `name:unit` | string | optional | v1.0.1 | Display unit |
| indicators: `name:round` | number | optional | v1.0.1 | Rounding number value |
| indicators: `name:hide` | boolean | optional | v2.5.0 | Hide indicator, default value `False` |
Expand Down Expand Up @@ -377,6 +380,7 @@ indicators:
| `source:mapper` | function | indicator config | value, entity, climate_entity, hvac_mode | any |
| `icon:template` | function | indicator config | value, entity, climate_entity, hvac_mode | string |
| `icon:style` | function | indicator config | value, entity, climate_entity, hvac_mode | object |
| `value:style` | function | indicator config | value, entity, climate_entity, hvac_mode | object |
| `hide` | function | indicator config | value, entity, climate_entity, hvac_mode | boolean |

`value` - current indicator value
Expand Down Expand Up @@ -430,7 +434,7 @@ indicators:
entity: sensor.humidity
```

##### icon template
##### icon style

> You can also set custom styles.
for example:
Expand All @@ -449,6 +453,24 @@ indicators:
entity: sensor.humidity
```

##### value style

> You can also set custom styles.
for example:
```yaml
type: custom:mini-climate
entity: climate.my_ac
indicators:
humidity:
value:
style: >
(value) => (value > 30 ? { color: 'red'} : {})
unit: '%'
round: 1
source:
entity: sensor.humidity
```

##### Hide

> You can also hide based on state.
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": "mini-climate-card",
"version": "v2.5.0",
"version": "v2.6.0",
"description": "a/c card for Home Assistant Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
5 changes: 5 additions & 0 deletions release_notes/v2.6.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## v2.6.0
[![Downloads](https://img.shields.io/github/downloads/artem-sedykh/mini-climate-card/v2.6.0/total.svg)](/~https://github.com/artem-sedykh/mini-climate-card/releases/tag/v2.6.0)

### Added
- feature: add custom style functionality to indicators value by @regevbr
10 changes: 5 additions & 5 deletions src/components/indicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default class ClimateIndicators extends ScopedRegistryHost(LitElement) {
return html`<ha-icon style=${styleMap(indicator.iconStyle)} class='state__value_icon' .icon=${icon}></ha-icon>`;
}

renderUnit(unit) {
if (!unit)
renderUnit(indicator) {
if (!indicator.unit)
return '';

return html`<span class='state__uom'>${unit}</span>`;
return html`<span class='state__uom' style=${styleMap(indicator.valueStyle)}>${indicator.unit}</span>`;
}

renderIndicator(indicator) {
Expand All @@ -50,8 +50,8 @@ export default class ClimateIndicators extends ScopedRegistryHost(LitElement) {
return html`
<div class='state ${cls}' @click=${e => this.handlePopup(e, indicator)}>
${this.renderIcon(indicator)}
<span class='state__value'>${indicator.value}</span>
${this.renderUnit(indicator.unit)}
<span class='state__value' style=${styleMap(indicator.valueStyle)}>${indicator.value}</span>
${this.renderUnit(indicator)}
</div>
`;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ class MiniClimate extends ScopedRegistryHost(LitElement) {
item.functions.icon.style = compileTemplate(item.icon.style, context);
}

if (typeof item.value === 'object') {
item.functions.value = {};

if (item.value.style)
item.functions.value.style = compileTemplate(item.value.style, context);
}

if (item.hide) {
if (typeof item.hide === 'boolean') {
item.functions.hide = () => true;
Expand Down
8 changes: 8 additions & 0 deletions src/models/indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export default class IndicatorObject {
return {};
}

get valueStyle() {
if (this.config.functions.value && this.config.functions.value.style)
return this.config.functions.value.style(this.value, this.entity,
this.climate.entity, this.climate.mode) || {};

return {};
}

get hide() {
if (this.config.functions.hide) {
return this.config.functions.hide(this.value, this.entity,
Expand Down

0 comments on commit b509ea8

Please sign in to comment.