Complete tests for eheimdigital (#143337)

* Complete tests for eheimdigital

* Review

* Review

* Review

* Review

* Fix tests
This commit is contained in:
Sid 2025-07-02 10:09:30 +02:00 committed by GitHub
parent afb247c907
commit 088c02d38a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 10 deletions

View File

@ -2,8 +2,9 @@
from unittest.mock import MagicMock
from eheimdigital.types import EheimDeviceType
from eheimdigital.types import EheimDeviceType, EheimDigitalClientError
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
@ -54,3 +55,15 @@ async def test_remove_device(
device_entry.id, mock_config_entry.entry_id
)
assert response["success"]
async def test_entry_setup_error(
hass: HomeAssistant,
eheimdigital_hub_mock: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test errors on setting up the config entry."""
eheimdigital_hub_mock.return_value.connect.side_effect = EheimDigitalClientError()
await init_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY

View File

@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
from aiohttp import ClientError
from eheimdigital.classic_led_ctrl import EheimDigitalClassicLEDControl
from eheimdigital.types import EheimDeviceType
from eheimdigital.types import EheimDeviceType, EheimDigitalClientError
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
@ -24,6 +24,7 @@ from homeassistant.const import (
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.util.color import value_to_brightness
@ -114,20 +115,34 @@ async def test_dynamic_new_devices(
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.usefixtures("eheimdigital_hub_mock")
async def test_turn_off(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
eheimdigital_hub_mock: MagicMock,
classic_led_ctrl_mock: EheimDigitalClassicLEDControl,
) -> None:
"""Test turning off the light."""
await init_integration(hass, mock_config_entry)
await mock_config_entry.runtime_data._async_device_found(
await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"](
"00:00:00:00:00:01", EheimDeviceType.VERSION_EHEIM_CLASSIC_LED_CTRL_PLUS_E
)
await hass.async_block_till_done()
classic_led_ctrl_mock.hub.send_packet.side_effect = EheimDigitalClientError
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.mock_classicledcontrol_e_channel_1"},
blocking=True,
)
assert exc_info.value.translation_key == "communication_error"
classic_led_ctrl_mock.hub.send_packet.side_effect = None
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
@ -140,9 +155,9 @@ async def test_turn_off(
for call in classic_led_ctrl_mock.hub.mock_calls
if call[0] == "send_packet"
]
assert len(calls) == 2
assert calls[0][1][0].get("title") == "MAN_MODE"
assert calls[1][1][0]["currentValues"][1] == 0
assert len(calls) == 3
assert calls[1][1][0].get("title") == "MAN_MODE"
assert calls[2][1][0]["currentValues"][1] == 0
@pytest.mark.parametrize(
@ -169,6 +184,23 @@ async def test_turn_on_brightness(
)
await hass.async_block_till_done()
classic_led_ctrl_mock.hub.send_packet.side_effect = EheimDigitalClientError
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "light.mock_classicledcontrol_e_channel_1",
ATTR_BRIGHTNESS: dim_input,
},
blocking=True,
)
assert exc_info.value.translation_key == "communication_error"
classic_led_ctrl_mock.hub.send_packet.side_effect = None
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
@ -184,9 +216,9 @@ async def test_turn_on_brightness(
for call in classic_led_ctrl_mock.hub.mock_calls
if call[0] == "send_packet"
]
assert len(calls) == 2
assert calls[0][1][0].get("title") == "MAN_MODE"
assert calls[1][1][0]["currentValues"][1] == expected_dim_value
assert len(calls) == 3
assert calls[1][1][0].get("title") == "MAN_MODE"
assert calls[2][1][0]["currentValues"][1] == expected_dim_value
async def test_turn_on_effect(