Skip to content

Commit

Permalink
Adding support for Mi AirPurifier 3
Browse files Browse the repository at this point in the history
  • Loading branch information
foxel committed Feb 11, 2020
1 parent 8a61581 commit 9cd4464
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
62 changes: 58 additions & 4 deletions homeassistant/components/xiaomi_miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
AirFresh,
AirHumidifier,
AirPurifier,
AirPurifierMiot,
Device,
DeviceException,
)
Expand All @@ -23,6 +24,10 @@
LedBrightness as AirpurifierLedBrightness,
OperationMode as AirpurifierOperationMode,
)
from miio.airpurifier_miot import ( # pylint: disable=import-error, import-error
LedBrightness as AirpurifierMiotLedBrightness,
OperationMode as AirpurifierMiotOperationMode,
)
import voluptuous as vol

from homeassistant.components.fan import PLATFORM_SCHEMA, SUPPORT_SET_SPEED, FanEntity
Expand Down Expand Up @@ -77,6 +82,7 @@
MODEL_AIRPURIFIER_SA1 = "zhimi.airpurifier.sa1"
MODEL_AIRPURIFIER_SA2 = "zhimi.airpurifier.sa2"
MODEL_AIRPURIFIER_2S = "zhimi.airpurifier.mc1"
MODEL_AIRPURIFIER_3 = "zhimi.airpurifier.ma4"

MODEL_AIRHUMIDIFIER_V1 = "zhimi.humidifier.v1"
MODEL_AIRHUMIDIFIER_CA1 = "zhimi.humidifier.ca1"
Expand Down Expand Up @@ -104,6 +110,7 @@
MODEL_AIRPURIFIER_SA1,
MODEL_AIRPURIFIER_SA2,
MODEL_AIRPURIFIER_2S,
MODEL_AIRPURIFIER_3,
MODEL_AIRHUMIDIFIER_V1,
MODEL_AIRHUMIDIFIER_CA1,
MODEL_AIRHUMIDIFIER_CB1,
Expand Down Expand Up @@ -131,6 +138,7 @@
ATTR_PURIFY_VOLUME = "purify_volume"
ATTR_BRIGHTNESS = "brightness"
ATTR_LEVEL = "level"
ATTR_FAN_LEVEL = "fan_level"
ATTR_MOTOR2_SPEED = "motor2_speed"
ATTR_ILLUMINANCE = "illuminance"
ATTR_FILTER_RFID_PRODUCT_ID = "filter_rfid_product_id"
Expand Down Expand Up @@ -161,6 +169,8 @@
# Air Fresh
ATTR_CO2 = "co2"

PURIFIER_MIOT = [MODEL_AIRPURIFIER_3]

# Map attributes to properties of the state object
AVAILABLE_ATTRIBUTES_AIRPURIFIER_COMMON = {
ATTR_TEMPERATURE: "temperature",
Expand Down Expand Up @@ -227,6 +237,28 @@
ATTR_ILLUMINANCE: "illuminance",
}

AVAILABLE_ATTRIBUTES_AIRPURIFIER_3 = {
ATTR_TEMPERATURE: "temperature",
ATTR_HUMIDITY: "humidity",
ATTR_AIR_QUALITY_INDEX: "aqi",
ATTR_MODE: "mode",
ATTR_FILTER_HOURS_USED: "filter_hours_used",
ATTR_FILTER_LIFE: "filter_life_remaining",
ATTR_FAVORITE_LEVEL: "favorite_level",
ATTR_CHILD_LOCK: "child_lock",
ATTR_LED: "led",
ATTR_MOTOR_SPEED: "motor_speed",
ATTR_AVERAGE_AIR_QUALITY_INDEX: "average_aqi",
ATTR_PURIFY_VOLUME: "purify_volume",
ATTR_USE_TIME: "use_time",
ATTR_BUZZER: "buzzer",
ATTR_LED_BRIGHTNESS: "led_brightness",
ATTR_FILTER_RFID_PRODUCT_ID: "filter_rfid_product_id",
ATTR_FILTER_RFID_TAG: "filter_rfid_tag",
ATTR_FILTER_TYPE: "filter_type",
ATTR_FAN_LEVEL: "fan_level",
}

AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3 = {
# Common set isn't used here. It's a very basic version of the device.
ATTR_AIR_QUALITY_INDEX: "aqi",
Expand Down Expand Up @@ -302,6 +334,7 @@
OPERATION_MODES_AIRPURIFIER_PRO = ["Auto", "Silent", "Favorite"]
OPERATION_MODES_AIRPURIFIER_PRO_V7 = OPERATION_MODES_AIRPURIFIER_PRO
OPERATION_MODES_AIRPURIFIER_2S = ["Auto", "Silent", "Favorite"]
OPERATION_MODES_AIRPURIFIER_3 = ["Auto", "Silent", "Favorite", "Fan"]
OPERATION_MODES_AIRPURIFIER_V3 = [
"Auto",
"Silent",
Expand Down Expand Up @@ -361,6 +394,13 @@
| FEATURE_SET_FAVORITE_LEVEL
)

FEATURE_FLAGS_AIRPURIFIER_3 = (
FEATURE_SET_BUZZER
| FEATURE_SET_CHILD_LOCK
| FEATURE_SET_LED
| FEATURE_SET_FAVORITE_LEVEL
)

FEATURE_FLAGS_AIRPURIFIER_V3 = (
FEATURE_SET_BUZZER | FEATURE_SET_CHILD_LOCK | FEATURE_SET_LED
)
Expand Down Expand Up @@ -472,7 +512,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
except DeviceException:
raise PlatformNotReady

if model.startswith("zhimi.airpurifier."):
if model in PURIFIER_MIOT:
air_purifier = AirPurifierMiot(host, token)
device = XiaomiAirPurifier(name, air_purifier, model, unique_id)
elif model.startswith("zhimi.airpurifier."):
air_purifier = AirPurifier(host, token)
device = XiaomiAirPurifier(name, air_purifier, model, unique_id)
elif model.startswith("zhimi.humidifier."):
Expand Down Expand Up @@ -690,6 +733,10 @@ def __init__(self, name, device, model, unique_id):
self._device_features = FEATURE_FLAGS_AIRPURIFIER_2S
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_2S
self._speed_list = OPERATION_MODES_AIRPURIFIER_2S
elif self._model == MODEL_AIRPURIFIER_3:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_3
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_3
self._speed_list = OPERATION_MODES_AIRPURIFIER_3
elif self._model == MODEL_AIRPURIFIER_V3:
self._device_features = FEATURE_FLAGS_AIRPURIFIER_V3
self._available_attributes = AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3
Expand Down Expand Up @@ -736,7 +783,10 @@ def speed_list(self) -> list:
def speed(self):
"""Return the current speed."""
if self._state:
return AirpurifierOperationMode(self._state_attrs[ATTR_MODE]).name
if isinstance(self._device, AirPurifierMiot):
return AirpurifierMiotOperationMode(self._state_attrs[ATTR_MODE]).name
else:
return AirpurifierOperationMode(self._state_attrs[ATTR_MODE]).name

return None

Expand All @@ -750,7 +800,9 @@ async def async_set_speed(self, speed: str) -> None:
await self._try_command(
"Setting operation mode of the miio device failed.",
self._device.set_mode,
AirpurifierOperationMode[speed.title()],
AirpurifierMiotOperationMode[speed.title()]
if isinstance(self._device, AirPurifierMiot)
else AirpurifierOperationMode[speed.title()],
)

async def async_set_led_on(self):
Expand Down Expand Up @@ -781,7 +833,9 @@ async def async_set_led_brightness(self, brightness: int = 2):
await self._try_command(
"Setting the led brightness of the miio device failed.",
self._device.set_led_brightness,
AirpurifierLedBrightness(brightness),
AirpurifierMiotLedBrightness(brightness)
if isinstance(self._device, AirPurifierMiot)
else AirpurifierLedBrightness(brightness),
)

async def async_set_favorite_level(self, level: int = 1):
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/xiaomi_miio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"domain": "xiaomi_miio",
"name": "Xiaomi miio",
"documentation": "https://www.home-assistant.io/integrations/xiaomi_miio",
"requirements": ["construct==2.9.45", "python-miio==0.4.8"],
"requirements": [
"construct==2.9.45",
"https://github.com/foxel/python-miio/archive/air-purifier-3h-support.zip#python-miio==0.5.0"
],
"dependencies": [],
"codeowners": ["@rytilahti", "@syssi"]
}

0 comments on commit 9cd4464

Please sign in to comment.