Skip to content

Commit

Permalink
fix: Migrate fan model (denysdovhan#41)
Browse files Browse the repository at this point in the history
* fix: dont crash if localization does not exist

* refactor: fix typo tranlated -> translated

* feat: support new fan-model (aka preset_modes)
  • Loading branch information
betaboon authored Mar 22, 2021
1 parent e235c36 commit 0005c3c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
62 changes: 60 additions & 2 deletions src/purifier-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, {
Expand All @@ -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``;
}

Expand Down Expand Up @@ -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`
<paper-menu-button
slot="dropdown-trigger"
.horizontalAlign=${'right'}
.verticalAlign=${'top'}
.verticalOffset=${40}
.noAnimations=${true}
@click="${(e) => e.stopPropagation()}"
>
<paper-button slot="dropdown-trigger">
<ha-icon icon="mdi:fan"></ha-icon>
<span show=${true}
>${localize(`preset_mode.${preset_mode}`) || preset_mode}
</span>
</paper-button>
<paper-listbox
slot="dropdown-content"
selected=${selected}
@click="${(e) => this.handlePresetMode(e)}"
>
${preset_modes.map(
(item) =>
html`<paper-item value=${item}
>${localize(`preset_mode.${item}`) || item}</paper-item
>`
)}
</paper-listbox>
</paper-menu-button>
`;
}

renderAQI() {
const { aqi = {} } = this.config;
const { entity_id, attribute = 'aqi', unit = 'AQI' } = aqi;
Expand Down Expand Up @@ -267,13 +318,18 @@ class PurifierCard extends LitElement {
service,
service_data,
speed,
preset_mode,
xiaomi_miio_favorite_level,
}) => {
const execute = () => {
if (service) {
this.callService(service, service_data);
}

if (preset_mode) {
this.callService('fan.set_preset_mode', { preset_mode });
}

if (speed) {
this.callService('fan.set_speed', { speed });
}
Expand All @@ -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) ||
Expand Down Expand Up @@ -358,6 +415,7 @@ class PurifierCard extends LitElement {
>
<div class="header">
<div class="speed">${this.renderSpeed()}</div>
<div class="preset-mode">${this.renderPresetMode()}</div>
</div>
<div class="image ${className}">${this.renderAQI()}</div>
Expand Down

0 comments on commit 0005c3c

Please sign in to comment.