Skip to content

Commit

Permalink
refactor: drop xiaomi_miio_favorite_level option
Browse files Browse the repository at this point in the history
Remove xiaomi_miio_favorite_level options for shortcuts

BREAKING CHANGE: xiaomi_miio_favorite_level is removed
  • Loading branch information
denysdovhan committed Dec 1, 2021
1 parent 81f302d commit f20fdcc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 40 deletions.
18 changes: 8 additions & 10 deletions README.md
Expand Up @@ -136,22 +136,20 @@ You can use any attribute of purifier or even any entity by `entity_id` to displ

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 |
| `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 |
| 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 |

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 `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
32 changes: 2 additions & 30 deletions src/purifier-card.js
Expand Up @@ -360,15 +360,7 @@ class PurifierCard extends LitElement {
}

const buttons = shortcuts.map(
({
name,
icon,
service,
service_data,
preset_mode,
percentage,
xiaomi_miio_favorite_level,
}) => {
({ name, icon, service, service_data, preset_mode, percentage }) => {
const execute = () => {
if (service) {
this.callService(service, service_data);
Expand All @@ -378,20 +370,6 @@ class PurifierCard extends LitElement {
this.callService('fan.set_preset_mode', { preset_mode });
}

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

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 });
}
Expand All @@ -400,13 +378,7 @@ class PurifierCard extends LitElement {
const isActive =
service ||
percentage === attributes.percentage ||
preset_mode === attributes.preset_mode ||
// preset_mode with specific favorite level
(preset_mode === attributes.preset_mode &&
xiaomi_miio_favorite_level === attributes.favorite_level) ||
// specific preset_mode with no specific favorite level
(preset_mode === attributes.preset_mode &&
!xiaomi_miio_favorite_level);
preset_mode === attributes.preset_mode;

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

Expand Down

0 comments on commit f20fdcc

Please sign in to comment.