diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 16d56c84cb0..3e498b52a08 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -1,5 +1,6 @@ """The tests for the automation component.""" import asyncio +import logging from unittest.mock import Mock, patch import pytest @@ -152,7 +153,7 @@ async def test_two_triggers(hass, calls): assert len(calls) == 2 -async def test_trigger_service_ignoring_condition(hass, calls): +async def test_trigger_service_ignoring_condition(hass, caplog, calls): """Test triggers.""" assert await async_setup_component( hass, @@ -171,11 +172,15 @@ async def test_trigger_service_ignoring_condition(hass, calls): }, ) - with patch("homeassistant.components.automation.LOGGER.warning") as logwarn: - hass.bus.async_fire("test_event") - await hass.async_block_till_done() - assert len(calls) == 0 - assert len(logwarn.mock_calls) == 1 + caplog.clear() + caplog.set_level(logging.WARNING) + + hass.bus.async_fire("test_event") + await hass.async_block_till_done() + assert len(calls) == 0 + + assert len(caplog.record_tuples) == 1 + assert caplog.record_tuples[0][1] == logging.WARNING await hass.services.async_call( "automation", "trigger", {"entity_id": "automation.test"}, blocking=True diff --git a/tests/components/homeassistant/triggers/test_numeric_state.py b/tests/components/homeassistant/triggers/test_numeric_state.py index e58bccb0bcc..85dc68c770d 100644 --- a/tests/components/homeassistant/triggers/test_numeric_state.py +++ b/tests/components/homeassistant/triggers/test_numeric_state.py @@ -1,5 +1,6 @@ """The tests for numeric state automation.""" from datetime import timedelta +import logging from unittest.mock import patch import pytest @@ -572,7 +573,7 @@ async def test_if_not_fires_if_entity_not_match(hass, calls, below): assert len(calls) == 0 -async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, calls): +async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, caplog, calls): """Test if warns with unknown below entity.""" assert await async_setup_component( hass, @@ -589,13 +590,15 @@ async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, calls): }, ) - with patch( - "homeassistant.components.homeassistant.triggers.numeric_state._LOGGER.warning" - ) as logwarn: - hass.states.async_set("test.entity", 1) - await hass.async_block_till_done() - assert len(calls) == 0 - assert len(logwarn.mock_calls) == 1 + caplog.clear() + caplog.set_level(logging.WARNING) + + hass.states.async_set("test.entity", 1) + await hass.async_block_till_done() + assert len(calls) == 0 + + assert len(caplog.record_tuples) == 1 + assert caplog.record_tuples[0][1] == logging.WARNING @pytest.mark.parametrize("below", (10, "input_number.value_10")) @@ -1203,7 +1206,7 @@ async def test_wait_template_with_trigger(hass, calls, above): hass.states.async_set("test.entity", "8") await hass.async_block_till_done() assert len(calls) == 1 - assert "numeric_state - test.entity - 12" == calls[0].data["some"] + assert calls[0].data["some"] == "numeric_state - test.entity - 12" @pytest.mark.parametrize( diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index 5c388aa6db4..cd4039f5262 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -1,5 +1,5 @@ """Test the condition helper.""" -from logging import ERROR +from logging import ERROR, WARNING from unittest.mock import patch import pytest @@ -338,23 +338,25 @@ async def test_time_using_input_datetime(hass): assert not condition.time(hass, before="input_datetime.not_existing") -async def test_if_numeric_state_raises_on_unavailable(hass): +async def test_if_numeric_state_raises_on_unavailable(hass, caplog): """Test numeric_state raises on unavailable/unknown state.""" test = await condition.async_from_config( hass, {"condition": "numeric_state", "entity_id": "sensor.temperature", "below": 42}, ) - with patch("homeassistant.helpers.condition._LOGGER.warning") as logwarn: - hass.states.async_set("sensor.temperature", "unavailable") - with pytest.raises(ConditionError): - test(hass) - assert len(logwarn.mock_calls) == 0 + caplog.clear() + caplog.set_level(WARNING) - hass.states.async_set("sensor.temperature", "unknown") - with pytest.raises(ConditionError): - test(hass) - assert len(logwarn.mock_calls) == 0 + hass.states.async_set("sensor.temperature", "unavailable") + with pytest.raises(ConditionError): + test(hass) + assert len(caplog.record_tuples) == 0 + + hass.states.async_set("sensor.temperature", "unknown") + with pytest.raises(ConditionError): + test(hass) + assert len(caplog.record_tuples) == 0 async def test_state_multiple_entities(hass): diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 003e903de14..5cd9a9d2449 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -990,7 +990,7 @@ async def test_wait_for_trigger_generated_exception(hass, caplog): assert "something bad" in caplog.text -async def test_condition_warning(hass): +async def test_condition_warning(hass, caplog): """Test warning on condition.""" event = "test_event" events = async_capture_events(hass, event) @@ -1007,11 +1007,15 @@ async def test_condition_warning(hass): ) script_obj = script.Script(hass, sequence, "Test Name", "test_domain") + caplog.clear() + caplog.set_level(logging.WARNING) + hass.states.async_set("test.entity", "string") - with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn: - await script_obj.async_run(context=Context()) - await hass.async_block_till_done() - assert len(logwarn.mock_calls) == 1 + await script_obj.async_run(context=Context()) + await hass.async_block_till_done() + + assert len(caplog.record_tuples) == 1 + assert caplog.record_tuples[0][1] == logging.WARNING assert len(events) == 1 @@ -1127,7 +1131,7 @@ async def test_repeat_count(hass): @pytest.mark.parametrize("condition", ["while", "until"]) -async def test_repeat_condition_warning(hass, condition): +async def test_repeat_condition_warning(hass, caplog, condition): """Test warning on repeat conditions.""" event = "test_event" events = async_capture_events(hass, event) @@ -1156,10 +1160,14 @@ async def test_repeat_condition_warning(hass, condition): # wait_started = async_watch_for_action(script_obj, "wait") hass.states.async_set("sensor.test", "1") - with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn: - hass.async_create_task(script_obj.async_run(context=Context())) - await asyncio.wait_for(hass.async_block_till_done(), 1) - assert len(logwarn.mock_calls) == 1 + caplog.clear() + caplog.set_level(logging.WARNING) + + hass.async_create_task(script_obj.async_run(context=Context())) + await asyncio.wait_for(hass.async_block_till_done(), 1) + + assert len(caplog.record_tuples) == 1 + assert caplog.record_tuples[0][1] == logging.WARNING assert len(events) == count @@ -1369,7 +1377,7 @@ async def test_repeat_nested(hass, variables, first_last, inside_x): } -async def test_choose_warning(hass): +async def test_choose_warning(hass, caplog): """Test warning on choose.""" event = "test_event" events = async_capture_events(hass, event) @@ -1404,11 +1412,15 @@ async def test_choose_warning(hass): hass.states.async_set("test.entity", "9") await hass.async_block_till_done() - with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn: - await script_obj.async_run(context=Context()) - await hass.async_block_till_done() - print(logwarn.mock_calls) - assert len(logwarn.mock_calls) == 2 + caplog.clear() + caplog.set_level(logging.WARNING) + + await script_obj.async_run(context=Context()) + await hass.async_block_till_done() + + assert len(caplog.record_tuples) == 2 + assert caplog.record_tuples[0][1] == logging.WARNING + assert caplog.record_tuples[1][1] == logging.WARNING assert len(events) == 1 assert events[0].data["choice"] == "default"