From c011f3fa9518dea378d3a5630bbfca99be0cbc64 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 26 Sep 2020 13:19:59 +0200 Subject: [PATCH] Use direct service calls in light platform tests (#40604) --- tests/components/light/test_init.py | 388 ++++++++++++++++++---------- 1 file changed, 258 insertions(+), 130 deletions(-) diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index f3276313775..e723ac77e03 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -11,6 +11,7 @@ from homeassistant.components import light from homeassistant.const import ( ATTR_ENTITY_ID, CONF_PLATFORM, + ENTITY_MATCH_ALL, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON, @@ -22,7 +23,6 @@ from homeassistant.setup import async_setup_component import tests.async_mock as mock from tests.common import async_mock_service -from tests.components.light import common @pytest.fixture @@ -47,20 +47,22 @@ async def test_methods(hass): # Test turn_on turn_on_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_ON) - await common.async_turn_on( - hass, - entity_id="entity_id_val", - transition="transition_val", - brightness="brightness_val", - rgb_color="rgb_color_val", - xy_color="xy_color_val", - profile="profile_val", - color_name="color_name_val", - white_value="white_val", + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: "entity_id_val", + light.ATTR_TRANSITION: "transition_val", + light.ATTR_BRIGHTNESS: "brightness_val", + light.ATTR_RGB_COLOR: "rgb_color_val", + light.ATTR_XY_COLOR: "xy_color_val", + light.ATTR_PROFILE: "profile_val", + light.ATTR_COLOR_NAME: "color_name_val", + light.ATTR_WHITE_VALUE: "white_val", + }, + blocking=True, ) - await hass.async_block_till_done() - assert len(turn_on_calls) == 1 call = turn_on_calls[-1] @@ -78,29 +80,34 @@ async def test_methods(hass): # Test turn_off turn_off_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_OFF) - await common.async_turn_off( - hass, entity_id="entity_id_val", transition="transition_val" + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: "entity_id_val", + light.ATTR_TRANSITION: "transition_val", + }, + blocking=True, ) - await hass.async_block_till_done() - assert len(turn_off_calls) == 1 call = turn_off_calls[-1] - assert light.DOMAIN == call.domain - assert SERVICE_TURN_OFF == call.service + assert call.domain == light.DOMAIN + assert call.service == SERVICE_TURN_OFF assert call.data[ATTR_ENTITY_ID] == "entity_id_val" assert call.data[light.ATTR_TRANSITION] == "transition_val" # Test toggle toggle_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TOGGLE) - await common.async_toggle( - hass, entity_id="entity_id_val", transition="transition_val" + await hass.services.async_call( + light.DOMAIN, + SERVICE_TOGGLE, + {ATTR_ENTITY_ID: "entity_id_val", light.ATTR_TRANSITION: "transition_val"}, + blocking=True, ) - await hass.async_block_till_done() - assert len(toggle_calls) == 1 call = toggle_calls[-1] @@ -128,186 +135,287 @@ async def test_services(hass): assert not light.is_on(hass, ent3.entity_id) # Test basic turn_on, turn_off, toggle services - await common.async_turn_off(hass, entity_id=ent1.entity_id) - await common.async_turn_on(hass, entity_id=ent2.entity_id) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ent1.entity_id}, blocking=True + ) + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ent2.entity_id}, blocking=True + ) assert not light.is_on(hass, ent1.entity_id) assert light.is_on(hass, ent2.entity_id) # turn on all lights - await common.async_turn_on(hass) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, blocking=True + ) assert light.is_on(hass, ent1.entity_id) assert light.is_on(hass, ent2.entity_id) assert light.is_on(hass, ent3.entity_id) # turn off all lights - await common.async_turn_off(hass) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, + blocking=True, + ) assert not light.is_on(hass, ent1.entity_id) assert not light.is_on(hass, ent2.entity_id) assert not light.is_on(hass, ent3.entity_id) # turn off all lights by setting brightness to 0 - await common.async_turn_on(hass) - await hass.async_block_till_done() - - await common.async_turn_on(hass, brightness=0) - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, blocking=True + ) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_MATCH_ALL, light.ATTR_BRIGHTNESS: 0}, + blocking=True, + ) assert not light.is_on(hass, ent1.entity_id) assert not light.is_on(hass, ent2.entity_id) assert not light.is_on(hass, ent3.entity_id) # toggle all lights - await common.async_toggle(hass) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, blocking=True + ) assert light.is_on(hass, ent1.entity_id) assert light.is_on(hass, ent2.entity_id) assert light.is_on(hass, ent3.entity_id) # toggle all lights - await common.async_toggle(hass) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, blocking=True + ) assert not light.is_on(hass, ent1.entity_id) assert not light.is_on(hass, ent2.entity_id) assert not light.is_on(hass, ent3.entity_id) # Ensure all attributes process correctly - await common.async_turn_on( - hass, ent1.entity_id, transition=10, brightness=20, color_name="blue" + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent1.entity_id, + light.ATTR_TRANSITION: 10, + light.ATTR_BRIGHTNESS: 20, + light.ATTR_COLOR_NAME: "blue", + }, + blocking=True, ) - await common.async_turn_on( - hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255 + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent2.entity_id, + light.ATTR_RGB_COLOR: (255, 255, 255), + light.ATTR_WHITE_VALUE: 255, + }, + blocking=True, + ) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent3.entity_id, + light.ATTR_XY_COLOR: (0.4, 0.6), + }, + blocking=True, ) - await common.async_turn_on(hass, ent3.entity_id, xy_color=(0.4, 0.6)) - await hass.async_block_till_done() _, data = ent1.last_call("turn_on") - assert { + assert data == { light.ATTR_TRANSITION: 10, light.ATTR_BRIGHTNESS: 20, light.ATTR_HS_COLOR: (240, 100), - } == data + } _, data = ent2.last_call("turn_on") - assert {light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255} == data + assert data == {light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255} _, data = ent3.last_call("turn_on") - assert {light.ATTR_HS_COLOR: (71.059, 100)} == data + assert data == {light.ATTR_HS_COLOR: (71.059, 100)} # Ensure attributes are filtered when light is turned off - await common.async_turn_on( - hass, ent1.entity_id, transition=10, brightness=0, color_name="blue" + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent1.entity_id, + light.ATTR_TRANSITION: 10, + light.ATTR_BRIGHTNESS: 0, + light.ATTR_COLOR_NAME: "blue", + }, + blocking=True, ) - await common.async_turn_on( - hass, - ent2.entity_id, - brightness=0, - rgb_color=(255, 255, 255), - white_value=0, + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent2.entity_id, + light.ATTR_BRIGHTNESS: 0, + light.ATTR_RGB_COLOR: (255, 255, 255), + light.ATTR_WHITE_VALUE: 0, + }, + blocking=True, + ) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent3.entity_id, + light.ATTR_BRIGHTNESS: 0, + light.ATTR_XY_COLOR: (0.4, 0.6), + }, + blocking=True, ) - await common.async_turn_on(hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6)) - - await hass.async_block_till_done() assert not light.is_on(hass, ent1.entity_id) assert not light.is_on(hass, ent2.entity_id) assert not light.is_on(hass, ent3.entity_id) _, data = ent1.last_call("turn_off") - assert {light.ATTR_TRANSITION: 10} == data + assert data == {light.ATTR_TRANSITION: 10} _, data = ent2.last_call("turn_off") - assert {} == data + assert data == {} _, data = ent3.last_call("turn_off") - assert {} == data + assert data == {} # One of the light profiles prof_name, prof_h, prof_s, prof_bri, prof_t = "relax", 35.932, 69.412, 144, 0 # Test light profiles - await common.async_turn_on(hass, ent1.entity_id, profile=prof_name) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: prof_name}, + blocking=True, + ) # Specify a profile and a brightness attribute to overwrite it - await common.async_turn_on( - hass, ent2.entity_id, profile=prof_name, brightness=100, transition=1 + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent2.entity_id, + light.ATTR_PROFILE: prof_name, + light.ATTR_BRIGHTNESS: 100, + light.ATTR_TRANSITION: 1, + }, + blocking=True, ) - await hass.async_block_till_done() - _, data = ent1.last_call("turn_on") - assert { + assert data == { light.ATTR_BRIGHTNESS: prof_bri, light.ATTR_HS_COLOR: (prof_h, prof_s), light.ATTR_TRANSITION: prof_t, - } == data + } _, data = ent2.last_call("turn_on") - assert { + assert data == { light.ATTR_BRIGHTNESS: 100, light.ATTR_HS_COLOR: (prof_h, prof_s), light.ATTR_TRANSITION: 1, - } == data + } # Test toggle with parameters - await common.async_toggle( - hass, ent3.entity_id, profile=prof_name, brightness_pct=100 + await hass.services.async_call( + light.DOMAIN, + SERVICE_TOGGLE, + { + ATTR_ENTITY_ID: ent3.entity_id, + light.ATTR_PROFILE: prof_name, + light.ATTR_BRIGHTNESS_PCT: 100, + }, + blocking=True, ) - await hass.async_block_till_done() + _, data = ent3.last_call("turn_on") - assert { + assert data == { light.ATTR_BRIGHTNESS: 255, light.ATTR_HS_COLOR: (prof_h, prof_s), light.ATTR_TRANSITION: prof_t, - } == data + } # Test bad data - await common.async_turn_on(hass) - await common.async_turn_on(hass, ent1.entity_id, profile="nonexisting") + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_MATCH_ALL}, blocking=True + ) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: -1}, + blocking=True, + ) with pytest.raises(vol.MultipleInvalid): - await common.async_turn_on(hass, ent2.entity_id, xy_color=["bla-di-bla", 5]) + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent2.entity_id, light.ATTR_XY_COLOR: ["bla-di-bla", 5]}, + blocking=True, + ) with pytest.raises(vol.MultipleInvalid): - await common.async_turn_on(hass, ent3.entity_id, rgb_color=[255, None, 2]) - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent3.entity_id, light.ATTR_RGB_COLOR: [255, None, 2]}, + blocking=True, + ) _, data = ent1.last_call("turn_on") - assert {} == data + assert data == {} _, data = ent2.last_call("turn_on") - assert {} == data + assert data == {} _, data = ent3.last_call("turn_on") - assert {} == data + assert data == {} # faulty attributes will not trigger a service call with pytest.raises(vol.MultipleInvalid): - await common.async_turn_on( - hass, ent1.entity_id, profile=prof_name, brightness="bright" + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent1.entity_id, + light.ATTR_PROFILE: prof_name, + light.ATTR_BRIGHTNESS: "bright", + }, + blocking=True, ) with pytest.raises(vol.MultipleInvalid): - await common.async_turn_on(hass, ent1.entity_id, rgb_color="yellowish") + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent1.entity_id, + light.ATTR_RGB_COLOR: "yellowish", + }, + blocking=True, + ) with pytest.raises(vol.MultipleInvalid): - await common.async_turn_on(hass, ent2.entity_id, white_value="high") - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent2.entity_id, light.ATTR_WHITE_VALUE: "high"}, + blocking=True, + ) _, data = ent1.last_call("turn_on") - assert {} == data + assert data == {} _, data = ent2.last_call("turn_on") - assert {} == data + assert data == {} async def test_broken_light_profiles(hass, mock_storage): @@ -346,12 +454,17 @@ async def test_light_profiles(hass, mock_storage): ent1, _, _ = platform.ENTITIES - await common.async_turn_on(hass, ent1.entity_id, profile="test") - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: ent1.entity_id, + light.ATTR_PROFILE: "test", + }, + blocking=True, + ) _, data = ent1.last_call("turn_on") - assert light.is_on(hass, ent1.entity_id) assert data == { light.ATTR_HS_COLOR: (71.059, 100), @@ -359,12 +472,14 @@ async def test_light_profiles(hass, mock_storage): light.ATTR_TRANSITION: 0, } - await common.async_turn_on(hass, ent1.entity_id, profile="test_off") - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: "test_off"}, + blocking=True, + ) _, data = ent1.last_call("turn_off") - assert not light.is_on(hass, ent1.entity_id) assert data == {light.ATTR_TRANSITION: 0} @@ -388,12 +503,14 @@ async def test_light_profiles_with_transition(hass, mock_storage): ent1, _, _ = platform.ENTITIES - await common.async_turn_on(hass, ent1.entity_id, profile="test") - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: "test"}, + blocking=True, + ) _, data = ent1.last_call("turn_on") - assert light.is_on(hass, ent1.entity_id) assert data == { light.ATTR_HS_COLOR: (71.059, 100), @@ -401,12 +518,14 @@ async def test_light_profiles_with_transition(hass, mock_storage): light.ATTR_TRANSITION: 2, } - await common.async_turn_on(hass, ent1.entity_id, profile="test_off") - - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ent1.entity_id, light.ATTR_PROFILE: "test_off"}, + blocking=True, + ) _, data = ent1.last_call("turn_off") - assert not light.is_on(hass, ent1.entity_id) assert data == {light.ATTR_TRANSITION: 0} @@ -440,8 +559,10 @@ async def test_default_profiles_group(hass, mock_storage): await hass.async_block_till_done() ent, _, _ = platform.ENTITIES - await common.async_turn_on(hass, ent.entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ent.entity_id}, blocking=True + ) + _, data = ent.last_call("turn_on") assert data == { light.ATTR_HS_COLOR: (71.059, 100), @@ -483,8 +604,15 @@ async def test_default_profiles_light(hass, mock_storage): await hass.async_block_till_done() dev = next(filter(lambda x: x.entity_id == "light.ceiling_2", platform.ENTITIES)) - await common.async_turn_on(hass, dev.entity_id) - await hass.async_block_till_done() + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: dev.entity_id, + }, + blocking=True, + ) + _, data = dev.last_call("turn_on") assert data == { light.ATTR_HS_COLOR: (50.353, 100), @@ -507,8 +635,8 @@ async def test_light_context(hass, hass_admin_user): "light", "toggle", {"entity_id": state.entity_id}, - True, - core.Context(user_id=hass_admin_user.id), + blocking=True, + context=core.Context(user_id=hass_admin_user.id), ) state2 = hass.states.get("light.ceiling") @@ -534,8 +662,8 @@ async def test_light_turn_on_auth(hass, hass_admin_user): "light", "turn_on", {"entity_id": state.entity_id}, - True, - core.Context(user_id=hass_admin_user.id), + blocking=True, + context=core.Context(user_id=hass_admin_user.id), ) @@ -557,7 +685,7 @@ async def test_light_brightness_step(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_step": -10}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -567,7 +695,7 @@ async def test_light_brightness_step(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_step_pct": 10}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -592,7 +720,7 @@ async def test_light_brightness_pct_conversion(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 1}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -602,7 +730,7 @@ async def test_light_brightness_pct_conversion(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 2}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -612,7 +740,7 @@ async def test_light_brightness_pct_conversion(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 50}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -622,7 +750,7 @@ async def test_light_brightness_pct_conversion(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 99}, - True, + blocking=True, ) _, data = entity.last_call("turn_on") @@ -632,7 +760,7 @@ async def test_light_brightness_pct_conversion(hass): "light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 100}, - True, + blocking=True, ) _, data = entity.last_call("turn_on")