From f51e1fcb6700afe6ac7466973dde607d8e566927 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 1 Nov 2021 06:33:09 -0700 Subject: [PATCH] Check for uncaught service not found exceptions (#58010) --- tests/components/alert/test_init.py | 7 --- tests/components/blueprint/conftest.py | 2 +- tests/components/config/test_automation.py | 59 +++++++++++++--------- tests/components/config/test_customize.py | 8 +++ tests/components/config/test_scene.py | 39 ++++++++------ tests/components/config/test_script.py | 10 ++++ tests/components/homekit/test_type_fans.py | 5 ++ tests/conftest.py | 3 -- 8 files changed, 84 insertions(+), 49 deletions(-) diff --git a/tests/components/alert/test_init.py b/tests/components/alert/test_init.py index 199be9845ca..ef21b463a12 100644 --- a/tests/components/alert/test_init.py +++ b/tests/components/alert/test_init.py @@ -310,13 +310,6 @@ async def test_skipfirst(hass): assert len(events) == 0 -async def test_noack(hass): - """Test no ack feature.""" - entity = alert.Alert(hass, *TEST_NOACK) - hass.async_add_job(entity.begin_alerting) - await hass.async_block_till_done() - - async def test_done_message_state_tracker_reset_on_cancel(hass): """Test that the done message is reset when canceled.""" entity = alert.Alert(hass, *TEST_NOACK) diff --git a/tests/components/blueprint/conftest.py b/tests/components/blueprint/conftest.py index ec76451065c..fe0df5d8260 100644 --- a/tests/components/blueprint/conftest.py +++ b/tests/components/blueprint/conftest.py @@ -7,7 +7,7 @@ import pytest @pytest.fixture(autouse=True) def stub_blueprint_populate(): - """Stub copying the blueprint automations to the config folder.""" + """Stub copying the blueprints to the config folder.""" with patch( "homeassistant.components.blueprint.models.DomainBlueprints.async_populate" ): diff --git a/tests/components/config/test_automation.py b/tests/components/config/test_automation.py index 0950e3d0358..80ee38350aa 100644 --- a/tests/components/config/test_automation.py +++ b/tests/components/config/test_automation.py @@ -3,6 +3,8 @@ from http import HTTPStatus import json from unittest.mock import patch +import pytest + from homeassistant.bootstrap import async_setup_component from homeassistant.components import config from homeassistant.helpers import entity_registry as er @@ -10,7 +12,18 @@ from homeassistant.helpers import entity_registry as er from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 -async def test_get_device_config(hass, hass_client): +@pytest.fixture +async def setup_automation( + hass, automation_config, stub_blueprint_populate # noqa: F811 +): + """Set up automation integration.""" + assert await async_setup_component( + hass, "automation", {"automation": automation_config} + ) + + +@pytest.mark.parametrize("automation_config", ({},)) +async def test_get_device_config(hass, hass_client, setup_automation): """Test getting device config.""" with patch.object(config, "SECTIONS", ["automation"]): await async_setup_component(hass, "config", {}) @@ -30,7 +43,8 @@ async def test_get_device_config(hass, hass_client): assert result == {"id": "moon"} -async def test_update_device_config(hass, hass_client): +@pytest.mark.parametrize("automation_config", ({},)) +async def test_update_device_config(hass, hass_client, setup_automation): """Test updating device config.""" with patch.object(config, "SECTIONS", ["automation"]): await async_setup_component(hass, "config", {}) @@ -66,7 +80,8 @@ async def test_update_device_config(hass, hass_client): assert written[0] == orig_data -async def test_bad_formatted_automations(hass, hass_client): +@pytest.mark.parametrize("automation_config", ({},)) +async def test_bad_formatted_automations(hass, hass_client, setup_automation): """Test that we handle automations without ID.""" with patch.object(config, "SECTIONS", ["automation"]): await async_setup_component(hass, "config", {}) @@ -110,29 +125,27 @@ async def test_bad_formatted_automations(hass, hass_client): assert orig_data[1] == {"id": "moon", "trigger": [], "condition": [], "action": []} -async def test_delete_automation(hass, hass_client): +@pytest.mark.parametrize( + "automation_config", + ( + [ + { + "id": "sun", + "trigger": {"platform": "event", "event_type": "test_event"}, + "action": {"service": "test.automation"}, + }, + { + "id": "moon", + "trigger": {"platform": "event", "event_type": "test_event"}, + "action": {"service": "test.automation"}, + }, + ], + ), +) +async def test_delete_automation(hass, hass_client, setup_automation): """Test deleting an automation.""" ent_reg = er.async_get(hass) - assert await async_setup_component( - hass, - "automation", - { - "automation": [ - { - "id": "sun", - "trigger": {"platform": "event", "event_type": "test_event"}, - "action": {"service": "test.automation"}, - }, - { - "id": "moon", - "trigger": {"platform": "event", "event_type": "test_event"}, - "action": {"service": "test.automation"}, - }, - ] - }, - ) - assert len(ent_reg.entities) == 2 with patch.object(config, "SECTIONS", ["automation"]): diff --git a/tests/components/config/test_customize.py b/tests/components/config/test_customize.py index d5b4c788bcf..9ea18ff2ae0 100644 --- a/tests/components/config/test_customize.py +++ b/tests/components/config/test_customize.py @@ -3,11 +3,19 @@ from http import HTTPStatus import json from unittest.mock import patch +import pytest + from homeassistant.bootstrap import async_setup_component from homeassistant.components import config from homeassistant.config import DATA_CUSTOMIZE +@pytest.fixture(autouse=True) +async def setup_homeassistant(hass): + """Set up homeassistant integration.""" + assert await async_setup_component(hass, "homeassistant", {}) + + async def test_get_entity(hass, hass_client): """Test getting entity.""" with patch.object(config, "SECTIONS", ["customize"]): diff --git a/tests/components/config/test_scene.py b/tests/components/config/test_scene.py index db938638d01..69f75cc5895 100644 --- a/tests/components/config/test_scene.py +++ b/tests/components/config/test_scene.py @@ -3,13 +3,22 @@ from http import HTTPStatus import json from unittest.mock import patch +import pytest + from homeassistant.bootstrap import async_setup_component from homeassistant.components import config from homeassistant.helpers import entity_registry as er from homeassistant.util.yaml import dump -async def test_create_scene(hass, hass_client): +@pytest.fixture +async def setup_scene(hass, scene_config): + """Set up scene integration.""" + assert await async_setup_component(hass, "scene", {"scene": scene_config}) + + +@pytest.mark.parametrize("scene_config", ({},)) +async def test_create_scene(hass, hass_client, setup_scene): """Test creating a scene.""" with patch.object(config, "SECTIONS", ["scene"]): await async_setup_component(hass, "config", {}) @@ -58,7 +67,8 @@ async def test_create_scene(hass, hass_client): ) -async def test_update_scene(hass, hass_client): +@pytest.mark.parametrize("scene_config", ({},)) +async def test_update_scene(hass, hass_client, setup_scene): """Test updating a scene.""" with patch.object(config, "SECTIONS", ["scene"]): await async_setup_component(hass, "config", {}) @@ -110,7 +120,8 @@ async def test_update_scene(hass, hass_client): ) -async def test_bad_formatted_scene(hass, hass_client): +@pytest.mark.parametrize("scene_config", ({},)) +async def test_bad_formatted_scene(hass, hass_client, setup_scene): """Test that we handle scene without ID.""" with patch.object(config, "SECTIONS", ["scene"]): await async_setup_component(hass, "config", {}) @@ -163,21 +174,19 @@ async def test_bad_formatted_scene(hass, hass_client): } -async def test_delete_scene(hass, hass_client): +@pytest.mark.parametrize( + "scene_config", + ( + [ + {"id": "light_on", "name": "Light on", "entities": {}}, + {"id": "light_off", "name": "Light off", "entities": {}}, + ], + ), +) +async def test_delete_scene(hass, hass_client, setup_scene): """Test deleting a scene.""" ent_reg = er.async_get(hass) - assert await async_setup_component( - hass, - "scene", - { - "scene": [ - {"id": "light_on", "name": "Light on", "entities": {}}, - {"id": "light_off", "name": "Light off", "entities": {}}, - ] - }, - ) - assert len(ent_reg.entities) == 2 with patch.object(config, "SECTIONS", ["scene"]): diff --git a/tests/components/config/test_script.py b/tests/components/config/test_script.py index 18ab87b8c40..dca9a8aa8a7 100644 --- a/tests/components/config/test_script.py +++ b/tests/components/config/test_script.py @@ -2,9 +2,19 @@ from http import HTTPStatus from unittest.mock import patch +import pytest + from homeassistant.bootstrap import async_setup_component from homeassistant.components import config +from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 + + +@pytest.fixture(autouse=True) +async def setup_script(hass, stub_blueprint_populate): # noqa: F811 + """Set up script integration.""" + assert await async_setup_component(hass, "script", {}) + async def test_delete_script(hass, hass_client): """Test deleting a script.""" diff --git a/tests/components/homekit/test_type_fans.py b/tests/components/homekit/test_type_fans.py index 85d00dcb287..646d1baad63 100644 --- a/tests/components/homekit/test_type_fans.py +++ b/tests/components/homekit/test_type_fans.py @@ -312,6 +312,8 @@ async def test_fan_speed(hass, hk_driver, events): assert acc.char_speed.value == 50 assert acc.char_active.value == 0 + call_turn_on = async_mock_service(hass, DOMAIN, "turn_on") + hk_driver.set_characteristics( { HAP_REPR_CHARS: [ @@ -328,6 +330,9 @@ async def test_fan_speed(hass, hk_driver, events): assert acc.char_speed.value == 50 assert acc.char_active.value == 1 + assert call_turn_on[0] + assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id + async def test_fan_set_all_one_shot(hass, hk_driver, events): """Test fan with speed.""" diff --git a/tests/conftest.py b/tests/conftest.py index 845145c2ec2..80eb75ef2bd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,7 +26,6 @@ from homeassistant.components.websocket_api.auth import ( ) from homeassistant.components.websocket_api.http import URL from homeassistant.const import ATTR_NOW, EVENT_TIME_CHANGED -from homeassistant.exceptions import ServiceNotFound from homeassistant.helpers import config_entry_oauth2_flow, event from homeassistant.setup import async_setup_component from homeassistant.util import location @@ -232,8 +231,6 @@ def hass(loop, load_registries, hass_storage, request): request.function.__name__, ) in IGNORE_UNCAUGHT_EXCEPTIONS: continue - if isinstance(ex, ServiceNotFound): - continue raise ex