mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add select platform for Xiaomi Miio fans (#54702)
* Add select platform for Xiaomi Miio purifiers * Add missing condition for AirFresh * Suggested change * Remove fan_set_led_brightness from services.yaml * Remove zhimi.airpurifier.v3
This commit is contained in:
parent
102af02d8a
commit
c937a235e1
@ -46,7 +46,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
GATEWAY_PLATFORMS = ["alarm_control_panel", "light", "sensor", "switch"]
|
||||
SWITCH_PLATFORMS = ["switch"]
|
||||
FAN_PLATFORMS = ["fan", "sensor"]
|
||||
FAN_PLATFORMS = ["fan", "select", "sensor"]
|
||||
HUMIDIFIER_PLATFORMS = [
|
||||
"binary_sensor",
|
||||
"humidifier",
|
||||
|
@ -159,10 +159,8 @@ SERVICE_SET_BUZZER_OFF = "fan_set_buzzer_off"
|
||||
SERVICE_SET_FAN_LED_ON = "fan_set_led_on"
|
||||
SERVICE_SET_FAN_LED_OFF = "fan_set_led_off"
|
||||
SERVICE_SET_FAN_LED = "fan_set_led"
|
||||
SERVICE_SET_LED_BRIGHTNESS = "set_led_brightness"
|
||||
SERVICE_SET_CHILD_LOCK_ON = "fan_set_child_lock_on"
|
||||
SERVICE_SET_CHILD_LOCK_OFF = "fan_set_child_lock_off"
|
||||
SERVICE_SET_LED_BRIGHTNESS = "fan_set_led_brightness"
|
||||
SERVICE_SET_FAVORITE_LEVEL = "fan_set_favorite_level"
|
||||
SERVICE_SET_FAN_LEVEL = "fan_set_fan_level"
|
||||
SERVICE_SET_AUTO_DETECT_ON = "fan_set_auto_detect_on"
|
||||
@ -226,7 +224,6 @@ FEATURE_FLAGS_AIRPURIFIER = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_SET_FAVORITE_LEVEL
|
||||
| FEATURE_SET_LEARN_MODE
|
||||
| FEATURE_RESET_FILTER
|
||||
@ -261,7 +258,6 @@ FEATURE_FLAGS_AIRPURIFIER_3 = (
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_FAVORITE_LEVEL
|
||||
| FEATURE_SET_FAN_LEVEL
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
)
|
||||
|
||||
FEATURE_FLAGS_AIRPURIFIER_V3 = (
|
||||
@ -269,10 +265,7 @@ FEATURE_FLAGS_AIRPURIFIER_V3 = (
|
||||
)
|
||||
|
||||
FEATURE_FLAGS_AIRHUMIDIFIER = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_SET_TARGET_HUMIDITY
|
||||
FEATURE_SET_BUZZER | FEATURE_SET_CHILD_LOCK | FEATURE_SET_TARGET_HUMIDITY
|
||||
)
|
||||
|
||||
FEATURE_FLAGS_AIRHUMIDIFIER_CA_AND_CB = FEATURE_FLAGS_AIRHUMIDIFIER | FEATURE_SET_DRY
|
||||
@ -284,7 +277,6 @@ FEATURE_FLAGS_AIRHUMIDIFIER_MJSSQ = (
|
||||
FEATURE_FLAGS_AIRHUMIDIFIER_CA4 = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_SET_TARGET_HUMIDITY
|
||||
| FEATURE_SET_DRY
|
||||
| FEATURE_SET_MOTOR_SPEED
|
||||
@ -295,7 +287,6 @@ FEATURE_FLAGS_AIRFRESH = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_RESET_FILTER
|
||||
| FEATURE_SET_EXTRA_FEATURES
|
||||
)
|
||||
|
@ -4,18 +4,9 @@ from enum import Enum
|
||||
import logging
|
||||
import math
|
||||
|
||||
from miio.airfresh import (
|
||||
LedBrightness as AirfreshLedBrightness,
|
||||
OperationMode as AirfreshOperationMode,
|
||||
)
|
||||
from miio.airpurifier import (
|
||||
LedBrightness as AirpurifierLedBrightness,
|
||||
OperationMode as AirpurifierOperationMode,
|
||||
)
|
||||
from miio.airpurifier_miot import (
|
||||
LedBrightness as AirpurifierMiotLedBrightness,
|
||||
OperationMode as AirpurifierMiotOperationMode,
|
||||
)
|
||||
from miio.airfresh import OperationMode as AirfreshOperationMode
|
||||
from miio.airpurifier import OperationMode as AirpurifierOperationMode
|
||||
from miio.airpurifier_miot import OperationMode as AirpurifierMiotOperationMode
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.fan import (
|
||||
@ -52,7 +43,6 @@ from .const import (
|
||||
FEATURE_SET_FAVORITE_LEVEL,
|
||||
FEATURE_SET_LEARN_MODE,
|
||||
FEATURE_SET_LED,
|
||||
FEATURE_SET_LED_BRIGHTNESS,
|
||||
FEATURE_SET_VOLUME,
|
||||
KEY_COORDINATOR,
|
||||
KEY_DEVICE,
|
||||
@ -77,7 +67,6 @@ from .const import (
|
||||
SERVICE_SET_FAVORITE_LEVEL,
|
||||
SERVICE_SET_LEARN_MODE_OFF,
|
||||
SERVICE_SET_LEARN_MODE_ON,
|
||||
SERVICE_SET_LED_BRIGHTNESS,
|
||||
SERVICE_SET_VOLUME,
|
||||
)
|
||||
from .device import XiaomiCoordinatedMiioEntity
|
||||
@ -107,7 +96,6 @@ ATTR_FAVORITE_LEVEL = "favorite_level"
|
||||
ATTR_BUZZER = "buzzer"
|
||||
ATTR_CHILD_LOCK = "child_lock"
|
||||
ATTR_LED = "led"
|
||||
ATTR_LED_BRIGHTNESS = "led_brightness"
|
||||
ATTR_BRIGHTNESS = "brightness"
|
||||
ATTR_LEVEL = "level"
|
||||
ATTR_FAN_LEVEL = "fan_level"
|
||||
@ -142,7 +130,6 @@ AVAILABLE_ATTRIBUTES_AIRPURIFIER = {
|
||||
ATTR_AUTO_DETECT: "auto_detect",
|
||||
ATTR_USE_TIME: "use_time",
|
||||
ATTR_BUZZER: "buzzer",
|
||||
ATTR_LED_BRIGHTNESS: "led_brightness",
|
||||
ATTR_SLEEP_MODE: "sleep_mode",
|
||||
}
|
||||
|
||||
@ -172,7 +159,6 @@ AVAILABLE_ATTRIBUTES_AIRPURIFIER_3 = {
|
||||
ATTR_LED: "led",
|
||||
ATTR_USE_TIME: "use_time",
|
||||
ATTR_BUZZER: "buzzer",
|
||||
ATTR_LED_BRIGHTNESS: "led_brightness",
|
||||
ATTR_FAN_LEVEL: "fan_level",
|
||||
}
|
||||
|
||||
@ -195,7 +181,6 @@ AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3 = {
|
||||
AVAILABLE_ATTRIBUTES_AIRFRESH = {
|
||||
ATTR_MODE: "mode",
|
||||
ATTR_LED: "led",
|
||||
ATTR_LED_BRIGHTNESS: "led_brightness",
|
||||
ATTR_BUZZER: "buzzer",
|
||||
ATTR_CHILD_LOCK: "child_lock",
|
||||
ATTR_USE_TIME: "use_time",
|
||||
@ -236,7 +221,6 @@ FEATURE_FLAGS_AIRPURIFIER = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_SET_FAVORITE_LEVEL
|
||||
| FEATURE_SET_LEARN_MODE
|
||||
| FEATURE_RESET_FILTER
|
||||
@ -271,7 +255,6 @@ FEATURE_FLAGS_AIRPURIFIER_3 = (
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_FAVORITE_LEVEL
|
||||
| FEATURE_SET_FAN_LEVEL
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
)
|
||||
|
||||
FEATURE_FLAGS_AIRPURIFIER_V3 = (
|
||||
@ -282,17 +265,12 @@ FEATURE_FLAGS_AIRFRESH = (
|
||||
FEATURE_SET_BUZZER
|
||||
| FEATURE_SET_CHILD_LOCK
|
||||
| FEATURE_SET_LED
|
||||
| FEATURE_SET_LED_BRIGHTNESS
|
||||
| FEATURE_RESET_FILTER
|
||||
| FEATURE_SET_EXTRA_FEATURES
|
||||
)
|
||||
|
||||
AIRPURIFIER_SERVICE_SCHEMA = vol.Schema({vol.Optional(ATTR_ENTITY_ID): cv.entity_ids})
|
||||
|
||||
SERVICE_SCHEMA_LED_BRIGHTNESS = AIRPURIFIER_SERVICE_SCHEMA.extend(
|
||||
{vol.Required(ATTR_BRIGHTNESS): vol.All(vol.Coerce(int), vol.Clamp(min=0, max=2))}
|
||||
)
|
||||
|
||||
SERVICE_SCHEMA_FAVORITE_LEVEL = AIRPURIFIER_SERVICE_SCHEMA.extend(
|
||||
{vol.Required(ATTR_LEVEL): vol.All(vol.Coerce(int), vol.Clamp(min=0, max=17))}
|
||||
)
|
||||
@ -321,10 +299,6 @@ SERVICE_TO_METHOD = {
|
||||
SERVICE_SET_LEARN_MODE_ON: {"method": "async_set_learn_mode_on"},
|
||||
SERVICE_SET_LEARN_MODE_OFF: {"method": "async_set_learn_mode_off"},
|
||||
SERVICE_RESET_FILTER: {"method": "async_reset_filter"},
|
||||
SERVICE_SET_LED_BRIGHTNESS: {
|
||||
"method": "async_set_led_brightness",
|
||||
"schema": SERVICE_SCHEMA_LED_BRIGHTNESS,
|
||||
},
|
||||
SERVICE_SET_FAVORITE_LEVEL: {
|
||||
"method": "async_set_favorite_level",
|
||||
"schema": SERVICE_SCHEMA_FAVORITE_LEVEL,
|
||||
@ -792,17 +766,6 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
|
||||
False,
|
||||
)
|
||||
|
||||
async def async_set_led_brightness(self, brightness: int = 2):
|
||||
"""Set the led brightness."""
|
||||
if self._device_features & FEATURE_SET_LED_BRIGHTNESS == 0:
|
||||
return
|
||||
|
||||
await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirpurifierLedBrightness(brightness),
|
||||
)
|
||||
|
||||
async def async_set_favorite_level(self, level: int = 1):
|
||||
"""Set the favorite level."""
|
||||
if self._device_features & FEATURE_SET_FAVORITE_LEVEL == 0:
|
||||
@ -987,17 +950,6 @@ class XiaomiAirPurifierMiot(XiaomiAirPurifier):
|
||||
self._mode = AirpurifierMiotOperationMode[speed.title()].value
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_set_led_brightness(self, brightness: int = 2):
|
||||
"""Set the led brightness."""
|
||||
if self._device_features & FEATURE_SET_LED_BRIGHTNESS == 0:
|
||||
return
|
||||
|
||||
await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirpurifierMiotLedBrightness(brightness),
|
||||
)
|
||||
|
||||
|
||||
class XiaomiAirFresh(XiaomiGenericDevice):
|
||||
"""Representation of a Xiaomi Air Fresh."""
|
||||
@ -1134,17 +1086,6 @@ class XiaomiAirFresh(XiaomiGenericDevice):
|
||||
False,
|
||||
)
|
||||
|
||||
async def async_set_led_brightness(self, brightness: int = 2):
|
||||
"""Set the led brightness."""
|
||||
if self._device_features & FEATURE_SET_LED_BRIGHTNESS == 0:
|
||||
return
|
||||
|
||||
await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirfreshLedBrightness(brightness),
|
||||
)
|
||||
|
||||
async def async_set_extra_features(self, features: int = 1):
|
||||
"""Set the extra features."""
|
||||
if self._device_features & FEATURE_SET_EXTRA_FEATURES == 0:
|
||||
|
@ -4,8 +4,11 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
from miio.airfresh import LedBrightness as AirfreshLedBrightness
|
||||
from miio.airhumidifier import LedBrightness as AirhumidifierLedBrightness
|
||||
from miio.airhumidifier_miot import LedBrightness as AirhumidifierMiotLedBrightness
|
||||
from miio.airpurifier import LedBrightness as AirpurifierLedBrightness
|
||||
from miio.airpurifier_miot import LedBrightness as AirpurifierMiotLedBrightness
|
||||
|
||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||
from homeassistant.core import callback
|
||||
@ -18,8 +21,12 @@ from .const import (
|
||||
FEATURE_SET_LED_BRIGHTNESS,
|
||||
KEY_COORDINATOR,
|
||||
KEY_DEVICE,
|
||||
MODEL_AIRFRESH_VA2,
|
||||
MODEL_AIRPURIFIER_M1,
|
||||
MODEL_AIRPURIFIER_M2,
|
||||
MODELS_HUMIDIFIER_MIIO,
|
||||
MODELS_HUMIDIFIER_MIOT,
|
||||
MODELS_PURIFIER_MIOT,
|
||||
)
|
||||
from .device import XiaomiCoordinatedMiioEntity
|
||||
|
||||
@ -27,10 +34,10 @@ ATTR_LED_BRIGHTNESS = "led_brightness"
|
||||
|
||||
|
||||
LED_BRIGHTNESS_MAP = {"Bright": 0, "Dim": 1, "Off": 2}
|
||||
LED_BRIGHTNESS_MAP_MIOT = {"Bright": 2, "Dim": 1, "Off": 0}
|
||||
LED_BRIGHTNESS_MAP_HUMIDIFIER_MIOT = {"Bright": 2, "Dim": 1, "Off": 0}
|
||||
LED_BRIGHTNESS_REVERSE_MAP = {val: key for key, val in LED_BRIGHTNESS_MAP.items()}
|
||||
LED_BRIGHTNESS_REVERSE_MAP_MIOT = {
|
||||
val: key for key, val in LED_BRIGHTNESS_MAP_MIOT.items()
|
||||
LED_BRIGHTNESS_REVERSE_MAP_HUMIDIFIER_MIOT = {
|
||||
val: key for key, val in LED_BRIGHTNESS_MAP_HUMIDIFIER_MIOT.items()
|
||||
}
|
||||
|
||||
|
||||
@ -65,6 +72,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
entity_class = XiaomiAirHumidifierSelector
|
||||
elif model in MODELS_HUMIDIFIER_MIOT:
|
||||
entity_class = XiaomiAirHumidifierMiotSelector
|
||||
elif model in [MODEL_AIRPURIFIER_M1, MODEL_AIRPURIFIER_M2]:
|
||||
entity_class = XiaomiAirPurifierSelector
|
||||
elif model in MODELS_PURIFIER_MIOT:
|
||||
entity_class = XiaomiAirPurifierMiotSelector
|
||||
elif model == MODEL_AIRFRESH_VA2:
|
||||
entity_class = XiaomiAirFreshSelector
|
||||
else:
|
||||
return
|
||||
|
||||
@ -150,14 +163,62 @@ class XiaomiAirHumidifierMiotSelector(XiaomiAirHumidifierSelector):
|
||||
@property
|
||||
def led_brightness(self):
|
||||
"""Return the current led brightness."""
|
||||
return LED_BRIGHTNESS_REVERSE_MAP_MIOT.get(self._current_led_brightness)
|
||||
return LED_BRIGHTNESS_REVERSE_MAP_HUMIDIFIER_MIOT.get(
|
||||
self._current_led_brightness
|
||||
)
|
||||
|
||||
async def async_set_led_brightness(self, brightness: str):
|
||||
async def async_set_led_brightness(self, brightness: str) -> None:
|
||||
"""Set the led brightness."""
|
||||
if await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirhumidifierMiotLedBrightness(LED_BRIGHTNESS_MAP_MIOT[brightness]),
|
||||
AirhumidifierMiotLedBrightness(
|
||||
LED_BRIGHTNESS_MAP_HUMIDIFIER_MIOT[brightness]
|
||||
),
|
||||
):
|
||||
self._current_led_brightness = LED_BRIGHTNESS_MAP_MIOT[brightness]
|
||||
self._current_led_brightness = LED_BRIGHTNESS_MAP_HUMIDIFIER_MIOT[
|
||||
brightness
|
||||
]
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class XiaomiAirPurifierSelector(XiaomiAirHumidifierSelector):
|
||||
"""Representation of a Xiaomi Air Purifier (MIIO protocol) selector."""
|
||||
|
||||
async def async_set_led_brightness(self, brightness: str) -> None:
|
||||
"""Set the led brightness."""
|
||||
if await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirpurifierLedBrightness(LED_BRIGHTNESS_MAP[brightness]),
|
||||
):
|
||||
self._current_led_brightness = LED_BRIGHTNESS_MAP[brightness]
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class XiaomiAirPurifierMiotSelector(XiaomiAirHumidifierSelector):
|
||||
"""Representation of a Xiaomi Air Purifier (MiOT protocol) selector."""
|
||||
|
||||
async def async_set_led_brightness(self, brightness: str) -> None:
|
||||
"""Set the led brightness."""
|
||||
if await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirpurifierMiotLedBrightness(LED_BRIGHTNESS_MAP[brightness]),
|
||||
):
|
||||
self._current_led_brightness = LED_BRIGHTNESS_MAP[brightness]
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class XiaomiAirFreshSelector(XiaomiAirHumidifierSelector):
|
||||
"""Representation of a Xiaomi Air Fresh selector."""
|
||||
|
||||
async def async_set_led_brightness(self, brightness: str) -> None:
|
||||
"""Set the led brightness."""
|
||||
if await self._try_command(
|
||||
"Setting the led brightness of the miio device failed.",
|
||||
self._device.set_led_brightness,
|
||||
AirfreshLedBrightness(LED_BRIGHTNESS_MAP[brightness]),
|
||||
):
|
||||
self._current_led_brightness = LED_BRIGHTNESS_MAP[brightness]
|
||||
self.async_write_ha_state()
|
||||
|
@ -101,24 +101,6 @@ fan_set_fan_level:
|
||||
min: 1
|
||||
max: 3
|
||||
|
||||
fan_set_led_brightness:
|
||||
name: Fan set LED brightness
|
||||
description: Set the led brightness.
|
||||
fields:
|
||||
entity_id:
|
||||
description: Name of the xiaomi miio entity.
|
||||
selector:
|
||||
entity:
|
||||
integration: xiaomi_miio
|
||||
domain: fan
|
||||
brightness:
|
||||
description: Brightness (0 = Bright, 1 = Dim, 2 = Off)
|
||||
required: true
|
||||
selector:
|
||||
number:
|
||||
min: 0
|
||||
max: 2
|
||||
|
||||
fan_set_auto_detect_on:
|
||||
name: Fan set auto detect on
|
||||
description: Turn the auto detect on.
|
||||
|
Loading…
x
Reference in New Issue
Block a user