diff --git a/src/localize.js b/src/localize.js index 6342b0b..28f3534 100644 --- a/src/localize.js +++ b/src/localize.js @@ -42,25 +42,29 @@ export default function localize(string, search, replace) { .replace(/['"]+/g, '') .replace('-', '_'); - let tranlated; + let translated; try { - tranlated = languages[lang][section][key]; + translated = languages[lang][section][key]; } catch (e) { - tranlated = languages[DEFAULT_LANG][section][key]; + /**/ } - if (tranlated === undefined) { - tranlated = languages[DEFAULT_LANG][section][key]; + if (translated === undefined) { + try { + translated = languages[DEFAULT_LANG][section][key]; + } catch (e) { + /**/ + } } - if (tranlated === undefined) { + if (translated === undefined) { return; } if (search !== '' && replace !== '') { - tranlated = tranlated.replace(search, replace); + translated = translated.replace(search, replace); } - return tranlated; + return translated; } diff --git a/src/purifier-card.js b/src/purifier-card.js index 02eb1f2..64dbe8a 100644 --- a/src/purifier-card.js +++ b/src/purifier-card.js @@ -11,6 +11,9 @@ if (!customElements.get('ha-icon-button')) { ); } +const SUPPORT_SET_SPEED = 1; +const SUPPORT_PRESET_MODE = 8; + class PurifierCard extends LitElement { static get properties() { return { @@ -118,6 +121,11 @@ class PurifierCard extends LitElement { this.callService('fan.set_speed', { speed }); } + handlePresetMode(e) { + const preset_mode = e.target.getAttribute('value'); + this.callService('fan.set_preset_mode', { preset_mode }); + } + callService(service, options = {}, isRequest = true) { const [domain, name] = service.split('.'); this.hass.callService(domain, name, { @@ -133,10 +141,11 @@ class PurifierCard extends LitElement { renderSpeed() { const { - attributes: { speed, speed_list }, + attributes: { speed, speed_list, supported_features }, } = this.entity; - if (!speed_list) { + // TODO handle percentages + if (!speed_list || !(supported_features & SUPPORT_SET_SPEED)) { return html``; } @@ -171,6 +180,48 @@ class PurifierCard extends LitElement { `; } + renderPresetMode() { + const { + attributes: { preset_mode, preset_modes, supported_features }, + } = this.entity; + + if (!preset_modes || !(supported_features & SUPPORT_PRESET_MODE)) { + return html``; + } + + const selected = preset_modes.indexOf(preset_mode); + + return html` + + + + ${localize(`preset_mode.${preset_mode}`) || preset_mode} + + + + ${preset_modes.map( + (item) => + html`${localize(`preset_mode.${item}`) || item}` + )} + + + `; + } + renderAQI() { const { aqi = {} } = this.config; const { entity_id, attribute = 'aqi', unit = 'AQI' } = aqi; @@ -267,6 +318,7 @@ class PurifierCard extends LitElement { service, service_data, speed, + preset_mode, xiaomi_miio_favorite_level, }) => { const execute = () => { @@ -274,6 +326,10 @@ class PurifierCard extends LitElement { this.callService(service, service_data); } + if (preset_mode) { + this.callService('fan.set_preset_mode', { preset_mode }); + } + if (speed) { this.callService('fan.set_speed', { speed }); } @@ -294,6 +350,7 @@ class PurifierCard extends LitElement { const isActive = service || + preset_mode === attributes.preset_mode || // Speed with specific favorite level (speed === attributes.speed && xiaomi_miio_favorite_level === attributes.favorite_level) || @@ -358,6 +415,7 @@ class PurifierCard extends LitElement { >
${this.renderSpeed()}
+
${this.renderPresetMode()}
${this.renderAQI()}