Files
core/tests/components/velux/test_light.py
T

73 lines
2.6 KiB
Python

"""Test Velux light entities."""
from unittest.mock import AsyncMock
import pytest
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
@pytest.fixture
def platform() -> Platform:
"""Fixture to specify platform to test."""
return Platform.LIGHT
@pytest.mark.usefixtures("setup_integration")
async def test_light_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
mock_light: AsyncMock,
) -> None:
"""Test light entity setup and device association."""
test_entity_id = f"light.{mock_light.name.lower().replace(' ', '_')}"
# Check that the entity exists and its name matches the node name (the light is the main feature).
state = hass.states.get(test_entity_id)
assert state is not None
assert state.attributes.get("friendly_name") == mock_light.name
# Get entity + device entry
entity_entry = entity_registry.async_get(test_entity_id)
assert entity_entry is not None
assert entity_entry.device_id is not None
device_entry = device_registry.async_get(entity_entry.device_id)
assert device_entry is not None
# Verify device has correct identifiers + name
assert ("velux", mock_light.serial_number) in device_entry.identifiers
assert device_entry.name == mock_light.name
# This test is not light specific, it just uses the light platform to test the base entity class.
@pytest.mark.usefixtures("setup_integration")
async def test_entity_callbacks(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_light: AsyncMock,
) -> None:
"""Ensure the entity unregisters its device-updated callback when unloaded."""
# Entity is created by setup_integration; callback should be registered
test_entity_id = f"light.{mock_light.name.lower().replace(' ', '_')}"
state = hass.states.get(test_entity_id)
assert state is not None
# Callback is registered exactly once with a callable
assert mock_light.register_device_updated_cb.call_count == 1
cb = mock_light.register_device_updated_cb.call_args[0][0]
assert callable(cb)
# Unload the config entry to trigger async_will_remove_from_hass
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
# Callback must be unregistered with the same callable
assert mock_light.unregister_device_updated_cb.call_count == 1
assert mock_light.unregister_device_updated_cb.call_args[0][0] is cb