Skip to content

Commit ec65ebe

Browse files
authored
refactor: Update localization and icon states, and add entity state converter and formatting support (#233)
* First step to add localization using HA JS modules * Format any number * Split state processing and displaying * Split state processing and displaying * Add converters to buildStateString() * Update entity state converters * First working version for lights * Update dist too * Update and Add Icon States * Add icon monitoring and cleanup * Remove locale from formatTime24h * Remove isNumericState from import * Remove unused variable iconSet
1 parent 75e1fd5 commit ec65ebe

35 files changed

+2591
-489
lines changed

Diff for: dist/swiss-army-knife-card.js

+150-149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package-lock.json

+15-154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
},
2626
"dependencies": {
2727
"@formatjs/intl-utils": "^3.8.4",
28-
"custom-card-helpers": "^1.8.0",
28+
"@mdi/js": "^7.2.96",
29+
"@tanem/svg-injector": "^10.1.53",
2930
"home-assistant-js-websocket": "^5.7.0",
3031
"lit-element": "^2.5.1",
3132
"lit-html": "^1.4.1",
32-
"@tanem/svg-injector": "^10.1.53"
33+
"memoize-one": "^6.0.0"
3334
}
3435
}

Diff for: src/base-tool.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent } from 'custom-card-helpers';
1+
import { fireEvent } from './frontend_mods/common/dom/fire_event';
22

33
import Merge from './merge';
44
import Utils from './utils';
@@ -122,9 +122,14 @@ export default class BaseTool {
122122
*/
123123
set value(state) {
124124
let localState = state;
125-
126125
if (this.dev.debug) console.log('BaseTool set value(state)', localState);
127-
if (typeof (localState) !== 'undefined') if (this._stateValue?.toLowerCase() === localState.toLowerCase()) return;
126+
127+
try {
128+
if (localState !== 'undefined'
129+
&& typeof localState !== 'undefined') if (this._stateValue?.toString().toLowerCase() === localState.toString().toLowerCase()) return;
130+
} catch (e) {
131+
console.log('catching something', e, state, this.config);
132+
}
128133

129134
this.derivedEntity = null;
130135

@@ -137,7 +142,6 @@ export default class BaseTool {
137142
this._stateValuePrev = this._stateValue || localState;
138143
this._stateValue = localState;
139144
this._stateValueIsDirty = true;
140-
141145
// If animations defined, calculate style for current state.
142146

143147
// 2022.07.04 Temp disable this return, as animations should be able to process the 'undefined' state too!!!!
@@ -161,18 +165,13 @@ export default class BaseTool {
161165

162166
if (isMatch) return true;
163167

164-
// The state builder renames 'unavailable' to '-ua-'
165-
// Change this temporary in here to match this...
166-
if (item.state === 'unavailable') { item.state = '-ua-'; }
167-
168168
// #TODO:
169169
// Default is item.state. But can also be item.custom_field[x], so you can compare with custom value
170170
// Should index then not with item.state but item[custom_field[x]].toLowerCase() or similar...
171171
// Or above, with the mapping of the item using the name?????
172172

173173
// Assume equals operator if not defined...
174174
const operator = item.operator ? item.operator : '==';
175-
176175
switch (operator) {
177176
case '==':
178177
if (typeof (this._stateValue) === 'undefined') {
@@ -209,8 +208,6 @@ export default class BaseTool {
209208
// Unknown operator. Just do nothing and return;
210209
isMatch = false;
211210
}
212-
// Revert state
213-
if (item.state === '-ua-') { item.state = 'unavailable'; }
214211

215212
if (this.dev.debug) console.log('BaseTool, animation, match, value, config, operator', isMatch, this._stateValue, item.state, item.operator);
216213
if (!isMatch) return true;
@@ -449,9 +446,6 @@ export default class BaseTool {
449446
if (color !== '') {
450447
argStyleMap.fill = this.config[this.config.show.style].fill ? color : '';
451448
argStyleMap.stroke = this.config[this.config.show.style].stroke ? color : '';
452-
453-
// this.config[this.config.show.style].fill ? argStyleMap['fill'] = color : '';
454-
// this.config[this.config.show.style].stroke ? argStyleMap['stroke'] = color : '';
455449
}
456450
}
457451
}

Diff for: src/circular-slider-tool.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export default class CircularSliderTool extends BaseTool {
369369

370370
const x = 10 ** dec;
371371
this.labelValue2 = (Math.round(this.pointToSliderValue(m) * x) / x).toFixed(dec);
372-
372+
console.log('updateLabel, labelvalue ', this.labelValue2);
373373
if (this.config.position.label.placement !== 'none') {
374374
this.elements.label.textContent = this.labelValue2;
375375
}
@@ -720,18 +720,19 @@ export default class CircularSliderTool extends BaseTool {
720720
this.MergeAnimationStyleIfChanged();
721721

722722
// this.MergeColorFromState();
723-
724723
this.renderValue = this._stateValue;
724+
// if (this.renderValue === undefined) this.renderValue = 'undefined';
725725
if (this.dragging) {
726726
this.renderValue = this.labelValue2;
727-
} else if (this.elements?.label) this.elements.label.textContent = this.renderValue;
727+
} else if (this.elements?.label) this.elements.label.textContent = (this.renderValue === 'undefined') ? '' : this.renderValue;
728+
728729
function renderLabel(argGroup) {
729730
if ((this.config.position.label.placement === 'thumb') && argGroup) {
730731
return svg`
731732
<text id="label">
732733
<tspan class="${classMap(this.classes.label)}" x="${this.svg.label.cx}" y="${this.svg.label.cy}" style="${styleMap(this.styles.label)}">
733-
${this.renderValue}</tspan>
734-
${this._renderUom()}
734+
${typeof this.renderValue === 'undefined' ? '' : this.renderValue}</tspan>
735+
${typeof this.renderValue === 'undefined' ? '' : this._renderUom()}
735736
</text>
736737
`;
737738
}
@@ -740,8 +741,9 @@ export default class CircularSliderTool extends BaseTool {
740741
return svg`
741742
<text id="label" style="transform-origin:center;transform-box: fill-box;">
742743
<tspan class="${classMap(this.classes.label)}" data-placement="position" x="${this.svg.label.cx}" y="${this.svg.label.cy}"
743-
style="${styleMap(this.styles.label)}">${this.renderValue ? this.renderValue : ''}</tspan>
744-
${this.renderValue ? this._renderUom() : ''}
744+
style="${styleMap(this.styles.label)}">
745+
${typeof this.renderValue === 'undefined' ? '' : this.renderValue}</tspan>
746+
${typeof this.renderValue === 'undefined' ? '' : this._renderUom()}
745747
</text>
746748
`;
747749
}

0 commit comments

Comments
 (0)