2024-03-27 13:36:31 +00:00
|
|
|
"""Fixturs for Alarm Control Panel tests."""
|
|
|
|
|
2024-05-29 08:46:53 +00:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2024-03-27 13:36:31 +00:00
|
|
|
import pytest
|
2024-06-06 15:24:22 +00:00
|
|
|
from typing_extensions import Generator
|
2024-03-27 13:36:31 +00:00
|
|
|
|
2024-05-29 08:46:53 +00:00
|
|
|
from homeassistant.components.alarm_control_panel import (
|
|
|
|
DOMAIN as ALARM_CONTROL_PANEL_DOMAIN,
|
|
|
|
AlarmControlPanelEntity,
|
|
|
|
AlarmControlPanelEntityFeature,
|
|
|
|
)
|
|
|
|
from homeassistant.components.alarm_control_panel.const import CodeFormat
|
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .common import MockAlarm
|
|
|
|
|
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
MockModule,
|
|
|
|
MockPlatform,
|
|
|
|
mock_config_flow,
|
|
|
|
mock_integration,
|
|
|
|
mock_platform,
|
|
|
|
)
|
|
|
|
|
|
|
|
TEST_DOMAIN = "test"
|
2024-03-27 13:36:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_alarm_control_panel_entities() -> dict[str, MockAlarm]:
|
|
|
|
"""Mock Alarm control panel class."""
|
|
|
|
return {
|
|
|
|
"arm_code": MockAlarm(
|
|
|
|
name="Alarm arm code",
|
|
|
|
code_arm_required=True,
|
|
|
|
unique_id="unique_arm_code",
|
|
|
|
),
|
|
|
|
"no_arm_code": MockAlarm(
|
|
|
|
name="Alarm no arm code",
|
|
|
|
code_arm_required=False,
|
|
|
|
unique_id="unique_no_arm_code",
|
|
|
|
),
|
|
|
|
}
|
2024-05-29 08:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockAlarmControlPanel(AlarmControlPanelEntity):
|
|
|
|
"""Mocked alarm control entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
supported_features: AlarmControlPanelEntityFeature = AlarmControlPanelEntityFeature(
|
|
|
|
0
|
|
|
|
),
|
|
|
|
code_format: CodeFormat | None = None,
|
|
|
|
code_arm_required: bool = True,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the alarm control."""
|
|
|
|
self.calls_disarm = MagicMock()
|
|
|
|
self.calls_arm_home = MagicMock()
|
|
|
|
self.calls_arm_away = MagicMock()
|
|
|
|
self.calls_arm_night = MagicMock()
|
|
|
|
self.calls_arm_vacation = MagicMock()
|
|
|
|
self.calls_trigger = MagicMock()
|
|
|
|
self.calls_arm_custom = MagicMock()
|
|
|
|
self._attr_code_format = code_format
|
|
|
|
self._attr_supported_features = supported_features
|
|
|
|
self._attr_code_arm_required = code_arm_required
|
|
|
|
self._attr_has_entity_name = True
|
|
|
|
self._attr_name = "test_alarm_control_panel"
|
|
|
|
self._attr_unique_id = "very_unique_alarm_control_panel_id"
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def alarm_disarm(self, code: str | None = None) -> None:
|
|
|
|
"""Mock alarm disarm calls."""
|
|
|
|
self.calls_disarm(code)
|
|
|
|
|
|
|
|
def alarm_arm_home(self, code: str | None = None) -> None:
|
|
|
|
"""Mock arm home calls."""
|
|
|
|
self.calls_arm_home(code)
|
|
|
|
|
|
|
|
def alarm_arm_away(self, code: str | None = None) -> None:
|
|
|
|
"""Mock arm away calls."""
|
|
|
|
self.calls_arm_away(code)
|
|
|
|
|
|
|
|
def alarm_arm_night(self, code: str | None = None) -> None:
|
|
|
|
"""Mock arm night calls."""
|
|
|
|
self.calls_arm_night(code)
|
|
|
|
|
|
|
|
def alarm_arm_vacation(self, code: str | None = None) -> None:
|
|
|
|
"""Mock arm vacation calls."""
|
|
|
|
self.calls_arm_vacation(code)
|
|
|
|
|
|
|
|
def alarm_trigger(self, code: str | None = None) -> None:
|
|
|
|
"""Mock trigger calls."""
|
|
|
|
self.calls_trigger(code)
|
|
|
|
|
|
|
|
def alarm_arm_custom_bypass(self, code: str | None = None) -> None:
|
|
|
|
"""Mock arm custom bypass calls."""
|
|
|
|
self.calls_arm_custom(code)
|
|
|
|
|
|
|
|
|
|
|
|
class MockFlow(ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-06 15:24:22 +00:00
|
|
|
def config_flow_fixture(hass: HomeAssistant) -> Generator[None]:
|
2024-05-29 08:46:53 +00:00
|
|
|
"""Mock config flow."""
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
|
|
|
|
with mock_config_flow(TEST_DOMAIN, MockFlow):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def code_format() -> CodeFormat | None:
|
|
|
|
"""Return the code format for the test alarm control panel entity."""
|
|
|
|
return CodeFormat.NUMBER
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def code_arm_required() -> bool:
|
|
|
|
"""Return if code required for arming."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="supported_features")
|
|
|
|
async def lock_supported_features() -> AlarmControlPanelEntityFeature:
|
|
|
|
"""Return the supported features for the test alarm control panel entity."""
|
|
|
|
return (
|
|
|
|
AlarmControlPanelEntityFeature.ARM_AWAY
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_HOME
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_NIGHT
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_VACATION
|
|
|
|
| AlarmControlPanelEntityFeature.TRIGGER
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="mock_alarm_control_panel_entity")
|
|
|
|
async def setup_lock_platform_test_entity(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
code_format: CodeFormat | None,
|
|
|
|
supported_features: AlarmControlPanelEntityFeature,
|
|
|
|
code_arm_required: bool,
|
|
|
|
) -> MagicMock:
|
|
|
|
"""Set up alarm control panel entity using an entity platform."""
|
|
|
|
|
|
|
|
async def async_setup_entry_init(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Set up test config entry."""
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(
|
|
|
|
config_entry, [ALARM_CONTROL_PANEL_DOMAIN]
|
2024-05-29 08:46:53 +00:00
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
MockPlatform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
TEST_DOMAIN,
|
|
|
|
async_setup_entry=async_setup_entry_init,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Unnamed sensor without device class -> no name
|
|
|
|
entity = MockAlarmControlPanel(
|
|
|
|
supported_features=supported_features,
|
|
|
|
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.add_to_hass(hass)
|
|
|
|
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
|
|
|
|
|
|
|
|
return entity
|