2019-11-06 22:55:39 +00:00
|
|
|
"""Tests for the WLED integration."""
|
2021-06-11 18:55:08 +00:00
|
|
|
import asyncio
|
2022-01-12 07:04:17 +00:00
|
|
|
from collections.abc import Callable
|
2021-06-11 09:36:54 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2021-06-11 18:55:08 +00:00
|
|
|
import pytest
|
2020-06-03 00:29:49 +00:00
|
|
|
from wled import WLEDConnectionError
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
from homeassistant.components.wled.const import DOMAIN
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2019-11-06 22:55:39 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
2021-06-11 18:55:08 +00:00
|
|
|
@pytest.mark.parametrize("mock_wled", ["wled/rgb_websocket.json"], indirect=True)
|
2021-06-11 09:36:54 +00:00
|
|
|
async def test_load_unload_config_entry(
|
|
|
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_wled: AsyncMock
|
|
|
|
) -> None:
|
|
|
|
"""Test the WLED configuration entry unloading."""
|
2021-06-11 18:55:08 +00:00
|
|
|
connection_connected = asyncio.Future()
|
|
|
|
connection_finished = asyncio.Future()
|
|
|
|
|
|
|
|
async def connect(callback: Callable):
|
|
|
|
connection_connected.set_result(None)
|
|
|
|
await connection_finished
|
|
|
|
|
|
|
|
# Mock out wled.listen with a Future
|
|
|
|
mock_wled.listen.side_effect = connect
|
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-11 18:55:08 +00:00
|
|
|
await connection_connected
|
2021-06-11 09:36:54 +00:00
|
|
|
|
2021-06-11 18:55:08 +00:00
|
|
|
# Ensure config entry is loaded and are connected
|
2021-06-11 09:36:54 +00:00
|
|
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
2021-06-11 18:55:08 +00:00
|
|
|
assert mock_wled.connect.call_count == 1
|
|
|
|
assert mock_wled.disconnect.call_count == 0
|
2021-06-11 09:36:54 +00:00
|
|
|
|
|
|
|
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-06-11 18:55:08 +00:00
|
|
|
# Ensure everything is cleaned up nicely and are disconnected
|
|
|
|
assert mock_wled.disconnect.call_count == 1
|
2021-06-11 09:36:54 +00:00
|
|
|
assert not hass.data.get(DOMAIN)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
2021-06-09 18:15:46 +00:00
|
|
|
@patch(
|
2021-06-11 09:36:54 +00:00
|
|
|
"homeassistant.components.wled.coordinator.WLED.request",
|
2021-06-09 18:15:46 +00:00
|
|
|
side_effect=WLEDConnectionError,
|
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
async def test_config_entry_not_ready(
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_request: MagicMock, hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
2019-11-06 22:55:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the WLED configuration entry not ready."""
|
2021-06-11 09:36:54 +00:00
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
2019-11-06 22:55:39 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
assert mock_request.call_count == 1
|
|
|
|
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2020-01-04 21:48:31 +00:00
|
|
|
|
2021-06-11 09:36:54 +00:00
|
|
|
async def test_setting_unique_id(
|
|
|
|
hass: HomeAssistant, init_integration: MockConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Test we set unique ID if not set yet."""
|
2020-01-04 21:48:31 +00:00
|
|
|
assert hass.data[DOMAIN]
|
2021-06-11 09:36:54 +00:00
|
|
|
assert init_integration.unique_id == "aabbccddeeff"
|
2022-01-21 08:19:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_error_config_entry_with_cct_channel(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
mock_wled: AsyncMock,
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
) -> None:
|
|
|
|
"""Test the WLED fails entry setup with a CCT channel."""
|
|
|
|
mock_wled.update.return_value.info.leds.cct = True
|
|
|
|
|
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Ensure config entry is errored and are connected and disconnected
|
|
|
|
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
|
|
|
assert "has a CCT channel, which is not supported" in caplog.text
|