Improve test quality in alarm_control_panel (#130541)

pull/131702/head^2
G Johansson 2024-11-27 17:49:02 +01:00 committed by GitHub
parent e4e9d76b45
commit 1450fe0880
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 78 additions and 118 deletions

View File

@ -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]
)

View File

@ -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 (<class 'tests.components.alarm_control_panel.test_init."
"test_alarm_control_panel_log_deprecated_state_warning_using_state_prop.<locals>.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 (<class 'tests.components.alarm_control_panel."
"test_init.test_alarm_control_panel_log_deprecated_state_warning_using"
"_state_prop.<locals>.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"
" (<class 'tests.components.alarm_control_panel.test_init."
"test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr.<locals>.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."
"<locals>.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