Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
3.7 KiB
Python
Raw Permalink Normal View History

"""Tests for the WLED button platform."""
2025-12-29 16:26:49 +01:00
from collections.abc import Generator
from unittest.mock import MagicMock, patch
import pytest
2023-03-04 11:45:53 +01:00
from syrupy.assertion import SnapshotAssertion
from wled import WLEDConnectionError, WLEDError
2023-03-04 11:45:53 +01:00
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.components.wled.const import DOMAIN
2025-12-29 16:26:49 +01:00
from homeassistant.const import (
ATTR_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant
2022-05-24 16:30:41 +02:00
from homeassistant.exceptions import HomeAssistantError
2023-03-04 11:45:53 +01:00
from homeassistant.helpers import device_registry as dr, entity_registry as er
2025-12-29 16:26:49 +01:00
from tests.common import MockConfigEntry, snapshot_platform
2023-03-04 11:45:53 +01:00
pytestmark = [
pytest.mark.usefixtures("init_integration"),
2023-08-25 15:59:30 +02:00
pytest.mark.freeze_time("2021-11-04 17:36:59+01:00"),
2023-03-04 11:45:53 +01:00
]
2025-12-29 16:26:49 +01:00
@pytest.fixture(autouse=True)
def override_platforms() -> Generator[None]:
"""Override PLATFORMS."""
with patch("homeassistant.components.wled.PLATFORMS", [Platform.BUTTON]):
yield
async def test_snapshots(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test snapshot of the platform."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_device_snapshot(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test device snapshot."""
assert (entity_entry := entity_registry.async_get("button.wled_rgb_light_restart"))
assert entity_entry.device_id
assert (device_entry := device_registry.async_get(entity_entry.device_id))
assert device_entry == snapshot
2021-11-16 19:58:04 +01:00
async def test_button_restart(
2023-03-04 11:45:53 +01:00
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_wled: MagicMock,
snapshot: SnapshotAssertion,
) -> None:
2025-12-29 16:26:49 +01:00
"""Test the behavior of the restart button."""
2023-02-20 13:00:02 +01:00
assert (state := hass.states.get("button.wled_rgb_light_restart"))
2023-03-04 11:45:53 +01:00
assert state.state == STATE_UNKNOWN
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
assert mock_wled.reset.call_count == 1
mock_wled.reset.assert_called_with()
2023-03-04 11:45:53 +01:00
assert (state := hass.states.get("button.wled_rgb_light_restart"))
assert state.state == "2021-11-04T16:37:00+00:00"
2025-12-29 16:26:49 +01:00
@pytest.mark.parametrize(
("side_effect", "expected_state", "expected_translation_key"),
[
(WLEDError, "2021-11-04T16:37:00+00:00", "invalid_response_wled_error"),
(WLEDConnectionError, STATE_UNAVAILABLE, "connection_error"),
],
)
async def test_button_restart_errors(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_wled: MagicMock,
side_effect: Exception,
expected_state: str,
expected_translation_key: str,
) -> None:
"""Test the error handling of the restart button."""
2023-03-04 11:45:53 +01:00
# Test with WLED connection error
2025-12-29 16:26:49 +01:00
mock_wled.reset.side_effect = side_effect
with pytest.raises(HomeAssistantError) as ex:
2022-05-24 16:30:41 +02:00
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
assert ex.value.translation_domain == DOMAIN
2025-12-29 16:26:49 +01:00
assert ex.value.translation_key == expected_translation_key
2023-03-04 11:45:53 +01:00
# Ensure this made the entity unavailable
2023-02-20 13:00:02 +01:00
assert (state := hass.states.get("button.wled_rgb_light_restart"))
2025-12-29 16:26:49 +01:00
assert state.state == expected_state