mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Register Wemo fan services with entity service helper (#44192)
* Use a singleton for the Wemo registry and fan services * Undo changes to the wemo subscription registry * Use an entity service helper to register the Wemo fan services * Fix Wemo fan test (missing ATTR_ENTITY_ID) * Use the function name directly rather than a string * Improve test coverage of the set_humidity service
This commit is contained in:
parent
69a7eff55d
commit
2765c6f1e9
@ -14,8 +14,7 @@ from homeassistant.components.fan import (
|
||||
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 (
|
||||
@ -81,27 +80,19 @@ HASS_FAN_SPEED_TO_WEMO = {
|
||||
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)
|
||||
|
||||
@ -112,34 +103,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
]
|
||||
)
|
||||
|
||||
def service_handle(service):
|
||||
"""Handle the WeMo humidifier services."""
|
||||
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||
platform = entity_platform.current_platform.get()
|
||||
|
||||
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()
|
||||
|
||||
# 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__
|
||||
)
|
||||
|
||||
|
||||
@ -247,21 +220,21 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
||||
|
||||
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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user