Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 26 additions & 53 deletions homeassistant/components/wemo/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.const import ATTR_ENTITY_ID
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from .const import (
Expand Down Expand Up @@ -81,27 +80,19 @@
if k not in [WEMO_FAN_LOW, WEMO_FAN_HIGH]
}

SET_HUMIDITY_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_TARGET_HUMIDITY): vol.All(
vol.Coerce(float), vol.Range(min=0, max=100)
),
}
)

RESET_FILTER_LIFE_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_ids})
SET_HUMIDITY_SCHEMA = {
vol.Required(ATTR_TARGET_HUMIDITY): vol.All(
vol.Coerce(float), vol.Range(min=0, max=100)
),
}


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up WeMo binary sensors."""
entities = []

async def _discovered_wemo(device):
"""Handle a discovered Wemo device."""
entity = WemoHumidifier(device)
entities.append(entity)
async_add_entities([entity])
async_add_entities([WemoHumidifier(device)])

async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.fan", _discovered_wemo)

Expand All @@ -112,34 +103,16 @@ async def _discovered_wemo(device):
]
)

def service_handle(service):
"""Handle the WeMo humidifier services."""
entity_ids = service.data.get(ATTR_ENTITY_ID)

humidifiers = [entity for entity in entities if entity.entity_id in entity_ids]

if service.service == SERVICE_SET_HUMIDITY:
target_humidity = service.data.get(ATTR_TARGET_HUMIDITY)

for humidifier in humidifiers:
humidifier.set_humidity(target_humidity)
elif service.service == SERVICE_RESET_FILTER_LIFE:
for humidifier in humidifiers:
humidifier.reset_filter_life()
platform = entity_platform.current_platform.get()

# Register service(s)
hass.services.async_register(
WEMO_DOMAIN,
SERVICE_SET_HUMIDITY,
service_handle,
schema=SET_HUMIDITY_SCHEMA,
# This will call WemoHumidifier.set_humidity(target_humidity=VALUE)
platform.async_register_entity_service(
SERVICE_SET_HUMIDITY, SET_HUMIDITY_SCHEMA, WemoHumidifier.set_humidity.__name__
)

hass.services.async_register(
WEMO_DOMAIN,
SERVICE_RESET_FILTER_LIFE,
service_handle,
schema=RESET_FILTER_LIFE_SCHEMA,
# This will call WemoHumidifier.reset_filter_life()
platform.async_register_entity_service(
SERVICE_RESET_FILTER_LIFE, {}, WemoHumidifier.reset_filter_life.__name__
)


Expand Down Expand Up @@ -247,21 +220,21 @@ def set_speed(self, speed: str) -> None:

self.schedule_update_ha_state()

def set_humidity(self, humidity: float) -> None:
def set_humidity(self, target_humidity: float) -> None:
"""Set the target humidity level for the Humidifier."""
if humidity < 50:
target_humidity = WEMO_HUMIDITY_45
elif 50 <= humidity < 55:
target_humidity = WEMO_HUMIDITY_50
elif 55 <= humidity < 60:
target_humidity = WEMO_HUMIDITY_55
elif 60 <= humidity < 100:
target_humidity = WEMO_HUMIDITY_60
elif humidity >= 100:
target_humidity = WEMO_HUMIDITY_100
if target_humidity < 50:
pywemo_humidity = WEMO_HUMIDITY_45
elif 50 <= target_humidity < 55:
pywemo_humidity = WEMO_HUMIDITY_50
elif 55 <= target_humidity < 60:
pywemo_humidity = WEMO_HUMIDITY_55
elif 60 <= target_humidity < 100:
pywemo_humidity = WEMO_HUMIDITY_60
elif target_humidity >= 100:
pywemo_humidity = WEMO_HUMIDITY_100

try:
self.wemo.set_humidity(target_humidity)
self.wemo.set_humidity(pywemo_humidity)
except ActionException as err:
_LOGGER.warning(
"Error while setting humidity of device: %s (%s)", self.name, err
Expand Down
23 changes: 18 additions & 5 deletions tests/components/wemo/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,34 @@ async def test_fan_reset_filter_service(hass, pywemo_device, wemo_entity):
assert await hass.services.async_call(
DOMAIN,
fan.SERVICE_RESET_FILTER_LIFE,
{fan.ATTR_ENTITY_ID: wemo_entity.entity_id},
{ATTR_ENTITY_ID: wemo_entity.entity_id},
blocking=True,
)
pywemo_device.reset_filter_life.assert_called_with()


async def test_fan_set_humidity_service(hass, pywemo_device, wemo_entity):
@pytest.mark.parametrize(
"test_input,expected",
[
(0, fan.WEMO_HUMIDITY_45),
(45, fan.WEMO_HUMIDITY_45),
(50, fan.WEMO_HUMIDITY_50),
(55, fan.WEMO_HUMIDITY_55),
(60, fan.WEMO_HUMIDITY_60),
(100, fan.WEMO_HUMIDITY_100),
],
)
async def test_fan_set_humidity_service(
hass, pywemo_device, wemo_entity, test_input, expected
):
"""Verify that SERVICE_SET_HUMIDITY is registered and works."""
assert await hass.services.async_call(
DOMAIN,
fan.SERVICE_SET_HUMIDITY,
{
fan.ATTR_ENTITY_ID: wemo_entity.entity_id,
fan.ATTR_TARGET_HUMIDITY: "50",
ATTR_ENTITY_ID: wemo_entity.entity_id,
fan.ATTR_TARGET_HUMIDITY: test_input,
},
blocking=True,
)
pywemo_device.set_humidity.assert_called_with(fan.WEMO_HUMIDITY_50)
pywemo_device.set_humidity.assert_called_with(expected)