mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve test coverage for letpot (#138420)
This commit is contained in:
parent
1ac16f6dbf
commit
737baaef2b
@ -44,7 +44,7 @@ rules:
|
|||||||
log-when-unavailable: todo
|
log-when-unavailable: todo
|
||||||
parallel-updates: done
|
parallel-updates: done
|
||||||
reauthentication-flow: done
|
reauthentication-flow: done
|
||||||
test-coverage: todo
|
test-coverage: done
|
||||||
|
|
||||||
# Gold
|
# Gold
|
||||||
devices: done
|
devices: done
|
||||||
|
@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
from letpot.exceptions import LetPotAuthenticationException, LetPotConnectionException
|
from letpot.exceptions import (
|
||||||
|
LetPotAuthenticationException,
|
||||||
|
LetPotConnectionException,
|
||||||
|
LetPotException,
|
||||||
|
)
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
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
|
assert mock_config_entry.state is config_entry_state
|
||||||
mock_client.get_devices.assert_called_once()
|
mock_client.get_devices.assert_called_once()
|
||||||
mock_device_client.subscribe.assert_not_called()
|
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()
|
||||||
|
@ -6,7 +6,11 @@ from letpot.exceptions import LetPotConnectionException, LetPotException
|
|||||||
import pytest
|
import pytest
|
||||||
from syrupy import SnapshotAssertion
|
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.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
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)
|
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(
|
@pytest.mark.parametrize(
|
||||||
("service", "exception", "user_error"),
|
("service", "exception", "user_error"),
|
||||||
[
|
[
|
||||||
|
@ -33,6 +33,26 @@ async def test_all_entities(
|
|||||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
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(
|
@pytest.mark.parametrize(
|
||||||
("exception", "user_error"),
|
("exception", "user_error"),
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user