Cleanups in WLED tests (#88480)

This commit is contained in:
Franck Nijhof 2023-02-20 13:00:02 +01:00 committed by GitHub
parent 92815c9948
commit 36e59fc322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 226 additions and 474 deletions

View file

@ -1,5 +1,5 @@
"""Tests for the WLED update platform."""
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import MagicMock
import pytest
from wled import WLEDError
@ -30,19 +30,14 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("init_integration")
async def test_update_available(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test the firmware update available."""
entity_registry = er.async_get(hass)
state = hass.states.get("update.wled_rgb_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
assert state.attributes.get(ATTR_DEVICE_CLASS) == UpdateDeviceClass.FIRMWARE
assert state.state == STATE_ON
assert (
@ -63,23 +58,17 @@ async def test_update_available(
assert state.attributes[ATTR_TITLE] == "WLED"
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("update.wled_rgb_light_firmware")
assert entry
assert (entry := entity_registry.async_get("update.wled_rgb_light_firmware"))
assert entry.unique_id == "aabbccddeeff"
assert entry.entity_category is EntityCategory.CONFIG
@pytest.mark.parametrize("mock_wled", ["wled/rgb_no_update.json"], indirect=True)
@pytest.mark.parametrize("device_fixture", ["rgb_no_update"])
async def test_update_information_available(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test having no update information available at all."""
entity_registry = er.async_get(hass)
state = hass.states.get("update.wled_rgb_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
assert state.attributes.get(ATTR_DEVICE_CLASS) == UpdateDeviceClass.FIRMWARE
assert state.state == STATE_UNKNOWN
assert state.attributes[ATTR_INSTALLED_VERSION] is None
@ -99,18 +88,13 @@ async def test_update_information_available(
assert entry.entity_category is EntityCategory.CONFIG
@pytest.mark.parametrize("mock_wled", ["wled/rgb_websocket.json"], indirect=True)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize("device_fixture", ["rgb_websocket"])
async def test_no_update_available(
hass: HomeAssistant,
entity_registry_enabled_by_default: AsyncMock,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test there is no update available."""
entity_registry = er.async_get(hass)
state = hass.states.get("update.wled_websocket_firmware")
assert state
assert (state := hass.states.get("update.wled_websocket_firmware"))
assert state.state == STATE_OFF
assert state.attributes.get(ATTR_DEVICE_CLASS) == UpdateDeviceClass.FIRMWARE
assert state.attributes[ATTR_INSTALLED_VERSION] == "0.12.0-b2"
@ -129,15 +113,13 @@ async def test_no_update_available(
assert ATTR_ICON not in state.attributes
entry = entity_registry.async_get("update.wled_websocket_firmware")
assert entry
assert (entry := entity_registry.async_get("update.wled_websocket_firmware"))
assert entry.unique_id == "aabbccddeeff"
assert entry.entity_category is EntityCategory.CONFIG
async def test_update_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
@ -150,17 +132,14 @@ async def test_update_error(
{ATTR_ENTITY_ID: "update.wled_rgb_light_firmware"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("update.wled_rgb_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
assert state.state == STATE_UNAVAILABLE
assert "Invalid response from API" in caplog.text
async def test_update_stay_stable(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
) -> None:
"""Test the update entity staying on stable.
@ -169,8 +148,7 @@ async def test_update_stay_stable(
is currently running a stable version. Therefore, the update entity should
update to the next stable (even though beta is newer).
"""
state = hass.states.get("update.wled_rgb_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
assert state.state == STATE_ON
assert state.attributes[ATTR_INSTALLED_VERSION] == "0.8.5"
assert state.attributes[ATTR_LATEST_VERSION] == "0.12.0"
@ -181,15 +159,13 @@ async def test_update_stay_stable(
{ATTR_ENTITY_ID: "update.wled_rgb_light_firmware"},
blocking=True,
)
await hass.async_block_till_done()
assert mock_wled.upgrade.call_count == 1
mock_wled.upgrade.assert_called_with(version="0.12.0")
@pytest.mark.parametrize("mock_wled", ["wled/rgbw.json"], indirect=True)
@pytest.mark.parametrize("device_fixture", ["rgbw"])
async def test_update_beta_to_stable(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
) -> None:
"""Test the update entity.
@ -198,8 +174,7 @@ async def test_update_beta_to_stable(
is currently a beta, however, a newer stable is available. Therefore, the
update entity should update to the next stable.
"""
state = hass.states.get("update.wled_rgbw_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgbw_light_firmware"))
assert state.state == STATE_ON
assert state.attributes[ATTR_INSTALLED_VERSION] == "0.8.6b4"
assert state.attributes[ATTR_LATEST_VERSION] == "0.8.6"
@ -210,15 +185,13 @@ async def test_update_beta_to_stable(
{ATTR_ENTITY_ID: "update.wled_rgbw_light_firmware"},
blocking=True,
)
await hass.async_block_till_done()
assert mock_wled.upgrade.call_count == 1
mock_wled.upgrade.assert_called_with(version="0.8.6")
@pytest.mark.parametrize("mock_wled", ["wled/rgb_single_segment.json"], indirect=True)
@pytest.mark.parametrize("device_fixture", ["rgb_single_segment"])
async def test_update_stay_beta(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
) -> None:
"""Test the update entity.
@ -226,8 +199,7 @@ async def test_update_stay_beta(
There is an update for beta and the device is currently a beta. Therefore,
the update entity should update to the next beta.
"""
state = hass.states.get("update.wled_rgb_light_firmware")
assert state
assert (state := hass.states.get("update.wled_rgb_light_firmware"))
assert state.state == STATE_ON
assert state.attributes[ATTR_INSTALLED_VERSION] == "0.8.6b1"
assert state.attributes[ATTR_LATEST_VERSION] == "0.8.6b2"
@ -238,6 +210,5 @@ async def test_update_stay_beta(
{ATTR_ENTITY_ID: "update.wled_rgb_light_firmware"},
blocking=True,
)
await hass.async_block_till_done()
assert mock_wled.upgrade.call_count == 1
mock_wled.upgrade.assert_called_with(version="0.8.6b2")