mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Update wemo to use new fan entity model (#45582)
This commit is contained in:
parent
0ec068667f
commit
0441960ffd
@ -2,20 +2,18 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
import math
|
||||||
|
|
||||||
from pywemo.ouimeaux_device.api.service import ActionException
|
from pywemo.ouimeaux_device.api.service import ActionException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.fan import (
|
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
|
||||||
SPEED_HIGH,
|
|
||||||
SPEED_LOW,
|
|
||||||
SPEED_MEDIUM,
|
|
||||||
SPEED_OFF,
|
|
||||||
SUPPORT_SET_SPEED,
|
|
||||||
FanEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.util.percentage import (
|
||||||
|
percentage_to_ranged_value,
|
||||||
|
ranged_value_to_percentage,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN as WEMO_DOMAIN,
|
DOMAIN as WEMO_DOMAIN,
|
||||||
@ -48,37 +46,17 @@ WEMO_HUMIDITY_100 = 4
|
|||||||
|
|
||||||
WEMO_FAN_OFF = 0
|
WEMO_FAN_OFF = 0
|
||||||
WEMO_FAN_MINIMUM = 1
|
WEMO_FAN_MINIMUM = 1
|
||||||
WEMO_FAN_LOW = 2 # Not used due to limitations of the base fan implementation
|
WEMO_FAN_MEDIUM = 4
|
||||||
WEMO_FAN_MEDIUM = 3
|
|
||||||
WEMO_FAN_HIGH = 4 # Not used due to limitations of the base fan implementation
|
|
||||||
WEMO_FAN_MAXIMUM = 5
|
WEMO_FAN_MAXIMUM = 5
|
||||||
|
|
||||||
|
SPEED_RANGE = (WEMO_FAN_MINIMUM, WEMO_FAN_MAXIMUM) # off is not included
|
||||||
|
|
||||||
WEMO_WATER_EMPTY = 0
|
WEMO_WATER_EMPTY = 0
|
||||||
WEMO_WATER_LOW = 1
|
WEMO_WATER_LOW = 1
|
||||||
WEMO_WATER_GOOD = 2
|
WEMO_WATER_GOOD = 2
|
||||||
|
|
||||||
SUPPORTED_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
||||||
|
|
||||||
SUPPORTED_FEATURES = SUPPORT_SET_SPEED
|
SUPPORTED_FEATURES = SUPPORT_SET_SPEED
|
||||||
|
|
||||||
# Since the base fan object supports a set list of fan speeds,
|
|
||||||
# we have to reuse some of them when mapping to the 5 WeMo speeds
|
|
||||||
WEMO_FAN_SPEED_TO_HASS = {
|
|
||||||
WEMO_FAN_OFF: SPEED_OFF,
|
|
||||||
WEMO_FAN_MINIMUM: SPEED_LOW,
|
|
||||||
WEMO_FAN_LOW: SPEED_LOW, # Reusing SPEED_LOW
|
|
||||||
WEMO_FAN_MEDIUM: SPEED_MEDIUM,
|
|
||||||
WEMO_FAN_HIGH: SPEED_HIGH, # Reusing SPEED_HIGH
|
|
||||||
WEMO_FAN_MAXIMUM: SPEED_HIGH,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Because we reused mappings in the previous dict, we have to filter them
|
|
||||||
# back out in this dict, or else we would have duplicate keys
|
|
||||||
HASS_FAN_SPEED_TO_WEMO = {
|
|
||||||
v: k
|
|
||||||
for (k, v) in WEMO_FAN_SPEED_TO_HASS.items()
|
|
||||||
if k not in [WEMO_FAN_LOW, WEMO_FAN_HIGH]
|
|
||||||
}
|
|
||||||
|
|
||||||
SET_HUMIDITY_SCHEMA = {
|
SET_HUMIDITY_SCHEMA = {
|
||||||
vol.Required(ATTR_TARGET_HUMIDITY): vol.All(
|
vol.Required(ATTR_TARGET_HUMIDITY): vol.All(
|
||||||
@ -122,7 +100,8 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
def __init__(self, device):
|
def __init__(self, device):
|
||||||
"""Initialize the WeMo switch."""
|
"""Initialize the WeMo switch."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._fan_mode = None
|
self._fan_mode = WEMO_FAN_OFF
|
||||||
|
self._fan_mode_string = None
|
||||||
self._target_humidity = None
|
self._target_humidity = None
|
||||||
self._current_humidity = None
|
self._current_humidity = None
|
||||||
self._water_level = None
|
self._water_level = None
|
||||||
@ -141,21 +120,16 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
return {
|
return {
|
||||||
ATTR_CURRENT_HUMIDITY: self._current_humidity,
|
ATTR_CURRENT_HUMIDITY: self._current_humidity,
|
||||||
ATTR_TARGET_HUMIDITY: self._target_humidity,
|
ATTR_TARGET_HUMIDITY: self._target_humidity,
|
||||||
ATTR_FAN_MODE: self._fan_mode,
|
ATTR_FAN_MODE: self._fan_mode_string,
|
||||||
ATTR_WATER_LEVEL: self._water_level,
|
ATTR_WATER_LEVEL: self._water_level,
|
||||||
ATTR_FILTER_LIFE: self._filter_life,
|
ATTR_FILTER_LIFE: self._filter_life,
|
||||||
ATTR_FILTER_EXPIRED: self._filter_expired,
|
ATTR_FILTER_EXPIRED: self._filter_expired,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed(self) -> str:
|
def percentage(self) -> str:
|
||||||
"""Return the current speed."""
|
"""Return the current speed percentage."""
|
||||||
return WEMO_FAN_SPEED_TO_HASS.get(self._fan_mode)
|
return ranged_value_to_percentage(SPEED_RANGE, self._fan_mode)
|
||||||
|
|
||||||
@property
|
|
||||||
def speed_list(self) -> list:
|
|
||||||
"""Get the list of available speeds."""
|
|
||||||
return SUPPORTED_SPEEDS
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
@ -167,7 +141,8 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
try:
|
try:
|
||||||
self._state = self.wemo.get_state(force_update)
|
self._state = self.wemo.get_state(force_update)
|
||||||
|
|
||||||
self._fan_mode = self.wemo.fan_mode_string
|
self._fan_mode = self.wemo.fan_mode
|
||||||
|
self._fan_mode_string = self.wemo.fan_mode_string
|
||||||
self._target_humidity = self.wemo.desired_humidity_percent
|
self._target_humidity = self.wemo.desired_humidity_percent
|
||||||
self._current_humidity = self.wemo.current_humidity_percent
|
self._current_humidity = self.wemo.current_humidity_percent
|
||||||
self._water_level = self.wemo.water_level_string
|
self._water_level = self.wemo.water_level_string
|
||||||
@ -185,13 +160,6 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
self._available = False
|
self._available = False
|
||||||
self.wemo.reconnect_with_device()
|
self.wemo.reconnect_with_device()
|
||||||
|
|
||||||
#
|
|
||||||
# The fan entity model has changed to use percentages and preset_modes
|
|
||||||
# instead of speeds.
|
|
||||||
#
|
|
||||||
# Please review
|
|
||||||
# https://developers.home-assistant.io/docs/core/entity/fan/
|
|
||||||
#
|
|
||||||
def turn_on(
|
def turn_on(
|
||||||
self,
|
self,
|
||||||
speed: str = None,
|
speed: str = None,
|
||||||
@ -199,17 +167,8 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
preset_mode: str = None,
|
preset_mode: str = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Turn the switch on."""
|
"""Turn the fan on."""
|
||||||
if speed is None:
|
self.set_percentage(percentage)
|
||||||
try:
|
|
||||||
self.wemo.set_state(self._last_fan_on_mode)
|
|
||||||
except ActionException as err:
|
|
||||||
_LOGGER.warning("Error while turning on device %s (%s)", self.name, err)
|
|
||||||
self._available = False
|
|
||||||
else:
|
|
||||||
self.set_speed(speed)
|
|
||||||
|
|
||||||
self.schedule_update_ha_state()
|
|
||||||
|
|
||||||
def turn_off(self, **kwargs) -> None:
|
def turn_off(self, **kwargs) -> None:
|
||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
@ -221,10 +180,17 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
|
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def set_speed(self, speed: str) -> None:
|
def set_percentage(self, percentage: int) -> None:
|
||||||
"""Set the fan_mode of the Humidifier."""
|
"""Set the fan_mode of the Humidifier."""
|
||||||
|
if percentage is None:
|
||||||
|
named_speed = self._last_fan_on_mode
|
||||||
|
elif percentage == 0:
|
||||||
|
named_speed = WEMO_FAN_OFF
|
||||||
|
else:
|
||||||
|
named_speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.wemo.set_state(HASS_FAN_SPEED_TO_WEMO.get(speed))
|
self.wemo.set_state(named_speed)
|
||||||
except ActionException as err:
|
except ActionException as err:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Error while setting speed of device %s (%s)", self.name, err
|
"Error while setting speed of device %s (%s)", self.name, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user