1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-01 19:57:08 +01:00
Files
core/tests/components/ialarm/test_init.py
T
jasonjhofmann 36b74d6f05 Add network MAC connection to iAlarm device (#173676)
Co-authored-by: jasonjhofmann <16144532+jasonjhofmann@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 08:02:49 +02:00

90 lines
3.1 KiB
Python

"""Test the Antifurto365 iAlarm init."""
from unittest.mock import MagicMock, Mock, patch
from uuid import uuid4
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.ialarm.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from tests.common import MockConfigEntry
@pytest.fixture(name="ialarm_api")
def ialarm_api_fixture():
"""Set up IAlarm API fixture."""
with patch("homeassistant.components.ialarm.IAlarm") as mock_ialarm_api:
yield mock_ialarm_api
@pytest.fixture(name="mock_config_entry")
def mock_config_fixture():
"""Return a fake config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.10.20", CONF_PORT: 18034},
entry_id=str(uuid4()),
)
async def test_setup_entry(hass: HomeAssistant, ialarm_api, mock_config_entry) -> None:
"""Test setup entry."""
ialarm_api.return_value.get_mac = Mock(return_value="00:00:54:12:34:56")
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
ialarm_api.return_value.get_mac.assert_called_once()
assert mock_config_entry.state is ConfigEntryState.LOADED
async def test_setup_not_ready(
hass: HomeAssistant, ialarm_api, mock_config_entry
) -> None:
"""Test setup failed because we can't connect to the alarm system."""
ialarm_api.return_value.get_mac = Mock(side_effect=ConnectionError)
mock_config_entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_device_registry(
hass: HomeAssistant,
ialarm_api: MagicMock,
mock_config_entry: MockConfigEntry,
device_registry: dr.DeviceRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test the device registry entry, including the network MAC connection."""
ialarm_api.return_value.get_mac = Mock(return_value="00:00:54:12:34:56")
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, "00:00:54:12:34:56")}
)
assert device_entry == snapshot
async def test_unload_entry(hass: HomeAssistant, ialarm_api, mock_config_entry) -> None:
"""Test being able to unload an entry."""
ialarm_api.return_value.get_mac = Mock(return_value="00:00:54:12:34:56")
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED