mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +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,
|
SUPPORT_SET_SPEED,
|
||||||
FanEntity,
|
FanEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.helpers import entity_platform
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -81,27 +80,19 @@ HASS_FAN_SPEED_TO_WEMO = {
|
|||||||
if k not in [WEMO_FAN_LOW, WEMO_FAN_HIGH]
|
if k not in [WEMO_FAN_LOW, WEMO_FAN_HIGH]
|
||||||
}
|
}
|
||||||
|
|
||||||
SET_HUMIDITY_SCHEMA = vol.Schema(
|
SET_HUMIDITY_SCHEMA = {
|
||||||
{
|
vol.Required(ATTR_TARGET_HUMIDITY): vol.All(
|
||||||
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
vol.Coerce(float), vol.Range(min=0, max=100)
|
||||||
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})
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up WeMo binary sensors."""
|
"""Set up WeMo binary sensors."""
|
||||||
entities = []
|
|
||||||
|
|
||||||
async def _discovered_wemo(device):
|
async def _discovered_wemo(device):
|
||||||
"""Handle a discovered Wemo device."""
|
"""Handle a discovered Wemo device."""
|
||||||
entity = WemoHumidifier(device)
|
async_add_entities([WemoHumidifier(device)])
|
||||||
entities.append(entity)
|
|
||||||
async_add_entities([entity])
|
|
||||||
|
|
||||||
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.fan", _discovered_wemo)
|
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):
|
platform = entity_platform.current_platform.get()
|
||||||
"""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]
|
# This will call WemoHumidifier.set_humidity(target_humidity=VALUE)
|
||||||
|
platform.async_register_entity_service(
|
||||||
if service.service == SERVICE_SET_HUMIDITY:
|
SERVICE_SET_HUMIDITY, SET_HUMIDITY_SCHEMA, WemoHumidifier.set_humidity.__name__
|
||||||
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,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.services.async_register(
|
# This will call WemoHumidifier.reset_filter_life()
|
||||||
WEMO_DOMAIN,
|
platform.async_register_entity_service(
|
||||||
SERVICE_RESET_FILTER_LIFE,
|
SERVICE_RESET_FILTER_LIFE, {}, WemoHumidifier.reset_filter_life.__name__
|
||||||
service_handle,
|
|
||||||
schema=RESET_FILTER_LIFE_SCHEMA,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -247,21 +220,21 @@ class WemoHumidifier(WemoSubscriptionEntity, FanEntity):
|
|||||||
|
|
||||||
self.schedule_update_ha_state()
|
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."""
|
"""Set the target humidity level for the Humidifier."""
|
||||||
if humidity < 50:
|
if target_humidity < 50:
|
||||||
target_humidity = WEMO_HUMIDITY_45
|
pywemo_humidity = WEMO_HUMIDITY_45
|
||||||
elif 50 <= humidity < 55:
|
elif 50 <= target_humidity < 55:
|
||||||
target_humidity = WEMO_HUMIDITY_50
|
pywemo_humidity = WEMO_HUMIDITY_50
|
||||||
elif 55 <= humidity < 60:
|
elif 55 <= target_humidity < 60:
|
||||||
target_humidity = WEMO_HUMIDITY_55
|
pywemo_humidity = WEMO_HUMIDITY_55
|
||||||
elif 60 <= humidity < 100:
|
elif 60 <= target_humidity < 100:
|
||||||
target_humidity = WEMO_HUMIDITY_60
|
pywemo_humidity = WEMO_HUMIDITY_60
|
||||||
elif humidity >= 100:
|
elif target_humidity >= 100:
|
||||||
target_humidity = WEMO_HUMIDITY_100
|
pywemo_humidity = WEMO_HUMIDITY_100
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.wemo.set_humidity(target_humidity)
|
self.wemo.set_humidity(pywemo_humidity)
|
||||||
except ActionException as err:
|
except ActionException as err:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Error while setting humidity of device: %s (%s)", self.name, err
|
"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(
|
assert await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
fan.SERVICE_RESET_FILTER_LIFE,
|
fan.SERVICE_RESET_FILTER_LIFE,
|
||||||
{fan.ATTR_ENTITY_ID: wemo_entity.entity_id},
|
{ATTR_ENTITY_ID: wemo_entity.entity_id},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
pywemo_device.reset_filter_life.assert_called_with()
|
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."""
|
"""Verify that SERVICE_SET_HUMIDITY is registered and works."""
|
||||||
assert await hass.services.async_call(
|
assert await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
fan.SERVICE_SET_HUMIDITY,
|
fan.SERVICE_SET_HUMIDITY,
|
||||||
{
|
{
|
||||||
fan.ATTR_ENTITY_ID: wemo_entity.entity_id,
|
ATTR_ENTITY_ID: wemo_entity.entity_id,
|
||||||
fan.ATTR_TARGET_HUMIDITY: "50",
|
fan.ATTR_TARGET_HUMIDITY: test_input,
|
||||||
},
|
},
|
||||||
blocking=True,
|
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