1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00

Coerce int in Flo set sleep mode service (#70592)

* Coerce int in Flo set sleep mode service

* add test to ensure exception thrown for bad value
This commit is contained in:
David F. Mulcahey 2022-04-25 09:18:09 -04:00 committed by GitHub
parent 069dd567f7
commit ea9c8b15de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -51,7 +51,10 @@ async def async_setup_entry(
platform.async_register_entity_service(
SERVICE_SET_SLEEP_MODE,
{
vol.Required(ATTR_SLEEP_MINUTES, default=120): vol.In(SLEEP_MINUTE_OPTIONS),
vol.Required(ATTR_SLEEP_MINUTES, default=120): vol.All(
vol.Coerce(int),
vol.In(SLEEP_MINUTE_OPTIONS),
),
vol.Required(ATTR_REVERT_TO_MODE, default=SYSTEM_MODE_HOME): vol.In(
SYSTEM_REVERT_MODES
),

View File

@ -1,4 +1,7 @@
"""Test the services for the Flo by Moen integration."""
import pytest
from voluptuous.error import MultipleInvalid
from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.components.flo.switch import (
ATTR_REVERT_TO_MODE,
@ -67,3 +70,32 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 12
# test calling with a string value to ensure it is converted to int
await hass.services.async_call(
FLO_DOMAIN,
SERVICE_SET_SLEEP_MODE,
{
ATTR_ENTITY_ID: SWITCH_ENTITY_ID,
ATTR_REVERT_TO_MODE: SYSTEM_MODE_HOME,
ATTR_SLEEP_MINUTES: "120",
},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 13
# test calling with a non string -> int value and ensure exception is thrown
with pytest.raises(MultipleInvalid):
await hass.services.async_call(
FLO_DOMAIN,
SERVICE_SET_SLEEP_MODE,
{
ATTR_ENTITY_ID: SWITCH_ENTITY_ID,
ATTR_REVERT_TO_MODE: SYSTEM_MODE_HOME,
ATTR_SLEEP_MINUTES: "test",
},
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 13