Improve test quality in alarm_control_panel (#130541)
parent
e4e9d76b45
commit
1450fe0880
|
@ -1 +1,27 @@
|
||||||
"""The tests for Alarm control panel platforms."""
|
"""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]
|
||||||
|
)
|
||||||
|
|
|
@ -12,7 +12,6 @@ from homeassistant.components.alarm_control_panel import (
|
||||||
AlarmControlPanelEntityFeature,
|
AlarmControlPanelEntityFeature,
|
||||||
CodeFormat,
|
CodeFormat,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_CODE,
|
ATTR_CODE,
|
||||||
SERVICE_ALARM_ARM_AWAY,
|
SERVICE_ALARM_ARM_AWAY,
|
||||||
|
@ -26,19 +25,18 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
from homeassistant.helpers import entity_registry as er, frame
|
from homeassistant.helpers import entity_registry as er, frame
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
||||||
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
|
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 (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
MockModule,
|
MockModule,
|
||||||
MockPlatform,
|
|
||||||
help_test_all,
|
help_test_all,
|
||||||
import_and_test_deprecated_constant_enum,
|
import_and_test_deprecated_constant_enum,
|
||||||
mock_integration,
|
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:
|
) -> None:
|
||||||
"""Test incorrectly using state property does log issue and raise repair."""
|
"""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):
|
class MockLegacyAlarmControlPanel(MockAlarmControlPanel):
|
||||||
"""Mocked alarm control entity."""
|
"""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_format=code_format,
|
||||||
code_arm_required=code_arm_required,
|
code_arm_required=code_arm_required,
|
||||||
)
|
)
|
||||||
|
config_entry = MockConfigEntry(domain="test")
|
||||||
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.add_to_hass(hass)
|
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)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity.entity_id)
|
state = hass.states.get(entity.entity_id)
|
||||||
assert state is not None
|
assert state is not None
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
"Detected that custom integration 'alarm_control_panel' is setting state directly."
|
"Detected that custom integration 'alarm_control_panel' is setting state"
|
||||||
" Entity None (<class 'tests.components.alarm_control_panel.test_init."
|
" directly. Entity None (<class 'tests.components.alarm_control_panel."
|
||||||
"test_alarm_control_panel_log_deprecated_state_warning_using_state_prop.<locals>.MockLegacyAlarmControlPanel'>)"
|
"test_init.test_alarm_control_panel_log_deprecated_state_warning_using"
|
||||||
" should implement the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
|
"_state_prop.<locals>.MockLegacyAlarmControlPanel'>) should implement"
|
||||||
" at test_init.py, line 123: yield. This will stop working in Home Assistant 2025.11, please create a bug report at"
|
" the 'alarm_state' property and return its state using the AlarmControlPanelState"
|
||||||
in caplog.text
|
" 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:
|
) -> None:
|
||||||
"""Test incorrectly using _attr_state attribute does log issue and raise repair."""
|
"""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):
|
class MockLegacyAlarmControlPanel(MockAlarmControlPanel):
|
||||||
"""Mocked alarm control entity."""
|
"""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_format=code_format,
|
||||||
code_arm_required=code_arm_required,
|
code_arm_required=code_arm_required,
|
||||||
)
|
)
|
||||||
|
config_entry = MockConfigEntry(domain="test")
|
||||||
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.add_to_hass(hass)
|
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)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity.entity_id)
|
state = hass.states.get(entity.entity_id)
|
||||||
assert state is not None
|
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."
|
"Detected that custom integration 'alarm_control_panel' is setting state directly."
|
||||||
" Entity alarm_control_panel.test_alarm_control_panel"
|
" Entity alarm_control_panel.test_alarm_control_panel"
|
||||||
" (<class 'tests.components.alarm_control_panel.test_init."
|
" (<class 'tests.components.alarm_control_panel.test_init."
|
||||||
"test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr.<locals>.MockLegacyAlarmControlPanel'>)"
|
"test_alarm_control_panel_log_deprecated_state_warning_using_attr_state_attr."
|
||||||
" should implement the 'alarm_state' property and return its state using the AlarmControlPanelState enum"
|
"<locals>.MockLegacyAlarmControlPanel'>) should implement the 'alarm_state' property"
|
||||||
" at test_init.py, line 123: yield. This will stop working in Home Assistant 2025.11, please create a bug report at"
|
" and return its state using the AlarmControlPanelState enum at test_init.py, line 123:"
|
||||||
in caplog.text
|
" yield. This will stop working in Home Assistant 2025.11,"
|
||||||
|
" please create a bug report at" in caplog.text
|
||||||
)
|
)
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
await help_test_async_alarm_control_panel_service(
|
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:
|
) -> None:
|
||||||
"""Test using _attr_state attribute does not break state."""
|
"""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):
|
class MockLegacyAlarmControlPanel(MockAlarmControlPanel):
|
||||||
"""Mocked alarm control entity."""
|
"""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_format=code_format,
|
||||||
code_arm_required=code_arm_required,
|
code_arm_required=code_arm_required,
|
||||||
)
|
)
|
||||||
|
config_entry = MockConfigEntry(domain="test")
|
||||||
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.add_to_hass(hass)
|
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)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity.entity_id)
|
state = hass.states.get(entity.entity_id)
|
||||||
assert state is not None
|
assert state is not None
|
||||||
|
|
Loading…
Reference in New Issue