Skip to content

Commit 69872d4

Browse files
Rocka84ciotlosm
authored andcommitted
gauge_card may use an attribute of the entity, instead of its state (ciotlosm#127)
* gauge_card may use an attribute of the entity, instead of its state * gauge_card attribute option in README * gauge_card added dot notation for attribute * gauge_card reworked dot.notation for attribute * gauge_card version bump to 0.1.0
1 parent 410da53 commit 69872d4

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

gauge-card/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A simple gauge implemented in CSS based on https://github.com/JohnrBell/Gauge_CS
1414
| type | string | **Required** | `custom:gauge-card`
1515
| title | string | optional | Name to display on card
1616
| entity | string | **Required** | `sensor.my_temperature`
17+
| attribute | string | optional | If set, this attribute of the entity is used, instead of its state
1718
| min | number | 0 | Minimum value for graph
1819
| max | number | 100 | Maximum value for graph
1920
| scale | string | '50px' | Base value for graph visual size
@@ -52,4 +53,18 @@ Simple one
5253
- type: custom:gauge-card
5354
entity: sensor.my_oil_sensor
5455
scale: 100px
55-
```
56+
```
57+
58+
Using an attribute
59+
```yaml
60+
- type: custom:gauge-card
61+
entity: climate.living_room
62+
attribute: current_temperature
63+
```
64+
65+
Using an attribute with dot notation
66+
```yaml
67+
- type: custom:gauge-card
68+
entity: climate.living_room.current_temperature
69+
```
70+

gauge-card/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1
1+
0.1.0

gauge-card/changelog.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
## 0.1.0
2+
- support attributes
3+
14
## 0.0.1
2-
Initial release that supports versioning
5+
Initial release that supports versioning

gauge-card/gauge-card.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class GaugeCard extends HTMLElement {
1616
if (!cardConfig.min) cardConfig.min = 0;
1717
if (!cardConfig.max) cardConfig.max = 100;
1818

19+
const entityParts = this._splitEntityAndAttribute(cardConfig.entity);
20+
cardConfig.entity = entityParts.entity;
21+
if (entityParts.attribute) cardConfig.attribute = entityParts.attribute;
22+
1923
const card = document.createElement('ha-card');
2024
const shadow = card.attachShadow({ mode: 'open' });
2125
const content = document.createElement('div');
@@ -106,6 +110,15 @@ class GaugeCard extends HTMLElement {
106110
this._config = cardConfig;
107111
}
108112

113+
_splitEntityAndAttribute(entity) {
114+
let parts = entity.split('.');
115+
if (parts.length < 3) {
116+
return { entity: entity };
117+
}
118+
119+
return { attribute: parts.pop(), entity: parts.join('.') };
120+
}
121+
109122
_fire(type, detail, options) {
110123
const node = this.shadowRoot;
111124
options = options || {};
@@ -151,9 +164,17 @@ class GaugeCard extends HTMLElement {
151164
return severityMap["normal"];
152165
}
153166

167+
_getEntityStateValue(entity, attribute) {
168+
if (!attribute) {
169+
return entity.state;
170+
}
171+
172+
return entity.attributes[attribute];
173+
}
174+
154175
set hass(hass) {
155176
const config = this._config;
156-
const entityState = hass.states[config.entity].state;
177+
const entityState = this._getEntityStateValue(hass.states[config.entity], config.attribute);
157178
const measurement = hass.states[config.entity].attributes.unit_of_measurement;
158179
const root = this.shadowRoot;
159180
if (entityState !== this._entityState) {
@@ -172,4 +193,4 @@ class GaugeCard extends HTMLElement {
172193
}
173194
}
174195

175-
customElements.define('gauge-card', GaugeCard);
196+
customElements.define('gauge-card', GaugeCard);

0 commit comments

Comments
 (0)