From ea9c8b15de9dcc9105ce57abbe085176aff8096d Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Mon, 25 Apr 2022 09:18:09 -0400 Subject: [PATCH] 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 --- homeassistant/components/flo/switch.py | 5 +++- tests/components/flo/test_services.py | 32 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/flo/switch.py b/homeassistant/components/flo/switch.py index d0120e1979ff..01ab2c9259e6 100644 --- a/homeassistant/components/flo/switch.py +++ b/homeassistant/components/flo/switch.py @@ -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 ), diff --git a/tests/components/flo/test_services.py b/tests/components/flo/test_services.py index 699bd97ebed7..720d0596b223 100644 --- a/tests/components/flo/test_services.py +++ b/tests/components/flo/test_services.py @@ -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