Improve test coverage for letpot (#138420)

This commit is contained in:
Joris Pelgröm 2025-02-13 09:22:05 +01:00 committed by GitHub
parent 1ac16f6dbf
commit 737baaef2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 100 additions and 3 deletions

View File

@ -44,7 +44,7 @@ rules:
log-when-unavailable: todo
parallel-updates: done
reauthentication-flow: done
test-coverage: todo
test-coverage: done
# Gold
devices: done

View File

@ -2,7 +2,11 @@
from unittest.mock import MagicMock
from letpot.exceptions import LetPotAuthenticationException, LetPotConnectionException
from letpot.exceptions import (
LetPotAuthenticationException,
LetPotConnectionException,
LetPotException,
)
import pytest
from homeassistant.config_entries import ConfigEntryState
@ -94,3 +98,34 @@ async def test_get_devices_exceptions(
assert mock_config_entry.state is config_entry_state
mock_client.get_devices.assert_called_once()
mock_device_client.subscribe.assert_not_called()
async def test_device_subscribe_authentication_exception(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
mock_device_client: MagicMock,
) -> None:
"""Test config entry errors if it is not allowed to subscribe to device updates."""
mock_device_client.subscribe.side_effect = LetPotAuthenticationException
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
mock_device_client.subscribe.assert_called_once()
mock_device_client.get_current_status.assert_not_called()
async def test_device_refresh_exception(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
mock_device_client: MagicMock,
) -> None:
"""Test config entry errors with retry if getting a device state update fails."""
mock_device_client.get_current_status.side_effect = LetPotException
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
mock_device_client.get_current_status.assert_called_once()

View File

@ -6,7 +6,11 @@ from letpot.exceptions import LetPotConnectionException, LetPotException
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.components.switch import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -32,6 +36,44 @@ async def test_all_entities(
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize(
("service", "parameter_value"),
[
(
SERVICE_TURN_ON,
True,
),
(
SERVICE_TURN_OFF,
False,
),
(
SERVICE_TOGGLE,
False, # Mock switch is on after setup, toggle will turn off
),
],
)
async def test_set_switch(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
mock_device_client: MagicMock,
service: str,
parameter_value: bool,
) -> None:
"""Test switch entity turned on/turned off/toggled."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
"switch",
service,
blocking=True,
target={"entity_id": "switch.garden_power"},
)
mock_device_client.set_power.assert_awaited_once_with(parameter_value)
@pytest.mark.parametrize(
("service", "exception", "user_error"),
[

View File

@ -33,6 +33,26 @@ async def test_all_entities(
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_set_time(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
mock_device_client: MagicMock,
) -> None:
"""Test setting the time entity."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
"time",
SERVICE_SET_VALUE,
service_data={"time": time(hour=7, minute=0)},
blocking=True,
target={"entity_id": "time.garden_light_on"},
)
mock_device_client.set_light_schedule.assert_awaited_once_with(time(7, 0), None)
@pytest.mark.parametrize(
("exception", "user_error"),
[