Add fixture for enabling disabled entities in WLED tests (#68087)

pull/68113/head
Franck Nijhof 2022-03-14 09:50:55 +01:00 committed by GitHub
parent cea21a00b3
commit dfdf5f2583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 61 deletions

View File

@ -1,5 +1,6 @@
"""Fixtures for component testing."""
from unittest.mock import patch
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
@ -22,3 +23,13 @@ def prevent_io():
return_value=[],
):
yield
@pytest.fixture
def entity_registry_enabled_by_default() -> Generator[AsyncMock, None, None]:
"""Test fixture that ensures all entities are enabled in the registry."""
with patch(
"homeassistant.helpers.entity.Entity.entity_registry_enabled_default",
return_value=True,
) as mock_entity_registry_enabled_by_default:
yield mock_entity_registry_enabled_by_default

View File

@ -1,6 +1,6 @@
"""Tests for the WLED sensor platform."""
from datetime import datetime
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@ -28,61 +28,12 @@ async def test_sensors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_wled: MagicMock,
entity_registry_enabled_by_default: AsyncMock,
) -> None:
"""Test the creation and values of the WLED sensors."""
registry = er.async_get(hass)
# Pre-create registry entries for disabled by default sensors
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_uptime",
suggested_object_id="wled_rgb_light_uptime",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_free_heap",
suggested_object_id="wled_rgb_light_free_memory",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_wifi_signal",
suggested_object_id="wled_rgb_light_wifi_signal",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_wifi_rssi",
suggested_object_id="wled_rgb_light_wifi_rssi",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_wifi_channel",
suggested_object_id="wled_rgb_light_wifi_channel",
disabled_by=None,
)
registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"aabbccddeeff_wifi_bssid",
suggested_object_id="wled_rgb_light_wifi_bssid",
disabled_by=None,
)
# Setup
mock_config_entry.add_to_hass(hass)
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=dt_util.UTC)
with patch("homeassistant.components.wled.sensor.utcnow", return_value=test_time):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
@ -124,19 +75,19 @@ async def test_sensors(
assert entry.unique_id == "aabbccddeeff_free_heap"
assert entry.entity_category is EntityCategory.DIAGNOSTIC
state = hass.states.get("sensor.wled_rgb_light_wifi_signal")
state = hass.states.get("sensor.wled_rgb_light_wi_fi_signal")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:wifi"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert state.state == "76"
assert entry.entity_category is EntityCategory.DIAGNOSTIC
entry = registry.async_get("sensor.wled_rgb_light_wifi_signal")
entry = registry.async_get("sensor.wled_rgb_light_wi_fi_signal")
assert entry
assert entry.unique_id == "aabbccddeeff_wifi_signal"
assert entry.entity_category is EntityCategory.DIAGNOSTIC
state = hass.states.get("sensor.wled_rgb_light_wifi_rssi")
state = hass.states.get("sensor.wled_rgb_light_wi_fi_rssi")
assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.SIGNAL_STRENGTH
assert (
@ -145,29 +96,29 @@ async def test_sensors(
)
assert state.state == "-62"
entry = registry.async_get("sensor.wled_rgb_light_wifi_rssi")
entry = registry.async_get("sensor.wled_rgb_light_wi_fi_rssi")
assert entry
assert entry.unique_id == "aabbccddeeff_wifi_rssi"
assert entry.entity_category is EntityCategory.DIAGNOSTIC
state = hass.states.get("sensor.wled_rgb_light_wifi_channel")
state = hass.states.get("sensor.wled_rgb_light_wi_fi_channel")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:wifi"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.state == "11"
entry = registry.async_get("sensor.wled_rgb_light_wifi_channel")
entry = registry.async_get("sensor.wled_rgb_light_wi_fi_channel")
assert entry
assert entry.unique_id == "aabbccddeeff_wifi_channel"
assert entry.entity_category is EntityCategory.DIAGNOSTIC
state = hass.states.get("sensor.wled_rgb_light_wifi_bssid")
state = hass.states.get("sensor.wled_rgb_light_wi_fi_bssid")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:wifi"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.state == "AA:AA:AA:AA:AA:BB"
entry = registry.async_get("sensor.wled_rgb_light_wifi_bssid")
entry = registry.async_get("sensor.wled_rgb_light_wi_fi_bssid")
assert entry
assert entry.unique_id == "aabbccddeeff_wifi_bssid"
assert entry.entity_category is EntityCategory.DIAGNOSTIC