Skip to content

Commit

Permalink
Detect what modes are supported by appliance
Browse files Browse the repository at this point in the history
  • Loading branch information
nbogojevic committed Dec 30, 2021
1 parent 2b6f11d commit c9b9ee2
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
76 changes: 59 additions & 17 deletions custom_components/midea_dehumidifier_lan/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
percentage_to_ordered_list_item,
)

from custom_components.midea_dehumidifier_lan import ApplianceEntity, Hub
from custom_components.midea_dehumidifier_lan import (
ApplianceEntity,
ApplianceUpdateCoordinator,
Hub,
)
from custom_components.midea_dehumidifier_lan.const import DOMAIN


Expand All @@ -25,9 +29,16 @@
MODE_MEDIUM: Final = "Medium"
MODE_TURBO: Final = "Turbo"
MODE_NONE: Final = "None"
MODE_AUTO: Final = "Auto"
MODE_LOW: Final = "Low"
MODE_HIGH: Final = "High"

PRESET_MODES: Final = [MODE_SILENT, MODE_MEDIUM, MODE_TURBO]
PRESET_SPEEDS: Final = [40, 60, 80]
PRESET_SPEEDS_7: Final = [40, 60, 80]
PRESET_SPEEDS_3: Final = [40, 80]
PRESET_SPEEDS_2: Final = [100]
PRESET_MODES_7: Final = [MODE_SILENT, MODE_MEDIUM, MODE_TURBO]
PRESET_MODES_3: Final = [MODE_LOW, MODE_HIGH]
PRESET_MODES_2: Final = [MODE_AUTO]


async def async_setup_entry(
Expand All @@ -47,6 +58,21 @@ async def async_setup_entry(
class DehumidiferFan(ApplianceEntity, FanEntity):
"""Entity for managing dehumidifer fan"""

def __init__(self, coordinator: ApplianceUpdateCoordinator) -> None:
super().__init__(coordinator)
supports = getattr(coordinator.appliance.state, "supports", {})
self.fan_capability = supports.get("fan_speed", 0)

if self.fan_capability == 3:
self._preset_modes = PRESET_MODES_3
self._preset_speeds = PRESET_SPEEDS_3
elif self.fan_capability == 2:
self._preset_modes = PRESET_MODES_2
self._preset_speeds = PRESET_SPEEDS_2
else:
self._preset_modes = PRESET_MODES_7
self._preset_speeds = PRESET_SPEEDS_7

@property
def name_suffix(self) -> str:
"""Suffix to append to entity name"""
Expand All @@ -62,52 +88,68 @@ def is_on(self) -> bool:
def percentage(self) -> Optional[int]:
"""Return the current speed percentage."""
speed = getattr(self.appliance.state, "fan_speed", 0)
return ordered_list_item_to_percentage(PRESET_SPEEDS, speed)
return ordered_list_item_to_percentage(self._preset_speeds, speed)

@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
return len(PRESET_SPEEDS)
return len(self._preset_speeds)

@property
def supported_features(self) -> int:
return SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED

@property
def preset_modes(self) -> list[str]:
return PRESET_MODES
return self._preset_modes

@property
def preset_mode(self) -> str:
speed = getattr(self.appliance.state, "fan_speed", 0)
if speed == 40:
return MODE_SILENT
if speed == 60:
return MODE_MEDIUM
if speed == 80:
return MODE_TURBO
if self.fan_capability == 2:
return MODE_AUTO
elif self.fan_capability == 3:
if speed == 40:
return MODE_LOW
if speed == 80:
return MODE_HIGH
else:
if speed == 40:
return MODE_SILENT
if speed == 60:
return MODE_MEDIUM
if speed == 80:
return MODE_TURBO
return MODE_NONE

def set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
if preset_mode == MODE_SILENT:
if preset_mode == MODE_SILENT or preset_mode == MODE_LOW:
self.apply("fan_speed", 40)
elif preset_mode == MODE_MEDIUM:
self.apply("fan_speed", 60)
elif preset_mode == MODE_TURBO:
elif preset_mode == MODE_TURBO or preset_mode == MODE_HIGH:
self.apply("fan_speed", 80)
elif preset_mode == MODE_AUTO:
self.apply("fan_speed", 101)
else:
_LOGGER.warning("Unsupported fan mode %s", preset_mode)

def set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
speed = percentage_to_ordered_list_item(PRESET_SPEEDS, percentage)
speed = percentage_to_ordered_list_item(self._preset_speeds, percentage)
self.apply("fan_speed", speed)

def turn_on(self, **kwargs) -> None:
"""Turns fan to medium speed."""
self.set_preset_mode(MODE_MEDIUM)
if self.fan_capability == 3:
self.set_preset_mode(MODE_HIGH)
elif self.fan_capability != 2:
self.set_preset_mode(MODE_MEDIUM)

def turn_off(self, **kwargs) -> None:
"""Turns fan to silent speed."""
self.set_preset_mode(MODE_SILENT)
if self.fan_capability == 3:
self.set_preset_mode(MODE_LOW)
elif self.fan_capability != 2:
self.set_preset_mode(MODE_SILENT)
44 changes: 41 additions & 3 deletions custom_components/midea_dehumidifier_lan/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from custom_components.midea_dehumidifier_lan import ApplianceEntity, Hub
from custom_components.midea_dehumidifier_lan import (
ApplianceEntity,
ApplianceUpdateCoordinator,
Hub,
)
from custom_components.midea_dehumidifier_lan.const import (
DOMAIN,
MAX_TARGET_HUMIDITY,
Expand All @@ -25,8 +29,12 @@
MODE_DRY: Final = "Dry"
MODE_SMART: Final = "Smart"
MODE_CONTINOUS: Final = "Continuous"
MODE_PURIFIER: Final = "Purifier"
MODE_ANTIMOULD: Final = "Antimould"
MODE_FAN: Final = "Fan"


AVAILABLE_MODES: Final = [MODE_SMART, MODE_SET, MODE_DRY, MODE_CONTINOUS]
_AVAILABLE_MODES: Final = [MODE_SMART, MODE_SET, MODE_DRY, MODE_CONTINOUS]


async def async_setup_entry(
Expand All @@ -45,6 +53,28 @@ async def async_setup_entry(
class DehumidifierEntity(ApplianceEntity, HumidifierEntity):
"""(de)Humidifer entity for Midea appliances """

def __init__(self, coordinator: ApplianceUpdateCoordinator) -> None:
super().__init__(coordinator)
supports = getattr(coordinator.appliance.state, "supports", {})

self.modes = [MODE_SET]
if supports.get("auto", 0):
self.modes.append(MODE_SMART)
self.modes.append(MODE_CONTINOUS)
if supports.get("dry_clothes", 0):
self.modes.append(MODE_DRY)

more_modes = supports.get("mode", "0")
if more_modes == 1:
self.modes.append(MODE_PURIFIER)
elif more_modes == 2:
self.modes.append(MODE_ANTIMOULD)
elif more_modes == 3:
self.modes.append(MODE_PURIFIER)
self.modes.append(MODE_ANTIMOULD)
elif more_modes == 4:
self.modes.append(MODE_FAN)

@property
def name_suffix(self) -> str:
"""Suffix to append to entity name"""
Expand All @@ -68,7 +98,7 @@ def supported_features(self) -> int:

@property
def available_modes(self) -> list[str]:
return AVAILABLE_MODES
return self.modes

@property
def mode(self):
Expand All @@ -81,6 +111,10 @@ def mode(self):
return MODE_SMART
if curr_mode == 4:
return MODE_DRY
if curr_mode == 6:
return MODE_PURIFIER
if curr_mode == 7:
return MODE_ANTIMOULD
_LOGGER.warning("Unknown mode %d", curr_mode)
return MODE_SET

Expand Down Expand Up @@ -112,6 +146,10 @@ def set_mode(self, mode) -> None:
curr_mode = 3
elif mode == MODE_DRY:
curr_mode = 4
elif mode == MODE_PURIFIER:
curr_mode = 6
elif mode == MODE_ANTIMOULD:
curr_mode = 7
else:
_LOGGER.warning("Unsupported dehumidifer mode %s", mode)
curr_mode = 1
Expand Down
2 changes: 1 addition & 1 deletion custom_components/midea_dehumidifier_lan/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@nbogojevic"
],
"requirements": [
"midea-beautiful-dehumidifier==0.5.3"
"midea-beautiful-dehumidifier==0.6.1"
],
"iot_class": "local_polling"
}
20 changes: 16 additions & 4 deletions custom_components/midea_dehumidifier_lan/switch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for ION mode switch"""

import logging
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand All @@ -23,12 +24,12 @@ async def async_setup_entry(
hub: Hub = hass.data[DOMAIN][config_entry.entry_id]

async_add_entities(
MideaSwitch(c, "ion_mode", "Ion Mode", "mdi:air-purifier")
MideaSwitch(c, "ion_mode", "Ion Mode", "mdi:air-purifier", capability="ion")
for c in hub.coordinators
if c.is_dehumidifier()
)
async_add_entities(
MideaSwitch(c, "pump", "Pump", "mdi:pump")
MideaSwitch(c, "pump", "Pump", "mdi:pump", capability="pump")
for c in hub.coordinators
if c.is_dehumidifier()
)
Expand All @@ -48,11 +49,22 @@ class MideaSwitch(ApplianceEntity, SwitchEntity):
"""Generic attr based switch"""

def __init__(
self, coordinator: ApplianceUpdateCoordinator, attr: str, suffix: str, icon: str
self,
coordinator: ApplianceUpdateCoordinator,
attr: str,
suffix: str,
icon: str,
capability: str = None,
) -> None:
self.attr = attr
self.suffix = " " + suffix
self.icon_name = icon
self.enabled_by_default = False
if capability:
supports = getattr(coordinator.appliance.state, "supports", {})
if supports.get(capability, 0):
self.enabled_by_default = True

super().__init__(coordinator)

@property
Expand All @@ -66,7 +78,7 @@ def icon(self):

@property
def entity_registry_enabled_default(self) -> bool:
return False
return self.enabled_by_default

@property
def is_on(self) -> bool:
Expand Down

0 comments on commit c9b9ee2

Please sign in to comment.