From 1450fe0880fb5247115e46ba8a5184e86bd15765 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 27 Nov 2024 17:49:02 +0100 Subject: [PATCH] Improve test quality in alarm_control_panel (#130541) --- .../alarm_control_panel/__init__.py | 26 +++ .../alarm_control_panel/test_init.py | 170 ++++++------------ 2 files changed, 78 insertions(+), 118 deletions(-) diff --git a/tests/components/alarm_control_panel/__init__.py b/tests/components/alarm_control_panel/__init__.py index 1ef1161edd0..1f43c567844 100644 --- a/tests/components/alarm_control_panel/__init__.py +++ b/tests/components/alarm_control_panel/__init__.py @@ -1 +1,27 @@ """The tests for Alarm control panel platforms.""" + +from homeassistant.components.alarm_control_panel import ( + DOMAIN as ALARM_CONTROL_PANEL_DOMAIN, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + + +async def help_async_setup_entry_init( + hass: HomeAssistant, config_entry: ConfigEntry +) -> bool: + """Set up test config entry.""" + await hass.config_entries.async_forward_entry_setups( + config_entry, [ALARM_CONTROL_PANEL_DOMAIN] + ) + return True + + +async def help_async_unload_entry( + hass: HomeAssistant, config_entry: ConfigEntry +) -> bool: + """Unload test config emntry.""" + return await hass.config_entries.async_unload_platforms( + config_entry, [Platform.ALARM_CONTROL_PANEL] + ) diff --git a/tests/components/alarm_control_panel/test_init.py b/tests/components/alarm_control_panel/test_init.py index 1523a884b88..58f585b40ea 100644 --- a/tests/components/alarm_control_panel/test_init.py +++ b/tests/components/alarm_control_panel/test_init.py @@ -12,7 +12,6 @@ from homeassistant.components.alarm_control_panel import ( AlarmControlPanelEntityFeature, CodeFormat, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_CODE, SERVICE_ALARM_ARM_AWAY, @@ -26,19 +25,18 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er, frame -from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import UNDEFINED, UndefinedType -from .conftest import TEST_DOMAIN, MockAlarmControlPanel +from . import help_async_setup_entry_init, help_async_unload_entry +from .conftest import MockAlarmControlPanel from tests.common import ( MockConfigEntry, MockModule, - MockPlatform, help_test_all, import_and_test_deprecated_constant_enum, mock_integration, - mock_platform, + setup_test_component_platform, ) @@ -323,24 +321,6 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_state_prop ) -> None: """Test incorrectly using state property does log issue and raise repair.""" - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups( - config_entry, [ALARM_CONTROL_PANEL_DOMAIN] - ) - return True - - mock_integration( - hass, - MockModule( - TEST_DOMAIN, - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - class MockLegacyAlarmControlPanel(MockAlarmControlPanel): """Mocked alarm control entity.""" @@ -365,36 +345,33 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_state_prop code_format=code_format, code_arm_required=code_arm_required, ) - - async def async_setup_entry_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test alarm control panel platform via config entry.""" - async_add_entities([entity]) - - mock_platform( - hass, - f"{TEST_DOMAIN}.{ALARM_CONTROL_PANEL_DOMAIN}", - MockPlatform(async_setup_entry=async_setup_entry_platform), - ) - - config_entry = MockConfigEntry(domain=TEST_DOMAIN) + config_entry = MockConfigEntry(domain="test") config_entry.add_to_hass(hass) + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=help_async_setup_entry_init, + async_unload_entry=help_async_unload_entry, + ), + built_in=False, + ) + setup_test_component_platform( + hass, ALARM_CONTROL_PANEL_DOMAIN, [entity], from_config_entry=True + ) assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() state = hass.states.get(entity.entity_id) assert state is not None assert ( - "Detected that custom integration 'alarm_control_panel' is setting state directly." - " Entity None (.MockLegacyAlarmControlPanel'>)" - " should implement the 'alarm_state' property and return its state using the AlarmControlPanelState enum" - " at test_init.py, line 123: yield. This will stop working in Home Assistant 2025.11, please create a bug report at" - in caplog.text + "Detected that custom integration 'alarm_control_panel' is setting state" + " directly. Entity None (.MockLegacyAlarmControlPanel'>) should implement" + " the 'alarm_state' property and return its state using the AlarmControlPanelState" + " enum at test_init.py, line 123: yield. This will stop working in Home Assistant" + " 2025.11, please create a bug report at" in caplog.text ) @@ -409,23 +386,6 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state ) -> None: """Test incorrectly using _attr_state attribute does log issue and raise repair.""" - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups( - config_entry, [ALARM_CONTROL_PANEL_DOMAIN] - ) - return True - - mock_integration( - hass, - MockModule( - TEST_DOMAIN, - async_setup_entry=async_setup_entry_init, - ), - ) - class MockLegacyAlarmControlPanel(MockAlarmControlPanel): """Mocked alarm control entity.""" @@ -449,25 +409,20 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state code_format=code_format, code_arm_required=code_arm_required, ) - - async def async_setup_entry_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test alarm control panel platform via config entry.""" - async_add_entities([entity]) - - mock_platform( - hass, - f"{TEST_DOMAIN}.{ALARM_CONTROL_PANEL_DOMAIN}", - MockPlatform(async_setup_entry=async_setup_entry_platform), - ) - - config_entry = MockConfigEntry(domain=TEST_DOMAIN) + config_entry = MockConfigEntry(domain="test") config_entry.add_to_hass(hass) + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=help_async_setup_entry_init, + async_unload_entry=help_async_unload_entry, + ), + ) + setup_test_component_platform( + hass, ALARM_CONTROL_PANEL_DOMAIN, [entity], from_config_entry=True + ) assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() state = hass.states.get(entity.entity_id) assert state is not None @@ -485,10 +440,11 @@ async def test_alarm_control_panel_log_deprecated_state_warning_using_attr_state "Detected that custom integration 'alarm_control_panel' is setting state directly." " Entity alarm_control_panel.test_alarm_control_panel" " (.MockLegacyAlarmControlPanel'>)" - " should implement the 'alarm_state' property and return its state using the AlarmControlPanelState enum" - " at test_init.py, line 123: yield. This will stop working in Home Assistant 2025.11, please create a bug report at" - in caplog.text + "test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr." + ".MockLegacyAlarmControlPanel'>) should implement the 'alarm_state' property" + " and return its state using the AlarmControlPanelState enum at test_init.py, line 123:" + " yield. This will stop working in Home Assistant 2025.11," + " please create a bug report at" in caplog.text ) caplog.clear() await help_test_async_alarm_control_panel_service( @@ -512,23 +468,6 @@ async def test_alarm_control_panel_deprecated_state_does_not_break_state( ) -> None: """Test using _attr_state attribute does not break state.""" - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups( - config_entry, [ALARM_CONTROL_PANEL_DOMAIN] - ) - return True - - mock_integration( - hass, - MockModule( - TEST_DOMAIN, - async_setup_entry=async_setup_entry_init, - ), - ) - class MockLegacyAlarmControlPanel(MockAlarmControlPanel): """Mocked alarm control entity.""" @@ -553,25 +492,20 @@ async def test_alarm_control_panel_deprecated_state_does_not_break_state( code_format=code_format, code_arm_required=code_arm_required, ) - - async def async_setup_entry_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test alarm control panel platform via config entry.""" - async_add_entities([entity]) - - mock_platform( - hass, - f"{TEST_DOMAIN}.{ALARM_CONTROL_PANEL_DOMAIN}", - MockPlatform(async_setup_entry=async_setup_entry_platform), - ) - - config_entry = MockConfigEntry(domain=TEST_DOMAIN) + config_entry = MockConfigEntry(domain="test") config_entry.add_to_hass(hass) + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=help_async_setup_entry_init, + async_unload_entry=help_async_unload_entry, + ), + ) + setup_test_component_platform( + hass, ALARM_CONTROL_PANEL_DOMAIN, [entity], from_config_entry=True + ) assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() state = hass.states.get(entity.entity_id) assert state is not None