Velbus make sure the services throw exceptions (#159583)

This commit is contained in:
Maikel Punie
2025-12-23 13:39:16 +01:00
committed by GitHub
parent e6d8092c37
commit 3e498d289b
4 changed files with 113 additions and 14 deletions
+55 -3
View File
@@ -1,6 +1,6 @@
"""Velbus services tests."""
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, patch
import pytest
import voluptuous as vol
@@ -16,7 +16,7 @@ from homeassistant.components.velbus.const import (
)
from homeassistant.const import CONF_ADDRESS
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from . import init_integration
@@ -64,6 +64,26 @@ async def test_global_services_with_config_entry(
blocking=True,
)
# Test scan with OSError
config_entry.runtime_data.controller.scan.side_effect = OSError("Boom")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SCAN,
{CONF_CONFIG_ENTRY: config_entry.entry_id},
blocking=True,
)
# Test sync_clock with OSError
config_entry.runtime_data.controller.sync_clock.side_effect = OSError("Boom")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SYNC,
{CONF_CONFIG_ENTRY: config_entry.entry_id},
blocking=True,
)
async def test_set_memo_text(
hass: HomeAssistant,
@@ -87,9 +107,26 @@ async def test_set_memo_text(
1
).set_memo_text.assert_called_once_with("Test")
# Test with OSError
controller.return_value.get_module.return_value.set_memo_text.side_effect = OSError(
"Boom"
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SET_MEMO_TEXT,
{
CONF_CONFIG_ENTRY: config_entry.entry_id,
CONF_MEMO_TEXT: "Test",
CONF_ADDRESS: 2,
},
blocking=True,
)
controller.return_value.get_module.return_value.set_memo_text.side_effect = None
# Test with unfound module
controller.return_value.get_module.return_value = None
with pytest.raises(ServiceValidationError, match="Module not found"):
with pytest.raises(ServiceValidationError, match="Module with address 2 not found"):
await hass.services.async_call(
DOMAIN,
SERVICE_SET_MEMO_TEXT,
@@ -124,3 +161,18 @@ async def test_clear_cache(
blocking=True,
)
assert config_entry.runtime_data.controller.scan.call_count == 2
# Test with OSError
with (
patch("os.unlink", side_effect=OSError("Boom")),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
DOMAIN,
SERVICE_CLEAR_CACHE,
{
CONF_CONFIG_ENTRY: config_entry.entry_id,
CONF_ADDRESS: 2,
},
blocking=True,
)