|
| 1 | +import { computeStateDisplay, computeStateDomain } from 'custom-card-helpers' |
| 2 | +import { html } from 'lit' |
| 3 | + |
| 4 | +export function getConfigDefaults (config) { |
| 5 | + return { |
| 6 | + borderRadius: config && config.borderRadius ? config.borderRadius : '12px', |
| 7 | + faderWidth: config && config.faderWidth ? config.faderWidth : '150px', |
| 8 | + faderHeight: config && config.faderHeight ? config.faderHeight : '400px', |
| 9 | + faderInactiveColor: config && config.faderInactiveColor ? config.faderInactiveColor : '#f00', |
| 10 | + faderThumbColor: config && config.faderThumbColor ? config.faderThumbColor : '#ddd', |
| 11 | + faderTrackColor: config && config.faderTrackColor ? config.faderTrackColor : '#ddd', |
| 12 | + faderActiveColor: config && config.faderActiveColor ? config.faderActiveColor : '#22ba00', |
| 13 | + faderTheme: config && config.faderTheme ? config.faderTheme : 'modern', |
| 14 | + updateWhileMoving: config && config.updateWhileMoving ? config.updateWhileMoving : false, |
| 15 | + alwaysShowFaderValue: config && config.alwaysShowFaderValue ? config.alwaysShowFaderValue : false, |
| 16 | + haCard: config && config.haCard !== undefined ? config.haCard : true, |
| 17 | + description: config && config.description ? config.description : '', |
| 18 | + title: config && config.title ? config.title : '' |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export function generateHeader (cfg) { |
| 23 | + const header = cfg.title ? html`<h1 class='card-header'><div class='name'>${cfg.title}</div></div>` : '' |
| 24 | + const desc = cfg.description ? html`<p class='mixer-description'>${cfg.description}</p>` : '' |
| 25 | + return html`${header}${desc}` |
| 26 | +} |
| 27 | + |
| 28 | +export function getFaderStyle (faderColors, cfg, activeState) { |
| 29 | + let style = `--fader-width: ${cfg.faderWidth}; --fader-height: ${cfg.faderHeight}; --fader-border-radius: ${cfg.borderRadius}; ` |
| 30 | + style += `--fader-color: ${activeState === 'on' ? faderColors.active : faderColors.inactive}; ` |
| 31 | + style += `--fader-thumb-color: ${faderColors.thumb}; --fader-track-color: ${faderColors.track}; --fader-track-inactive-color: ${faderColors.inactive};` |
| 32 | + return style |
| 33 | +} |
| 34 | + |
| 35 | +export function getFaderColor (faderRow, cfg) { |
| 36 | + return { |
| 37 | + track: faderRow.track_color || cfg.faderTrackColor, |
| 38 | + active: faderRow.active_color || cfg.faderActiveColor, |
| 39 | + inactive: faderRow.inactive_color || cfg.faderInactiveColor, |
| 40 | + thumb: faderRow.thumb_color || cfg.faderThumbColor |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function getFaderIcon (faderRow, stateObj, activeState) { |
| 45 | + return activeState === 'on' ? 'mdi:volume-high' : 'mdi:volume-mute' |
| 46 | +} |
| 47 | + |
| 48 | +export function getFaderValue (faderRow, stateObj, hass) { |
| 49 | + const maxValue = (typeof faderRow.max === 'number') ? faderRow.max : stateObj.attributes.max || 1 |
| 50 | + const minValue = (typeof faderRow.min === 'number') ? faderRow.min : stateObj.attributes.min || 0 |
| 51 | + let rawValue = 0 |
| 52 | + const domain = computeStateDomain(stateObj) |
| 53 | + if (domain === 'media_player') { |
| 54 | + rawValue = stateObj.attributes.volume_level || 0 |
| 55 | + } else { |
| 56 | + rawValue = stateObj.state |
| 57 | + } |
| 58 | + const inputValue = Math.round((rawValue - minValue) / (maxValue - minValue) * 100) |
| 59 | + let displayValue = inputValue + '%' |
| 60 | + if (faderRow.value_entity_id && Object.prototype.hasOwnProperty.call(hass.states, faderRow.value_entity_id)) { |
| 61 | + displayValue = computeStateDisplay(hass.localize, hass.states[faderRow.value_entity_id], hass.language) |
| 62 | + } else if (faderRow.value_attribute && Object.prototype.hasOwnProperty.call(stateObj.attributes, faderRow.value_attribute)) { |
| 63 | + displayValue = stateObj.attributes[faderRow.value_attribute] |
| 64 | + } |
| 65 | + const suffix = faderRow.value_suffix || '' |
| 66 | + if (suffix) { |
| 67 | + displayValue += ` ${suffix}` |
| 68 | + } |
| 69 | + return { displayValue, inputValue } |
| 70 | +} |
0 commit comments