hass-core/tests/components/deako/test_init.py
Blake Bryant c049129147
Add Deako integration (#121132)
* Deako integration using pydeako

* fix: address feedback

- make unit tests more e2e
- use runtime_data to store connection

* fix: address feedback part 2

- added better type safety for Deako config entries
- refactored the config flow tests to use a conftest mock instead of directly patching
- removed pytest.mark.asyncio test decorators

* fix: address feedback pt 3

- simplify config entry type
- add test for single_instance_allowed
- remove light.py get_state(), only used once, no need to be separate function

* fix: ruff format

* Update homeassistant/components/deako/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-28 19:16:05 +02:00

87 lines
3 KiB
Python

"""Tests for the deako component init."""
from unittest.mock import MagicMock
from pydeako.deako import DeviceListTimeout, FindDevicesTimeout
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_deako_async_setup_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
pydeako_discoverer_mock: MagicMock,
) -> None:
"""Test successful setup entry."""
pydeako_deako_mock.return_value.get_devices.return_value = {
"id1": {},
"id2": {},
}
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
pydeako_deako_mock.assert_called_once_with(
pydeako_discoverer_mock.return_value.get_address
)
pydeako_deako_mock.return_value.connect.assert_called_once()
pydeako_deako_mock.return_value.find_devices.assert_called_once()
pydeako_deako_mock.return_value.get_devices.assert_called()
assert mock_config_entry.runtime_data == pydeako_deako_mock.return_value
async def test_deako_async_setup_entry_device_list_timeout(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
pydeako_discoverer_mock: MagicMock,
) -> None:
"""Test async_setup_entry raises ConfigEntryNotReady when pydeako raises DeviceListTimeout."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.find_devices.side_effect = DeviceListTimeout()
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
pydeako_deako_mock.assert_called_once_with(
pydeako_discoverer_mock.return_value.get_address
)
pydeako_deako_mock.return_value.connect.assert_called_once()
pydeako_deako_mock.return_value.find_devices.assert_called_once()
pydeako_deako_mock.return_value.disconnect.assert_called_once()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_deako_async_setup_entry_find_devices_timeout(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
pydeako_discoverer_mock: MagicMock,
) -> None:
"""Test async_setup_entry raises ConfigEntryNotReady when pydeako raises FindDevicesTimeout."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.find_devices.side_effect = FindDevicesTimeout()
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
pydeako_deako_mock.assert_called_once_with(
pydeako_discoverer_mock.return_value.get_address
)
pydeako_deako_mock.return_value.connect.assert_called_once()
pydeako_deako_mock.return_value.find_devices.assert_called_once()
pydeako_deako_mock.return_value.disconnect.assert_called_once()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY