2021-11-11 06:22:52 +00:00
|
|
|
"""Tests for the WLED button platform."""
|
2022-05-13 08:18:16 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
from freezegun import freeze_time
|
|
|
|
import pytest
|
|
|
|
from wled import WLEDConnectionError, WLEDError
|
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
from homeassistant.components.button import (
|
|
|
|
DOMAIN as BUTTON_DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
ButtonDeviceClass,
|
|
|
|
)
|
2021-11-11 06:22:52 +00:00
|
|
|
from homeassistant.const import (
|
2021-11-30 11:12:08 +00:00
|
|
|
ATTR_DEVICE_CLASS,
|
2021-11-11 06:22:52 +00:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNKNOWN,
|
2023-02-09 19:15:37 +00:00
|
|
|
EntityCategory,
|
2021-11-11 06:22:52 +00:00
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-05-24 14:30:41 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-11-11 06:22:52 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
2021-11-16 18:58:04 +00:00
|
|
|
async def test_button_restart(
|
2021-11-11 06:22:52 +00:00
|
|
|
hass: HomeAssistant, init_integration: MockConfigEntry, mock_wled: MagicMock
|
|
|
|
) -> None:
|
|
|
|
"""Test the creation and values of the WLED button."""
|
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("button.wled_rgb_light_restart")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNKNOWN
|
2021-11-30 11:12:08 +00:00
|
|
|
assert state.attributes[ATTR_DEVICE_CLASS] == ButtonDeviceClass.RESTART
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
entry = entity_registry.async_get("button.wled_rgb_light_restart")
|
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == "aabbccddeeff_restart"
|
2021-12-18 15:17:55 +00:00
|
|
|
assert entry.entity_category is EntityCategory.CONFIG
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
BUTTON_DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_wled.reset.call_count == 1
|
|
|
|
mock_wled.reset.assert_called_with()
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2021-11-04 17:37:00", tz_offset=-1)
|
|
|
|
async def test_button_error(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
init_integration: MockConfigEntry,
|
|
|
|
mock_wled: MagicMock,
|
|
|
|
) -> None:
|
|
|
|
"""Test error handling of the WLED buttons."""
|
|
|
|
mock_wled.reset.side_effect = WLEDError
|
|
|
|
|
2022-05-24 14:30:41 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
|
|
|
|
await hass.services.async_call(
|
|
|
|
BUTTON_DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
state = hass.states.get("button.wled_rgb_light_restart")
|
|
|
|
assert state
|
|
|
|
assert state.state == "2021-11-04T16:37:00+00:00"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_button_connection_error(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
init_integration: MockConfigEntry,
|
|
|
|
mock_wled: MagicMock,
|
|
|
|
) -> None:
|
|
|
|
"""Test error handling of the WLED buttons."""
|
|
|
|
mock_wled.reset.side_effect = WLEDConnectionError
|
|
|
|
|
2022-05-24 14:30:41 +00:00
|
|
|
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
|
|
|
|
await hass.services.async_call(
|
|
|
|
BUTTON_DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
state = hass.states.get("button.wled_rgb_light_restart")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|