Skip to content

Commit

Permalink
refactor: remove deprecated fan.set_speed service
Browse files Browse the repository at this point in the history
Migrate from fan.set_speed to fan.set_preset_mode and fan.set_percentage

BREAKING CHANGE: `speed` option for `shortcuts` is replaced by `preset_mode` and `percentage`
  • Loading branch information
denysdovhan committed Dec 1, 2021
1 parent 3ae9086 commit bc93b78
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 67 deletions.
67 changes: 32 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,22 @@ stats:
shortcuts:
- name: Silent
icon: 'mdi:weather-night'
speed: Silent
preset_mode: Silent
- name: 25%
icon: 'mdi:circle-slice-2'
speed: Favorite
xiaomi_miio_favorite_level: 3
percentage: 25
- name: 50%
icon: 'mdi:circle-slice-4'
speed: Favorite
xiaomi_miio_favorite_level: 7
percentage: 50
- name: 75%
icon: 'mdi:circle-slice-6'
speed: Favorite
xiaomi_miio_favorite_level: 10
percentage: 50
- name: 100%
icon: 'mdi:circle-slice-8'
speed: Favorite
xiaomi_miio_favorite_level: 14
percentage: 100
- name: Auto
icon: 'mdi:brightness-auto'
speed: Auto
preset_mode: Auto
show_name: true
show_state: true
show_toolbar: true
Expand All @@ -102,20 +98,19 @@ compact_view: false

Here is what every option means:

| Name | Type | Default | Description |
| ------------------ | :-------: | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type` | `string` | **Required** | `custom:purifier-card` |
| `entity` | `string` | **Required** | An entity_id within the `fan` domain. |
| `show_name` | `boolean` | `true` | Show friendly name of the purifier. |
| `show_status` | `boolean` | `true` | Show status of the purifier. |
| `show_speed` | `boolean` | `false` | Show speed of the purifier in the header. |
| `show_preset_mode` | `boolean` | `true` | Show preset mode of the purifier in the header. |
| `show_toolbar` | `boolean` | `true` | Show toolbar with shortcuts. |
| `compact_view` | `boolean` | `false` | Compact view without image. |
| `aqi` | `object` | Optional | Custom entity or attribute for AQI value. |
| `stats` | `object` | Optional | Custom per state stats for your purifier cleaner |
| `shortcuts` | `object` | Optional | Custom shortcuts for your purifier cleaner. |
| `platform` | `string` | Optional | Default 'xiaomi_miio', for [Xiaomi Mi Air Purifier & Xiaomi Mi Air Humidifier Integration](https://github.com/syssi/xiaomi_airpurifier) you must specify `xiaomi_miio_airpurifier` |
| Name | Type | Default | Description |
| ------------------ | :-------: | ------------ | ------------------------------------------------ |
| `type` | `string` | **Required** | `custom:purifier-card` |
| `entity` | `string` | **Required** | An entity_id within the `fan` domain. |
| `show_name` | `boolean` | `true` | Show friendly name of the purifier. |
| `show_status` | `boolean` | `true` | Show status of the purifier. |
| `show_speed` | `boolean` | `false` | Show speed of the purifier in the header. |
| `show_preset_mode` | `boolean` | `true` | Show preset mode of the purifier in the header. |
| `show_toolbar` | `boolean` | `true` | Show toolbar with shortcuts. |
| `compact_view` | `boolean` | `false` | Compact view without image. |
| `aqi` | `object` | Optional | Custom entity or attribute for AQI value. |
| `stats` | `object` | Optional | Custom per state stats for your purifier cleaner |
| `shortcuts` | `object` | Optional | Custom shortcuts for your purifier cleaner. |

### `aqi` object

Expand All @@ -138,22 +133,24 @@ You can use any attribute of purifier or even any entity by `entity_id` to displ

### `shortcuts` object

You can define [custom scripts][ha-scripts] for custom actions or add shortcuts for switching modes and speeds via `shortcuts` option.
You can define [custom scripts][ha-scripts] for custom actions or add shortcuts for switching presets and speeds via `shortcuts` option.

| Name | Type | Default | Description |
| ---------------------------- | :------: | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | `string` | Optional | Friendly name of the shortcut, i.e. `Switch to Auto`. |
| `icon` | `string` | Optional | Any icon for shortcut button. |
| `service` | `string` | Optional | A service to call, i.e. `script.clean_air`. |
| `service_data` | `object` | Optional | `service_data` for `service` call |
| `speed` | `object` | Optional | A `speed` to switch to, i.e. `Auto`, etc |
| `xiaomi_miio_favorite_level` | `object` | Optional | A [favorite level][xiaomi-miio-favorite-levels] of the operation mode `Favorite` for Xioami Air Purifiers. `speed` is required with this option |
| Name | Type | Default | Description |
| ---------------------------- | :------: | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | `string` | Optional | Friendly name of the shortcut, i.e. `Switch to Auto`. |
| `icon` | `string` | Optional | Any icon for shortcut button. |
| `service` | `string` | Optional | A service to call, i.e. `script.clean_air`. |
| `service_data` | `object` | Optional | `service_data` for `service` call |
| `percentage` | `object` | Optional | A `percentage` to switch to, i.e. `27`, etc. See `entity`'s `percentage_step` for valid values. |
| `preset_mode` | `object` | Optional | A `speed` to switch to, i.e. `Auto`, etc |
| `xiaomi_miio_favorite_level` | `object` | Optional | A [favorite level][xiaomi-miio-favorite-levels] of the operation mode `Favorite` for Xioami Air Purifiers. `preset_mode` is required with this option |

The card will automatically try to figure out which one of shortcuts is currently active. The shortcut will be highlighted when:

1. It's a service.
2. `entity`'s `speed` attribute is equal to `shortcut`'s `speed`.
3. `entity`'s `speed` attribute and `favorite_level` is equal to `shortcut`'s `speed` and `xiaomi_miio_favorite_level` correspondingly.
2. `entity`'s `percentage` attribute is equal to `shortcut`'s `percentage`.
3. `entity`'s `preset_mode` attribute is equal to `shortcut`'s `preset_mode`.
4. `entity`'s `preset_mode` attribute and `favorite_level` is equal to `shortcut`'s `preset_mode` and `xiaomi_miio_favorite_level` correspondingly.

## Animations

Expand Down
30 changes: 17 additions & 13 deletions src/purifier-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ class PurifierCard extends LitElement {
icon,
service,
service_data,
speed,
preset_mode,
percentage,
xiaomi_miio_favorite_level,
}) => {
const execute = () => {
Expand All @@ -378,32 +378,36 @@ class PurifierCard extends LitElement {
this.callService('fan.set_preset_mode', { preset_mode });
}

if (speed) {
this.callService('fan.set_speed', { speed });
}

if (speed && xiaomi_miio_favorite_level) {
this.callService('fan.set_speed', { speed });
if (preset_mode && xiaomi_miio_favorite_level) {
setTimeout(() => {
this.callService(this.platform + '.fan_set_favorite_level', {
level: xiaomi_miio_favorite_level,
});
}, 500);
}

if (!speed && xiaomi_miio_favorite_level) {
throw new Error(localize('error.xiaomi_miio_level_without_speed'));
if (!preset_mode && xiaomi_miio_favorite_level) {
throw new Error(
localize('error.xiaomi_miio_level_without_preset_mode')
);
}

if (percentage) {
this.callService('fan.set_percentage', { percentage });
}
};

const isActive =
service ||
percentage === attributes.percentage ||
preset_mode === attributes.preset_mode ||
// Speed with specific favorite level
(speed === attributes.speed &&
// preset_mode with specific favorite level
(preset_mode === attributes.preset_mode &&
xiaomi_miio_favorite_level === attributes.favorite_level) ||
// Specific speed with no specific favorite level
(speed === attributes.speed && !xiaomi_miio_favorite_level);
// specific preset_mode with no specific favorite level
(preset_mode === attributes.preset_mode &&
!xiaomi_miio_favorite_level);

const className = isActive ? 'active' : '';

return html`
Expand Down
2 changes: 1 addition & 1 deletion src/translations/bg.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "Избирането на обект е задължително!",
"xiaomi_miio_level_without_speed": "speed е задължителен параметър, когато се използва xiaomi_miio_favorite_level!"
"xiaomi_miio_level_without_preset_mode": "preset_mode е задължителен параметър, когато се използва xiaomi_miio_favorite_level!"
},
"editor": {
"entity": "Обект (Задължително)",
Expand Down
3 changes: 1 addition & 2 deletions src/translations/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"Fan": "Ventilador"
},
"error": {
"missing_entity": "Cal especificar una entitat.",
"xiaomi_miio_level_without_speed": "es requereix la velocitat juntament amb la propietat xiaomi_miio_favorite_level."
"missing_entity": "Cal especificar una entitat."
},
"editor": {
"entity": "Entitat (Requerit)",
Expand Down
3 changes: 1 addition & 2 deletions src/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"Fan": "Lüfter"
},
"error": {
"missing_entity": "Es muss eine Entity definiert werden!",
"xiaomi_miio_level_without_speed": "Geschwindigkeit ist zusammen mit der xiaomi_miio_favorite_level Eigenschaft erforderlich!"
"missing_entity": "Es muss eine Entity definiert werden!"
},
"editor": {
"entity": "Entity (Erforderlich)",
Expand Down
2 changes: 1 addition & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "Specifying entity is required!",
"xiaomi_miio_level_without_speed": "speed is required along with xiaomi_miio_favorite_level property!"
"xiaomi_miio_level_without_preset_mode": "preset_mode is required along with xiaomi_miio_favorite_level property!"
},
"warning": {
"actions_array": "WARNING: 'actions' is renamed to 'shortcuts'"
Expand Down
2 changes: 1 addition & 1 deletion src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "Il est obligatoire de spécifier une entité!",
"xiaomi_miio_level_without_speed": "speed est obligatoire avec la propriété xiaomi_miio_favorite_level!"
"xiaomi_miio_level_without_preset_mode": "preset_mode est obligatoire avec la propriété xiaomi_miio_favorite_level!"
},
"editor": {
"entity": "Entité (obligatoire)",
Expand Down
3 changes: 1 addition & 2 deletions src/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"Fan": "Ventola"
},
"error": {
"missing_entity": "É necessario specificare l'entità",
"xiaomi_miio_level_without_speed": "É richiesto l'inserimento della velocità insieme alla proprietà xiaomi_miio_favorite_level!"
"missing_entity": "É necessario specificare l'entità"
},
"editor": {
"entity": "Entità (richiesto)",
Expand Down
3 changes: 1 addition & 2 deletions src/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"Fan": "Vifte"
},
"error": {
"missing_entity": "Spesifiserende enhet kreves!",
"xiaomi_miio_level_without_speed": "hastighet kreves sammen med eiendommen xiaomi_miio_favorite_level!"
"missing_entity": "Spesifiserende enhet kreves!"
},
"editor": {
"entity": "Enhet (påkrevd)",
Expand Down
2 changes: 1 addition & 1 deletion src/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"error": {
"missing_entity": "Wymagane jest zadeklarowanie encji!",
"xiaomi_miio_level_without_speed": "parametr speed jest wymagany w wywołaniu z xiaomi_miio_favorite_level!"
"xiaomi_miio_level_without_preset_mode": "parametr preset_mode jest wymagany w wywołaniu z xiaomi_miio_favorite_level!"
},
"editor": {
"entity": "Encja (wymagane)",
Expand Down
4 changes: 2 additions & 2 deletions src/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "Требуется указать сущность!",
"xiaomi_miio_level_without_speed": "Требуется указать скорость вместе с свойством xiaomi_miio_favorite_level!"
"xiaomi_miio_level_without_preset_mode": "Требуется указать preset_mode вместе с свойством xiaomi_miio_favorite_level!"
},
"editor": {
"entity": "Сущность (Обязательно!)",
Expand All @@ -35,4 +35,4 @@
"show_toolbar_aria_label_off": "Скрыть панель инструментов",
"code_only_note": "Примечание: Настройки действий и параметров состояний доступны только в редакторе кода."
}
}
}
3 changes: 1 addition & 2 deletions src/translations/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"Fan": "Fan"
},
"error": {
"missing_entity": "Varlığı belirtmeniz gereklidir!",
"xiaomi_miio_level_without_speed": "Hız xiaomi_miio_favorite_level için gereklidir!"
"missing_entity": "Varlığı belirtmeniz gereklidir!"
},
"editor": {
"entity": "Varlık (Gerekli)",
Expand Down
2 changes: 1 addition & 1 deletion src/translations/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "Сутність є обов’язковим полем!і",
"xiaomi_miio_level_without_speed": "Поле speed є обов’язковим разом з xiaomi_miio_favorite_level!"
"xiaomi_miio_level_without_preset_mode": "Поле preset_mode є обов’язковим разом з xiaomi_miio_favorite_level!"
},
"editor": {
"entity": "Об’єкт (Required)",
Expand Down
2 changes: 1 addition & 1 deletion src/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "必须指定实体",
"xiaomi_miio_level_without_speed": "speed 和 xiaomi_miio_favorite_level 属性是必需的!"
"xiaomi_miio_level_without_preset_mode": "preset_mode 和 xiaomi_miio_favorite_level 属性是必需的!"
},
"editor": {
"entity": "实体 (必填)",
Expand Down
2 changes: 1 addition & 1 deletion src/translations/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"error": {
"missing_entity": "必須指定實體名稱!",
"xiaomi_miio_level_without_speed": "設定 xiaomi_miio_favorite_level 屬性時必須包含 speed 屬性!"
"xiaomi_miio_level_without_preset_mode": "設定 xiaomi_miio_favorite_level 屬性時必須包含 preset_mode 屬性!"
},
"editor": {
"entity": "實體名稱(必填)",
Expand Down

0 comments on commit bc93b78

Please sign in to comment.