Simplify reolink light tests (#147637)

This commit is contained in:
Abílio Costa 2025-06-27 07:58:09 +01:00 committed by GitHub
parent 1ca03c8ae9
commit e481f14335
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,14 +22,23 @@ from .conftest import TEST_NVR_NAME
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("whiteled_brightness", "expected_brightness"),
[
(100, 255),
(None, None),
],
)
async def test_light_state( async def test_light_state(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_host: MagicMock, reolink_host: MagicMock,
whiteled_brightness: int | None,
expected_brightness: int | None,
) -> None: ) -> None:
"""Test light entity state with floodlight.""" """Test light entity state with floodlight."""
reolink_host.whiteled_state.return_value = True reolink_host.whiteled_state.return_value = True
reolink_host.whiteled_brightness.return_value = 100 reolink_host.whiteled_brightness.return_value = whiteled_brightness
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -40,28 +49,7 @@ async def test_light_state(
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes["brightness"] == 255 assert state.attributes["brightness"] == expected_brightness
async def test_light_brightness_none(
hass: HomeAssistant,
config_entry: MockConfigEntry,
reolink_host: MagicMock,
) -> None:
"""Test light entity with floodlight and brightness returning None."""
reolink_host.whiteled_state.return_value = True
reolink_host.whiteled_brightness.return_value = None
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight"
state = hass.states.get(entity_id)
assert state.state == STATE_ON
assert state.attributes["brightness"] is None
async def test_light_turn_off( async def test_light_turn_off(
@ -118,30 +106,36 @@ async def test_light_turn_on(
[call(0, brightness=20), call(0, state=True)] [call(0, brightness=20), call(0, state=True)]
) )
reolink_host.set_whiteled.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
reolink_host.set_whiteled.side_effect = ReolinkError("Test error") @pytest.mark.parametrize(
with pytest.raises(HomeAssistantError): ("exception", "service_data"),
await hass.services.async_call( [
LIGHT_DOMAIN, (ReolinkError("Test error"), {}),
SERVICE_TURN_ON, (ReolinkError("Test error"), {ATTR_BRIGHTNESS: 51}),
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, (InvalidParameterError("Test error"), {ATTR_BRIGHTNESS: 51}),
blocking=True, ],
) )
async def test_light_turn_on_errors(
hass: HomeAssistant,
config_entry: MockConfigEntry,
reolink_host: MagicMock,
exception: Exception,
service_data: dict,
) -> None:
"""Test light turn on service error cases."""
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
reolink_host.set_whiteled.side_effect = InvalidParameterError("Test error") entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight"
reolink_host.set_whiteled.side_effect = exception
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
LIGHT_DOMAIN, LIGHT_DOMAIN,
SERVICE_TURN_ON, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, {ATTR_ENTITY_ID: entity_id, **service_data},
blocking=True, blocking=True,
) )